/
Twe.java
318 lines (308 loc) · 11.5 KB
/
Twe.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
import java.util.*;
class Twe {
public static void testDown(Twe twe){
int[] ref1,ref2,ref3,ref4;
System.out.println("Testing DOWN");
System.out.println("1: Simple moves");
twe.board=new int[][]{{2, 0, 0, 0},
{0, 2, 0, 0},
{0, 0, 2, 0},
{0, 0, 0, 2}};
ref1=new int[]{0,0,0,0};
ref2=new int[]{2,2,2,2};
assert twe.fold(true,true)>0 : "Down doesn't move when it should.";
assert Arrays.equals(twe.board[0],ref1) : "First row invalid on down.";
assert Arrays.equals(twe.board[1],ref1) : "Second row invalid on down.";
assert Arrays.equals(twe.board[2],ref1) : "Third row invalid on down.";
assert Arrays.equals(twe.board[3],ref2) : "Fourth row invalid on down.";
assert twe.fold(true,true)==0 : "Down moves when it shouldn't.";
System.out.println("2: Simple moves and merges");
twe.board=new int[][]{{2, 0, 0, 0},
{2, 4, 0, 2},
{0, 4, 2, 0},
{0, 0, 2, 2}};
ref1=new int[]{0,0,0,0};
ref2=new int[]{4,8,4,4};
assert twe.fold(true,true)>0 : "Down doesn't move when it should.";
assert Arrays.equals(twe.board[0],ref1) : "First row invalid on down.";
assert Arrays.equals(twe.board[1],ref1) : "Second row invalid on down.";
assert Arrays.equals(twe.board[2],ref1) : "Third row invalid on down.";
assert Arrays.equals(twe.board[3],ref2) : "Fourth row invalid on down.";
assert twe.fold(true,true)==0 : "Down moves when it shouldn't.";
System.out.println("3: Simple moves and merges B");
twe.board=new int[][]{{2, 4, 0, 2},
{0, 0, 4, 0},
{2, 0, 4, 4},
{0, 4, 4, 4}};
ref1=new int[]{0,0,0,0};
ref2=new int[]{0,0,4,2};
ref3=new int[]{4,8,8,8};
assert twe.fold(true,true)>0 : "Down doesn't move when it should.";
assert Arrays.equals(twe.board[0],ref1) : "First row invalid on down.";
assert Arrays.equals(twe.board[1],ref1) : "Second row invalid on down.";
assert Arrays.equals(twe.board[2],ref2) : "Third row invalid on down.";
assert Arrays.equals(twe.board[3],ref3) : "Fourth row invalid on down.";
assert twe.fold(true,true)==0 : "Down moves when it shouldn't.";
System.out.println("4: Moves and merges C");
twe.board=new int[][]{{2, 4, 4, 0},
{0, 4, 4, 0},
{2, 4, 0, 2},
{2, 0, 2, 4}};
ref1=new int[]{0,0,0,0};
ref2=new int[]{2,4,8,2};
ref3=new int[]{4,8,2,4};
assert twe.fold(true,true)>0 : "Down doesn't move when it should.";
assert Arrays.equals(twe.board[0],ref1) : "First row invalid on down.";
assert Arrays.equals(twe.board[1],ref1) : "Second row invalid on down.";
assert Arrays.equals(twe.board[2],ref2) : "Third row invalid on down.";
assert Arrays.equals(twe.board[3],ref3) : "Fourth row invalid on down.";
assert twe.fold(true,true)==0 : "Down moves when it shouldn't.";
System.out.println("5: Moves and merges D");
twe.board=new int[][]{{2, 0, 2, 0},
{0, 2, 4, 2},
{4, 4, 0, 0},
{2, 2, 2, 8}};
ref1=new int[]{0,0,0,0};
ref2=new int[]{2,2,2,0};
ref3=new int[]{4,4,4,2};
ref4=new int[]{2,2,2,8};
assert twe.fold(true,true)>0 : "Down doesn't move when it should.";
assert Arrays.equals(twe.board[0],ref1) : "First row invalid on down.";
assert Arrays.equals(twe.board[1],ref2) : "Second row invalid on down.";
assert Arrays.equals(twe.board[2],ref3) : "Third row invalid on down.";
assert Arrays.equals(twe.board[3],ref4) : "Fourth row invalid on down.";
assert twe.fold(true,true)==0 : "Down moves when it shouldn't.";
System.out.println("6: Moves and merges E");
twe.board=new int[][]{{2, 2, 4, 4},
{2, 4, 4, 8},
{8, 4, 2, 2},
{2, 2, 2, 2}};
ref1=new int[]{0,0,0,0};
ref2=new int[]{4,2,0,4};
ref3=new int[]{8,8,8,8};
ref4=new int[]{2,2,4,4};
assert twe.fold(true,true)>0 : "Down doesn't move when it should.";
assert Arrays.equals(twe.board[0],ref1) : "First row invalid on down.";
assert Arrays.equals(twe.board[1],ref2) : "Second row invalid on down.";
assert Arrays.equals(twe.board[2],ref3) : "Third row invalid on down.";
assert Arrays.equals(twe.board[3],ref4) : "Fourth row invalid on down.";
assert twe.fold(true,true)==0 : "Down moves when it shouldn't.";
System.out.println("7: Moves A");
twe.board=new int[][]{{2, 0, 2, 2},
{4, 2, 0, 0},
{0, 4, 4, 0},
{0, 0, 0, 4}};
ref1=new int[]{0,0,0,0};
ref2=new int[]{0,0,0,0};
ref3=new int[]{2,2,2,2};
ref4=new int[]{4,4,4,4};
assert twe.fold(true,true)>0 : "Down doesn't move when it should.";
assert Arrays.equals(twe.board[0],ref1) : "First row invalid on down.";
assert Arrays.equals(twe.board[1],ref2) : "Second row invalid on down.";
assert Arrays.equals(twe.board[2],ref3) : "Third row invalid on down.";
assert Arrays.equals(twe.board[3],ref4) : "Fourth row invalid on down.";
assert twe.fold(true,true)==0 : "Down moves when it shouldn't.";
System.out.println("8: Moves B");
twe.board=new int[][]{{2, 0, 2, 2},
{4, 2, 0, 4},
{8, 4, 4, 0},
{0, 8, 8, 4}};
ref1=new int[]{0,0,0,0};
ref2=new int[]{2,2,2,0};
ref3=new int[]{4,4,4,2};
ref4=new int[]{8,8,8,8};
assert twe.fold(true,true)>0 : "Down doesn't move when it should.";
assert Arrays.equals(twe.board[0],ref1) : "First row invalid on down.";
assert Arrays.equals(twe.board[1],ref2) : "Second row invalid on down.";
assert Arrays.equals(twe.board[2],ref3) : "Third row invalid on down.";
assert Arrays.equals(twe.board[3],ref4) : "Fourth row invalid on down.";
assert twe.fold(true,true)==0 : "Down moves when it shouldn't.";
System.out.println("9: First order merges and moves A");
twe.board=new int[][]{{2, 2, 2, 2},
{2, 0, 2, 4},
{4, 2, 0, 0},
{0, 4, 4, 4}};
ref1=new int[]{0,0,0,0};
ref2=new int[]{0,0,0,0};
ref3=new int[]{4,4,4,2};
ref4=new int[]{4,4,4,8};
assert twe.fold(true,true)>0 : "Down doesn't move when it should.";
assert Arrays.equals(twe.board[0],ref1) : "First row invalid on down.";
assert Arrays.equals(twe.board[1],ref2) : "Second row invalid on down.";
assert Arrays.equals(twe.board[2],ref3) : "Third row invalid on down.";
assert Arrays.equals(twe.board[3],ref4) : "Fourth row invalid on down.";
assert twe.fold(true,true)>0 : "Down doesn't move when it should.";
System.out.println("10: First order merges and moves B");
twe.board=new int[][]{{2, 2, 2, 2},
{2, 4, 4, 2},
{2, 8, 2, 4},
{2,16, 2, 2}};
ref1=new int[]{0,2,0,0};
ref2=new int[]{0,4,2,4};
ref3=new int[]{4,8,4,4};
ref4=new int[]{4,16,4,2};
assert twe.fold(true,true)>0 : "Down doesn't move when it should.";
assert Arrays.equals(twe.board[0],ref1) : "First row invalid on down.";
assert Arrays.equals(twe.board[1],ref2) : "Second row invalid on down.";
assert Arrays.equals(twe.board[2],ref3) : "Third row invalid on down.";
assert Arrays.equals(twe.board[3],ref4) : "Fourth row invalid on down.";
assert twe.fold(true,true)>0 : "Down doesn't move when it should.";
System.out.println("11: Static test");
twe.board=new int[][]{{2, 4, 4, 8},
{4, 2, 8, 4},
{2, 4, 2, 2},
{8, 2, 4, 8}};
ref1=new int[]{2,4,4,8};
ref2=new int[]{4,2,8,4};
ref3=new int[]{2,4,2,2};
ref4=new int[]{8,2,4,8};
assert twe.fold(true,true)==0 : "Down moves when it shouldn't.";
assert Arrays.equals(twe.board[0],ref1) : "First row invalid on down.";
assert Arrays.equals(twe.board[1],ref2) : "Second row invalid on down.";
assert Arrays.equals(twe.board[2],ref3) : "Third row invalid on down.";
assert Arrays.equals(twe.board[3],ref4) : "Fourth row invalid on down.";
}
public static void main(String[] a){
// validity tests
if (a.length > 0) {
Twe twe = new Twe();
testDown(twe);
//testUp(twe);
//testLeft(twe);
//testRight(twe);
}
(new Twe()).start();
}
int[][] board=new int[4][4];
void start(){
int x;
placeTwo();
do{
drawBoard();
resolve();
placeTwo();
}while((x=notDone())>0);
drawBoard();
wrapup(x);
}
int hasFree(){
for(int[]y:board)
for(int x:y)
if(x<2)return 1;
return 0;
}
int notDone(){
int x,y;
for(y=0;y<4;y++){
for(x=0;x<4;){
if(x<3&&board[y][x]==board[y][x+1]||
y<3&&board[y][x]==board[y+1][x])return 1;
if(board[y][x++]>2047)return -1;
}
}
return hasFree();
}
void wrapup(int k){
if(k<0){
chalk("you win",true);
}else{
chalk("you lose",true);
}
}
void resolve(){
do{
switch((new Scanner(System.in)).nextLine().charAt(0)){
case 'u':
if (fold(false,true)>0)return;
break;
case 'd':
if (fold(true, true)>0)return;
break;
case 'l':
if (fold(false,false)>0)return;
break;
case 'r':
if (fold(true,false)>0)return;
break;
case 'z':
board[0][0]=2048; // instant win;
return;
}
} while(true);
}
// false,true = up
// true, true = down
// false,false = left
// true, false = right
int fold(boolean inv, boolean vert){
int didMove=0;
int x,y,q,r,v,n;
for(int i=0;i<4;i++){
n=inv?3:0;
for(int j=1;j<4;){
v=vert?i:j;
x=inv?3-v:v;
v=vert?j:i;
y=inv?3-v:v;
q=vert?x:n;
r=vert?n:y;
if(board[y][x]==0||n==(vert?y:x))j++;
else if(board[r][q]==0){
board[r][q]=board[y][x];
board[y][x]=0;
j++;
didMove++;
}else if(board[r][q]==board[y][x]){
board[r][q]*=2;
board[y][x]=0;
n+=inv?-1:1;
didMove++;
j++;
}else n+=inv?-1:1;
}
}
return didMove;
}
int vec(){
return (new Random()).nextInt(4);
}
void placeTwo(){
if (hasFree()<1) return;
int x,y;
do{
x=vec();y=vec();
}while(board[x][y]>0);
board[x][y]=2;
}
void chalk(String a, boolean nl){
System.out.print(a+(nl?"\n":""));
}
String fill(char node, char mid){
String str = ""+node;
for(int i=0;i<4;i++){
for(int j=0;j<4;j++)
str+=mid;
str+=node;
}
return str;
}
void drawBoard(){
chalk(fill('+','-'),true);
String p[] = new String[5];
for(int[]y:board){
p[0]=p[1]=p[3]=fill('|',' ');
p[2]="";
for(int x=0;x<4;){
p[2]+=adjust(y[x++]);
}
p[2]+="|";
p[4]=fill('+','-');
for (String q:p){
chalk(q,true);
}
}
}
String adjust(int a){
return String.format("|%4d",a);
}
}