-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTwenty48Board.java
334 lines (277 loc) · 6.98 KB
/
Twenty48Board.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
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
/**
* Board extension that gives specific functionality for the game of 2048
* @author Stephen
*
*/
public class Twenty48Board extends Board
{
private boolean movementOccured;
private int highScore;
public final int MAX_TILE = 2048;
public Twenty48Board(int row, int column)
{
//create the board with Twenty48Tiles
super(new Twenty48Tile[row][column]);
try{
//initialize all the tile objects with their location
for(int rows = 0; rows < row; rows++)
for(int cols = 0; cols < column; cols++)
getBoard()[rows][cols] = new Twenty48Tile(new Location(rows, cols), 0);
//create the 2 initial tiles w/ values
int r = (int)(Math.random() * 4);
int c = (int)(Math.random() * 4);
getBoard()[r][c] = new Twenty48Tile(new Location(r, c));
//loop through until finding a tile that has a value of 0
while(getBoard()[r][c].getValue() != 0)
{
r = (int)(Math.random() * 4);
c = (int)(Math.random() * 4);
}
getBoard()[r][c] = new Twenty48Tile(new Location(r, c));
//init the score object
setScore(new Integer(0));
}
catch(IncompatibleNumberException e)
{
System.out.println("Send help");
}
}
public int getHighScore()
{
return highScore;
}
public void setHighScore(int hS)
{
highScore = hS;
}
/**
* Saves the game board to an XML-like file
*/
public void saveToXML()
{
ArrayList<String> keys = new ArrayList<String>();
ArrayList<String> values = new ArrayList<String>();
String fileName = "save.xml";
for(int row = 0; row < 4; row++)
{
for(int col = 0; col < 4; col++)
keys.add("row" + row + "col" + col);
}
for(int row = 0; row < 4; row++)
{
for(int col = 0; col < 4; col++)
values.add(Integer.toString(get(row, col).getValue()));
}
keys.add("score");
values.add("" + getScore());
try {
SP_Lib.writeXML(keys, values, fileName);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
/**
* Reads the game board from the XML-like format used above
* @throws FileNotFoundException If the save file isn't found
*/
public void readFromXML() throws FileNotFoundException
{
Twenty48Tile[][] board = new Twenty48Tile[4][4];
BufferedReader reader = new BufferedReader(new FileReader("save.xml"));
String current = new String();
try {
int row, col, value;
while((current = reader.readLine()) != null && current.indexOf("score") == -1)
{
row = Character.getNumericValue(current.charAt(current.indexOf("row") + 3));
col = Character.getNumericValue(current.charAt(current.indexOf("col") + 3));
value = Integer.parseInt(current.substring(current.indexOf(">") + 1, current.indexOf("</")));
try
{
board[row][col] = new Twenty48Tile(new Location(row, col), value);
}
catch (IncompatibleNumberException e) {
e.printStackTrace();
}
}
}
catch (IOException e) {
e.printStackTrace();
}
setScore(Integer.parseInt(current.substring(current.indexOf(">") + 1, current.indexOf("</"))));
setBoard(board);
}
public Twenty48Tile get(Location loc)
{
return (Twenty48Tile)super.get(loc);
}
/**
* Increments the score of a given tile
* @param x Amount to increment by
*/
public void incrementScore(int x)
{
setScore((Integer)getScore() + x);
if((Integer)getScore() > highScore)
{
highScore = (Integer)getScore();
}
}
/**
* Moves all the tiles in the board in a given direction
* Also adds a new tile after movement is completed, and checks if the end game is reached
* @param dir Direction to move tiles
*/
public void moveBoard(Direction dir)
{
movementOccured = false;
switch(dir)
{
case UP:
{
for(int col = 0; col < 4; col++)
{
pushTile(new Location(3, col), dir);
}
break;
}
case DOWN:
{
for(int col = 0; col < 4; col++)
{
pushTile(new Location(0, col), dir);
}
break;
}
case RIGHT:
{
for(int row = 0; row < 4; row++)
{
pushTile(new Location(row, 0), dir);
}
break;
}
case LEFT:
{
for(int row = 0; row < 4; row++)
{
pushTile(new Location(row, 3), dir);
}
break;
}
}
reset();
if(movementOccured)
{
addTile(Types.RANDOM);
}
if(!checkForMoves())
endGame();
}
/**
* Resets the merged status of all tiles to TRUE
*/
public void reset()
{
for(int r = 0; r < 4; r++)
for(int c = 0; c < 4; c++)
get(new Location(r,c)).setCanBeMerged(true);
}
/**
* Pushes tiles in a given direction, recursively
* @param loc Location to push
* @param dir Direction to push in
*/
public void pushTile(Location loc, Direction dir)
{
if(loc == null) //base case 1 + checks for invalid input
return;
Location moveTo = getLocationInDirection(loc, dir); //location being moved into
if(moveTo == null) //main base case
return;
pushTile(moveTo, dir); //push all the tiles next to the current one away
//merge tiles if necessary
if(get(moveTo).getValue() == get(loc).getValue() && get(loc).getValue() != 0 && get(moveTo).canBeMerged() && get(loc).canBeMerged())
{
get(moveTo).increment();
incrementScore(get(moveTo).getValue());
set(new Twenty48Tile(loc, Types.EMPTY), loc);
get(moveTo).setCanBeMerged(false);
pushTile(loc, dir);
movementOccured = true;
}
else if(get(moveTo).getValue() == 0 && get(loc).getValue() != 0) //move tile if space available
{
moveTile(loc, moveTo);
movementOccured = true;
}
//push current tile into newly freed space
pushTile(moveTo, dir);
}
/**
* Adds a tile to the board
* @param t Type of addition
* @return Success of tile adding
*/
public boolean addTile(Types t)
{
switch(t)
{
case RANDOM: //add a tile in a random empty location on the board
{
ArrayList<Location> empty = getEmptyLocations();
if(empty.size() == 0)
{
if(!checkForMoves())
endGame();
return false;
}
int r = (int)(Math.random() * empty.size());
set(new Twenty48Tile(empty.get(r)), empty.get(r));
return true;
}
default:
return true;
}
}
/**
* Checks to see if there are any valid moves left on the board
* @return If there are valid moves left on the board
*/
public boolean checkForMoves()
{
if(getEmptyLocations().size() > 0)
return true;
for(int x = 0; x < 4; x++)
for(int y = 0; y < 4; y++)
if(canMergeAround(new Location(x, y)))
return true;
return false;
}
/**
* Checks if a given tile can merged with the tiles around it
* @param l Location of tile to check neighbors
* @return Whether the tile can merge with any of its neighbors
*/
public boolean canMergeAround(Location l)
{
ArrayList<Location> adjacent = getAdjacentLocations(l);
for(int x = 0; x < adjacent.size(); x++)
if(get(adjacent.get(x)).getValue() == get(l).getValue())
return true;
return false;
}
/**
* End the game
*/
public void endGame()
{
Display.updateBoard();
Display.endGame(false);
}
}