-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path20481.c
More file actions
515 lines (454 loc) · 12.7 KB
/
20481.c
File metadata and controls
515 lines (454 loc) · 12.7 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
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
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
// add your header comment here
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <string.h>
#include <time.h>
// put any extra includes here, but don't delete the ones above
#define SIZE 4
// add your function_prototypes here
// The functions moveLeft, moveRight, moveUp, moveDown
// return -1 if the specified moving of numbers is not possible.
// Otherwise they move the numbers as indicated and return
// the change to the score from combining adjacent identical numbers.
// They return 0 if no numbers were combined.
/*
题目的意思是这样的,需要‘严格’判断能否移动,我用flag标示移动的时候在第二个判断的时候出错了!
拿下移为例,if(cell==0)的时候直接判断可以移动是错误的,当下移的时候,如果只有
上面第一行的某一个元素为0,下面所有行不出现合并的情况下是不能下移的。
所以在上下左右移动的过程中,当if(cell==0)即当前元素为0时要使得能够移动,就必须限制这个为0的
元素不是上面第一行(下移时)、左侧第一列(右移)、右侧第一列(左移)、下面第一行(上移)、
所以修改每个move函数中判断if(cell==0)代码的i或者j的循环条件,以避开判断时0所在的特殊行位置
具体修改看下面部分注释(修改的只有每个move函数中第二个for循环中的i或者j的循环条件)
*/
int moveLeft(int board[SIZE][SIZE]) {
int i,j,score=0,flag=-1;
for(i=0;i<SIZE;i++)
{
for(j=0;j<SIZE;j++)
{
int cell=board[i][j];//cell单词用的不太恰当,表示当前元素,你可以采用更有意义的命名
if(cell!=0)
{
int k=j+1;
while(k<SIZE)
{
int nextcell=board[i][k];
if(nextcell!=0)
{
if(cell==nextcell)
{
flag=0;//相邻两个元素相同,就说明能移动,所以改变flag的值
board[i][j]+=board[i][k];
score+=board[i][j];
board[i][k]=0;
}
k=SIZE;
break;
}
k++;
}
}
}
}
//修改部分:for循环中的i或者j的循环条件
for(i=0;i<SIZE;i++)
{
for(j=0;j<SIZE-1;j++)
{
int cell=board[i][j];
if(cell==0)
{
int k=j+1;
while(k<SIZE)
{
int nextcell=board[i][k];
if(nextcell!=0)
{
flag=0;//
board[i][j]=nextcell;
board[i][k]=0;
k=SIZE;
}
k++;
}
}
}
}
if(flag!=-1)
return score;
else
return -1;
}
int moveRight(int board[SIZE][SIZE]) {
int i,j,score=0,flag=-1;
for(i=0;i<SIZE;i++)
{
for(j=SIZE-1;j>=0;j--)
{
int cell=board[i][j];
if(cell!=0)
{
int k=j-1;
while(k>=0)
{
int nextcell=board[i][k];
if(nextcell!=0)
{
if(cell==nextcell)
{
flag=0;
board[i][j]+=board[i][k];
score+=board[i][j];
board[i][k]=0;
}
k=-1;
break;
}
k--;
}
}
}
}
//修改部分:for循环中的i或者j的循环条件
for(i=0;i<SIZE;i++)
{
for(j=SIZE-1;j>0;j--)
{
int cell=board[i][j];
if(cell==0)
{
int k=j-1;
while(k>=0)
{
int nextcell=board[i][k];
if(nextcell!=0)
{
flag=0;//当前元素为0,说明能移动,改变flag的值
board[i][j]=nextcell;
board[i][k]=0;
k=-1;
}
k--;
}
}
}
}
if(flag!=-1)
return score;
else
return -1;
}
int moveDown(int board[SIZE][SIZE]) {
int i,j,score=0,flag=-1;
for(i=SIZE-1;i>=0;i--)
{
for(j=0;j<SIZE;j++)
{
int cell=board[i][j];
if(cell!=0)
{
int k=i-1;
while(k>=0)
{
int nextcell=board[k][j];
if(nextcell!=0)
{
if(board[i][j]==board[k][j])
{
flag=0;
board[i][j]+=board[k][j];
score+=board[i][j];
board[k][j]=0;
}
k=0;
break;
}
k--;
}
}
}
}
//修改部分:for循环中的i或者j的循环条件
for(i=SIZE-1;i>0;i--)
{
for(j=0;j<SIZE;j++)
{
int cell=board[i][j];
if(cell==0)
{
int k=i-1;
while(k>=0)
{
int nextcell=board[k][j];
if(nextcell!=0)
{
flag=0;
board[i][j]=nextcell;
board[k][j]=0;
k=0;
}
k--;
}
}
}
}
if(flag!=-1)
return score;
else
return -1;
}
int moveUp(int board[SIZE][SIZE]) {
int i,j,score=0,flag=-1;
for(i=0;i<SIZE;i++)
{
for(j=0;j<SIZE;j++)
{
int cell=board[i][j];
if(cell!=0)
{
int k=i+1;
while(k<SIZE)
{
int nextcell=board[k][j];
if(nextcell!=0)
{
if(cell==nextcell)
{
flag=0;
board[i][j]+=board[k][j];
score+=board[i][j];
board[k][j]=0;
}
k=SIZE;
break;
}
k++;
}
}
}
}
//修改部分:for循环中的i或者j的循环条件
for(i=0;i<SIZE-1;i++)
{
for(j=0;j<SIZE;j++)
{
int cell=board[i][j];
if(cell==0)
{
int k=i+1;
while(k<SIZE)
{
int nextcell=board[k][j];
if(nextcell!=0)
{
flag=0;
board[i][j]=nextcell;
board[k][j]=0;
k=SIZE;
}
k++;
}
}
}
}
if(flag!=-1)
return score;
else
return -1;
}
// gameOver returns 0 iff it is possible to make a move on the board
// It returns 1 otherwise.
int gameOver(int board[SIZE][SIZE]) {
int copy_board[SIZE][SIZE],i,j;
/*为了避免直接把board[][]传进move函数判断的时候改变board,所以把board复制给
另一个数组,然后判断,这样就不会改变board数组了
*/
for(i=0;i<SIZE;i++)
{
for(j=0;j<SIZE;j++)
{
copy_board[i][j]=board[i][j];
}
}
if(moveDown(copy_board)==-1&&moveUp(copy_board)==-1&&moveLeft(copy_board)==-1&&moveRight(copy_board)==-1)//如果四个移动函数都返回-1即不能移动GameOver
return 1;
else
return 0;
}
// boardContains2048 returns 1 iff the board contains 2048.
// It returns 0 otherwise.
int boardContains2048(int board[SIZE][SIZE]) {
int i,j;
for(i=0;i<SIZE;i++)
{
for(j=0;j<SIZE;j++)
{
if(board[i][j]==2048)
return 1;
}
}
return 0;
}
// printBoard displays the board.
void printBoard(int board[SIZE][SIZE]) {
int i,j;
printf("+");
for(j=0;j<SIZE;j++)
{
printf("-----");
}
printf("+\n");
for(i=0;i<SIZE;i++)
{
printf("|");
for(j=0;j<SIZE;j++)
{
if(board[i][j]==0)
{
char a='.';
printf("%5c",a);
}
else
{
printf("%5d",board[i][j]);
}
}
printf("|\n\n");
}
printf("+");
for(j=0;j<SIZE;j++)
{
printf("-----");
}
printf("+\n");
}
// readBoard attempts to read SIZE*SIZE integers into array board
// it returns how many integers actually were read
int readBoard(int board[SIZE][SIZE]) {
int i,j;
for(i=0;i<SIZE;i++)
{
for(j=0;j<SIZE;j++)
scanf("%d",&board[i][j]);
}
return i*j;
}
//
// add your functions here
//
//
// You do not need to modify the code below here.
//
// If you wish to modify the code below, you have
// misunderstood the assignment specification
//
void printHelp(void);
void insertNewNumber(int board[SIZE][SIZE]);
int main(int argc, char *argv[]) {
int board[SIZE][SIZE] = {{0}};
int c, score, moveScore, gameWon, numbersRead;
unsigned int seed;
// initialize random generator with command-line argument if provided
// or with current time
if (argc > 1){
seed = atoi(argv[1]);
} else {
seed = time(0);
}
srand(seed);
printf("Enter %d numbers making up initial board:\n", SIZE * SIZE);
numbersRead = readBoard(board);
if (numbersRead != SIZE * SIZE) {
printf("Warning readBoard read only %d numbers\n", numbersRead);
}
printf("Repeat game by running: %s %u\n", argv[0], seed);
printHelp();
score = 0;
gameWon = 0;
while (gameOver(board) == 0) {
printf("\n");
printBoard(board);
printf("Your score is %d.\n", score);
if (gameWon == 0 && boardContains2048(board)) {
gameWon = 1;
printf("Congratulations you've won the game - q to quit or you can keep going\n");
}
printf("> ");
c = getchar();
while (c != EOF && isspace(c)) {
c = getchar();
}
printf("\n");
if (c == EOF || c == 'q' || c == 'Q') {
printf("Good bye - your final score was %d.\n", score);
return 0;
}
c = tolower(c);
if (!strchr("hjklaswd", c)) {
printHelp();
} else {
moveScore = 0;
if (c == 'h' || c == 'a') {
moveScore = moveLeft(board);
} else if (c == 'j' || c == 's') {
moveScore = moveDown(board);
} else if (c == 'k' || c == 'w') {
moveScore = moveUp(board);
} else if (c == 'l' || c == 'd') {
moveScore = moveRight(board);
}
if (moveScore == -1) {
printf("%c is not a legal move in the current position.\n", c);
} else {
insertNewNumber(board);
score = score + moveScore;
}
}
}
printBoard(board);
printf("Game over - your final score was %d.\n", score);
return 0;
}
// print a help message
// do not change this function
void
printHelp(void) {
printf("Enter h or a for left, j or s for down, k or w for up, l or d for right, q to quit\n");
}
// add a new number to the board
// it will either be a 2 (90% probability) or a 4 (10% probability)
// do not change this function
void insertNewNumber(int board[SIZE][SIZE]) {
int row, column;
int index, availableSquares = 0;
// count vacant squares
for (row = 0; row < SIZE; row = row + 1) {
for (column = 0; column < SIZE; column = column + 1) {
if (board[row][column] == 0) {
availableSquares = availableSquares + 1;
}
}
}
if (availableSquares == 0) {
printf("Internal error no available square\n");
exit(1);
}
// randomly pick a vacant square
index = rand() % availableSquares;
for (row = 0; row < SIZE; row = row + 1) {
for (column = 0; column < SIZE; column = column + 1) {
if (board[row][column] == 0) {
if (index == 0) {
if (rand() % 10 == 0) {
board[row][column] = 4;
} else {
board[row][column] = 2;
}
return;
}
index = index - 1;
}
}
}
}