-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path_2048.java
More file actions
279 lines (258 loc) · 5.28 KB
/
Copy path_2048.java
File metadata and controls
279 lines (258 loc) · 5.28 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
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
import java.sql.SQLOutput;
public class _2048
{
private final int rows = 4;
private final int cols = 4;
private int[][] board;
private int[][] previousBoard;
private int score;
private int previousScore;
/**
* Initializes board and previousBoard using rows and cols.
* Uses the generateTile method to add two random tiles to board.
*/
public _2048()
{
board = new int[rows][cols];
generateTile();
generateTile();
}
/**
* Initializes the board of this object using the specified board.
* Initializes previousBoard using rows and cols.
*
* Precondition: the specified board is a 4x4 2D Array.
*
* @param board
*/
public _2048(int[][] board)
{
this.board = board;
previousBoard = new int[rows][cols];
}
/**
* Generates a tile and add it to an empty spot on the board.
* 80% chance to generate a 2
* 20% chance to generate a 4
*
* Does nothing if the board is full.
*/
private void generateTile()
{
if (!full()) {
if (Math.random() >= 0.2) {
while(true) {
int a = (int) (Math.random() * 4);
int b = (int) (Math.random() * 4);
if (board[a][b] == 0) {
board[a][b] = 2;
break;
}
}
}
else {
while(true) {
int a = (int) (Math.random() * 4);
int b = (int) (Math.random() * 4);
if (board[a][b] == 0) {
board[a][b] = 4;
break;
}
}
}
}
}
/**
* Returns false if the board contains a 0, true otherwise.
* @return
*/
private boolean full()
{
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
if (board[i][j] == 0) {
return false;
}
}
}
return true;
}
/**
* Returns the board.
* @return
*/
public int[][] getBoard()
{
return board;
}
/**
* Returns the score.
* @return
*/
public int getScore()
{
return score;
}
/**
* Saves board into previousBoard and score into previousScore
* then performs a move based on the specified direction:
*
* Valid directions (not case sensitive):
* up
* down
* left
* right
*
* Adds a new tile to the board using the generateTile method.
*
* @param direction
*/
public void move(String direction)
{
previousBoard = board;
previousScore = score;
direction.toLowerCase();
if (direction.equals("up")) {
moveUp();
moveUp();
moveUp();
moveUp();
}
else if (direction.equals("down")) {
moveDown();
moveDown();
moveDown();
moveDown();
}
else if (direction.equals("left")) {
moveLeft();
moveLeft();
moveLeft();
moveLeft();
}
else if (direction.equals("right")) {
moveRight();
moveRight();
moveRight();
moveRight();
}
generateTile();
}
/**
* Shifts all the tiles up, combines like tiles that collide.
*/
private void moveUp()
{
for (int i = 1; i < rows; i++) {
for (int j = 0; j < cols; j++) {
if (board[i][j] == board[i - 1][j]) {
board[i - 1][j] = board[i][j] + board[i - 1][j];
board[i][j] = 0;
}
else if (board[i - 1][j] == 0) {
board[i - 1][j] = board[i][j];
board[i][j] = 0;
}
}
}
}
/**
* Shifts all the tiles down, combines like tiles that collide.
*/
private void moveDown()
{
for (int i = rows - 2; i >= 0; i--) {
for (int j = 0; j < cols; j++) {
if (board[i][j] == board[i + 1][j]) {
board[i + 1][j] = board[i][j] + board[i + 1][j];
board[i][j] = 0;
}
else if (board[i + 1][j] == 0) {
board[i + 1][j] = board[i][j];
board[i][j] = 0;
}
}
}
}
/**
* Shifts all the tiles left, combines like tiles that collide.
*/
private void moveLeft()
{
for (int i = 0; i < rows; i++) {
for (int j = 1; j < cols; j++) {
if (board[i][j] == board[i][j - 1]) {
board[i][j - 1] = board[i][j] + board[i][j - 1];
board[i][j] = 0;
}
else if (board[i][j - 1] == 0) {
board[i][j - 1] = board[i][j];
board[i][j] = 0;
}
}
}
}
/**
* Shifts all the tiles right, combines like tiles that collide.
*/
private void moveRight()
{
for (int i = 0; i < rows; i++) {
for (int j = cols - 2; j >= 0; j--) {
if (board[i][j] == board[i][j + 1]) {
board[i][j + 1] = board[i][j] + board[i][j + 1];
board[i][j] = 0;
}
else if (board[i][j + 1] == 0) {
board[i][j + 1] = board[i][j];
board[i][j] = 0;
}
}
}
}
/**
* Sets board to previousBoard and score to previousScore
*/
public void undo()
{
board = previousBoard;
score = previousScore;
}
/**
* Returns true if the game is over, false otherwise.
* @return
*/
public boolean gameOver()
{
if (full() || score == 2048) {
return true;
}
else {
return false;
}
}
/**
* Returns a String representation of this object.
*/
public String toString()
{
String rtn = "";
for(int[] row : board)
{
rtn += "|";
for(int num : row)
if(num != 0)
{
String str = num + "";
while(str.length() < 4)
str = " " + str;
rtn += str;
}
else
rtn += " ";
rtn += "|\n";
}
rtn += "Score: " + score + "\n";
return rtn;
}
}