-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTwoDimensional2048.java
More file actions
282 lines (228 loc) · 7.08 KB
/
Copy pathTwoDimensional2048.java
File metadata and controls
282 lines (228 loc) · 7.08 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
280
281
282
import java.util.Scanner;
import java.util.Arrays;
public class TwoDimensional2048 {
///All the return values are set to default. You need to replace it with the correct format.
static boolean validateBoard(int[][] b){
for (int i = 0; i < b.length; i++) {
//if (!OneDimensional2048.validateRow(b[i])) {
return false;
//}
}
return true;
}
static int[][] blankBoard(int x, int y) {
//this is done
return new int[x][y];
}
static boolean identicalBoard(int[][] a, int[][] b) {
return Arrays.deepEquals(a, b);
}
static int numUnoccupied(int[][] b) {
int x = 0;
for (int i = 0; i < b.length; i++) {
for (int j = 0; j < b[i].length; j++) {
if (b[i][j] == 0) {
x++;
}
}
}
return x;
}
static int[] randCoord(int[][] b, int offset) {
int n = 0;
for (int i = 0; i < b.length; i++) {
for (int j = 0; j < b[i].length; j++) {
if (b[i][j] == 0) {
n++;
}
if (n == offset + 1) {
int[] A = {i, j};
return A;
}
}
}
return new int[]{0, 0};
}
static boolean addNewValue(int[][] b) {
int x = Rnd2048.randValue();
int y = Rnd2048.randNum(numUnoccupied(b));
int [] coords = randCoord(b, y);
b[coords[0]][coords[1]] = x;
return true;
}
/**
static int[][] combineLeft(int[][] b) {
int[][] B = copyBoard(b);
for (int i = 0; i < b.length; i++) {
OneDimensional2048.combineLeft(B[i]);
}
return B;
}
static int[][] rotateLeft(int[][] b) {
int[] readable = new int[b.length];
int[][] otherCases = new int[b[0].length][b.length];
for (int i = b[0].length-1; i>=0; i--) {
readable = new int [b.length];
for(int j = 0; j < b.length; j++){
readable[j] = b[j][i];
}
otherCases[b[0].length-1-i] = readable;
}
return otherCases;
}
static int[][] left(int[][] b) {
int[][] x = combineLeft(b);
return x;
}
static int[][] right(int[][] b) {
int [][] x = copyBoard(b);
x = rotateLeft(x);
x = rotateLeft(x);
x = combineLeft(x);
x = rotateLeft(x);
x = rotateLeft(x);
return x; //check this if asserts don't pass
}
static int[][] up(int[][] b) {
int [][] x = copyBoard(b);
x = rotateLeft(x);
x = combineLeft(x);
x = rotateLeft(x);
x = rotateLeft(x);
x = rotateLeft(x);
return x;
}
static int[][] down(int[][] b) {
int [][] x = copyBoard(b);
x = rotateLeft(x);
x = rotateLeft(x);
x = rotateLeft(x);
x = combineLeft(x);
x = rotateLeft(x);
return x;
}**/
static int[][] combineLeft(int[][] b) {
for(int i = 0; i< b.length; i++){
b[i] = combineLeftOneD(b[i]);
}
return b;
}
static int[][] rotateLeft(int[][] b) {
int[][] og = b.clone();
int[][] newB = new int[b[1].length][b.length];
for(int i = 0; i < og.length; i++){
int[] row = og[og.length - i -1];
/**
System.out.println("Row");
System.out.println(Arrays.toString(row));
**/
for(int j = 0; j < og[i].length; j++){
// System.out.println(Arrays.toString(og[i]));
newB[newB.length - j-1][i] = og[i][j];
}
}
return newB;
}
static int[][] left(int[][] b) {
return combineLeft(b);
}
static int[][] right(int[][] b) {
b = rotateLeft(b);
b = rotateLeft(b);
b = combineLeft(b);
b = rotateLeft(b);
b = rotateLeft(b);
return b;
}
static int[][] up(int[][] b) {
b = rotateLeft(b);
b = combineLeft(b);
for(int i = 0; i < b.length; i++){
b[i] = invertRow(b[i]);
}
b = rotateLeft(b);
for(int i = 0; i < b.length; i++){
b[i] = invertRow(b[i]);
}
return b;
}
static int[][] down(int[][] b) {
b = rotateLeft(b);
b = rotateLeft(b);
b = rotateLeft(b);
b = combineLeft(b);
for(int i = 0; i < b.length; i++){
b[i] = invertRow(b[i]);
}
b = rotateLeft(b);
b = rotateLeft(b);
b = rotateLeft(b);
for(int i = 0; i < b.length; i++){
b[i] = invertRow(b[i]);
}
return b;
}
public static int[] moveLeftOneD(int[] row) {
/**if(!validateRow(row)){
return row;
}
**/
for(int j = row.length - 1; j >0; j--){
for(int i = row.length - 1; i >0; i--){
if(row[i - 1] == 0){
row[i - 1] = row[i];
row[i] = 0;
}
}
}
return row;
}
public static int[] combineLeftOneD(int[] row) {
row = moveLeftOneD(row);
for(int i = 0; i <row.length; i++){
if(i+1 < row.length && row[i] == row[i+1]){
row[i] = 2*row[i];
row[i+1] = 0;
}
row = moveLeftOneD(row);
}
row = moveLeftOneD(row);
return row;
}
public static int[] invertRow(int[] row){
int[] og = row.clone();
for(int i = 0; i < og.length; i++){
row[i] = og[og.length - 1 -i];
}
return row;
}
//////////////////////////////////////////////////////////////////////optional methods
static int numMax(int[][] b){
return 0;
}
static int numOccupied(int[][] b){
return 0;
}
static boolean addValue(int[][] b, int x, int y,int val){
return true;
}
static int[][] copyBoard(int[][] b){
int[][] B = new int[b.length][b[0].length];
for (int i = 0; i < b.length; i++) {
for (int j = 0; j < b[i].length; j++) {
B[i][j] = b[i][j];
}
}
return B;
}
static void printBoard(int[][] b){
for (int i = 0; i < b.length; i++) {
for (int j = 0; j < b[i].length; j++) {
System.out.print(b[i][j] + " ");
if (j == b[i].length - 1) {
System.out.println();
}
}
}
}
}