-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBoardGameHistory.java
More file actions
88 lines (70 loc) · 3.2 KB
/
Copy pathBoardGameHistory.java
File metadata and controls
88 lines (70 loc) · 3.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
/*
Description:
BoardGameHistory class extends GameHistory and represents the history of a board game. It stores the final board state,
total turns taken, and the game details from GameHistory in a read-only format.
Fields:
- boardState: The final state of the board at the end of the game.
- totalTurns: The total number of turns taken in the game.
Constructor:
- BoardGameHistory(Team winner, Board boardState, int totalTurns, int number): Initializes the game history with the winning team,
final board state, total turns, and game number.
Important Methods:
- toString(): Returns a formatted string representing the game history, including game details, winner, board size, and a sorted list of moves.
Uses a TreeMap to iterate over the board cells in row major order and sppending them to the final string in sorted order by turn.
Prints out the game ID if the game is a subgame of SuperTicTacToe.
*/
import java.util.Map;
import java.util.TreeMap;
public class BoardGameHistory extends GameHistory {
// READ ONLY: history cannot be changed
private final Board boardState;
private final int totalTurns;
private String gameID;
// CONSTRUCTOR
public BoardGameHistory(Team winner, Board boardState, int totalTurns, int number) {
super(winner, number);
this.boardState = boardState;
this.totalTurns = totalTurns;
this.gameID = null;
}
public BoardGameHistory(Team winner, Board boardState, int totalTurns, int number, String gameID) {
super(winner, number);
this.boardState = boardState;
this.totalTurns = totalTurns;
this.gameID = gameID;
}
// GETTER methods
public Board getBoardState() {
return this.boardState;
}
public int getTotalTurns() {
return this.totalTurns;
}
public String getGameID() {
return this.gameID;
}
// TO STRING METHOD
public String toString() {
String data = "GAME NUMBER:\t" + this.getGameNumber() + "\n";
if (this.gameID != null) data += "GAME ID:\t" + this.gameID + "\n";
data += "GAME NAME:\t" + this.boardState.getGameType() + "\n";
if (this.getWinner() != null) data += "WINNER TEAM:\tTeam " + this.getWinner().getName() + "\n";
else data += "GAME TIED\n";
data += "TURNS TAKEN:\t" + this.totalTurns + "\n";
data += "BOARD SIZE:\t" + this.boardState.getRows() + "x" + this.boardState.getColumns() + "\n";
data += "MOVES (NUMBER, POSITION, PLAYER, PIECE, TEAM):\n";
Map<Integer, String> sortedMoves = new TreeMap<>(); // use to add the moves in sorted order
for (Cell[] cellRow : this.boardState.getBoard()) {
for (Cell cell : cellRow) {
if (cell.getValue() != null) {
String cellData = cell.toString();
String[] splitCellData = cellData.split(",");
int turnNum = Integer.parseInt(splitCellData[0]);
sortedMoves.put(turnNum, cellData);
}
}
}
for (Map.Entry<Integer, String> entry : sortedMoves.entrySet()) data += entry.getValue();
return data;
}
}