-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBoard.java
242 lines (211 loc) · 6.1 KB
/
Board.java
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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
import java.util.ArrayList;
/**
* A board that contains 9x9 squares by default.
* This class is using singleton design pattern by having only one instance of
* Board class in the program.
* This class is also part of the memento design pattern used for saving game states,
* with this class acting as an originator.
*
* @author Muhammad Faishal Dzaky (1141326988)
* @author Mohamed Haryz Izzudin bin Mohamed Rafy (1141127874)
* @author Ramanan R Muralitharan (1141128291)
*/
public class Board {
/**
* The single Board instance.
*/
private static Board instance = new Board();
/**
* Array of Square indicating how many squares are on the board.
*/
private Square[][] squares;
/**
* List of players that are on the board.
*/
private ArrayList<Player> players;
/**
* A counter to keep track of the current player turn.
*/
private int playerTurn;
/**
* The save manager for the board.
*/
private SaveManager saveManager;
/**
* The path of the save file to save to.
*/
private final String saveFile = "save.txt";
/**
* Constructor for Board class.
* Sets the the size of board to 9x9 squares, and initialises the player list and save manager.
*
* @author Faishal
* @author Haryz
*/
private Board() {
squares = new Square[9][9];
for (int i = 0; i < 9; i++) {
for (int j = 0; j < 9; j++) {
squares[i][j] = new Square(i,j);
}
}
players = new ArrayList<Player>();
saveManager = new SaveManager();
}
/**
* Gets the instance of the board.
*
* @author Faishal
* @return The singleton instance of the board.
*/
public static Board getInstance() {
return instance;
}
/**
* Gets the instance of the square at coordinate X and Y.
*
* @author Faishal
* @param x The X coordinate of the square.
* @param y The Y coordinate of the square.
* @return The square at coordinate X and Y of the board.
*/
public Square getSquare(int x, int y) {
return squares[x][y];
}
/**
* Advances the turn counter.
* Wraps back to 0 if the counter advances beyond the number of available players.
*
* @author Haryz
* @return A boolean - true if the counter is modified correctly
*/
public boolean advanceTurn() {
playerTurn = (playerTurn + 1) % players.size();
return true;
}
/**
* Gets the player of the current turn.
*
* @author Ramanan
* @return A player - the player of the current turn
*/
public Player getCurrentPlayer() {
return players.get(playerTurn);
}
/**
* Gets the player of the next turn.
*
* @author Haryz
* @return A player - the player of the next turn
*/
public Player getNextPlayer() {
return players.get((playerTurn + 1) % players.size());
}
/**
* Gets the current player turn.
*
* @author Ramanan
* @return An int - the counter for the current player
*/
public int getPlayerTurn() {
return playerTurn;
}
/**
* Gets all the players in the board.
*
* @author Ramanan
* @return An array list of players - all the players on the board
*/
public ArrayList<Player> getAllPlayers() {
return players;
}
/**
* Saves the state of the board with the save manager.
*
* @author Ramanan
*/
public void save() {
saveManager.save(saveFile);
}
/**
* Loads the board to a previous state.
*
* @author Ramanan
*/
public void load() {
saveManager.load(saveFile);
}
/**
* Initialises the board for a new game.
* Clears the board, then adds players and special pieces to the board.
*
* @author Haryz
* @return A boolean - true if the board is successfully initialised
*/
public boolean initializeBoard() {
clearBoard();
playerTurn = 0;
for (int i = 0; i < 4; i++) {
try {
Player player = Piece.generateNewPlayer();
players.add(player);
} catch (Exception e) {
System.err.println(e);
System.exit(0);
}
}
Piece.generateNewChest();
for (int i = 0; i < 5; i++) {
try {
Piece.generateNewKey();
} catch (Exception e) {
System.err.println(e);
System.exit(0);
}
}
return true;
}
/**
* Restores the board to a previously saved state.
* Clears the board, then adds players and special pieces to the board.
*
* @author Ramanan
* @param save The save file to restore from.
* @return A boolean - true if the board is successfully initialised
*/
public boolean initializeBoard(SaveFile save) {
int playerTurn = save.getPlayerTurn();
ArrayList<Player> players = save.getPlayerData();
ArrayList<Key> keys = save.getKeyData();
String chestIconPath = save.getChestIconPath();
clearBoard();
Square chestSquare = getSquare(4, 4);
chestSquare.setSpecialPiece(new Chest(chestSquare, chestIconPath, 5));
for (Key key : keys) {
Square square = key.getSquare();
square.setSpecialPiece(key);
}
for (Player player : players) {
Player p = new Player(player);
this.players.add(p);
p.getSquare().placePlayer(p);
}
this.playerTurn = playerTurn;
return true;
}
/**
* Clears and resets the board's attributes. Private method.
*
* @author Haryz
* @return A boolean - true if the operation is successful
*/
private boolean clearBoard() {
players.clear();
for (int i = 0; i < 9; i++) {
for (int j = 0; j < 9; j++) {
squares[i][j].clear();
}
}
return true;
}
}