-
Notifications
You must be signed in to change notification settings - Fork 0
/
Game.cpp
844 lines (656 loc) · 15.5 KB
/
Game.cpp
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
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
#include "Game.h"
#include<iostream>
#include<fstream>
#include<istream>
#include<string>
#include<cstdlib>
#include<ctype.h> //大小写转换,充分考虑用户大小写输入情况
#include<ctime>
#include<graphics.h>
#include<cmath>
#include<conio.h> //可使用_getch,_kbhit
using namespace std;
/*---------------------------Game------------------------------*/
Game::Game()
{
for (int i = 0; i < ROW; i++) //初始化数组
{
for (int j = 0; j < COL; j++)
{
board[i][j] = 0;
lastBoard[0][i][j] = 0;
lastBoard[1][i][j] = 0;
lastScore[0] = 0;
lastScore[1] = 0;
}
}
score = 0;
}
Game::~Game()
{
}
void Game::printGrid(int row, int col, int number)
{
RECT r = {
GRID_PADDING*col + GRID_SIZE * (col - 1),
GRID_PADDING*(row+1) + GRID_SIZE * row, //左上角坐标
GRID_PADDING*col + GRID_SIZE * col,
GRID_PADDING*(row+1) + GRID_SIZE * (row+1), //右下角坐标
};
/*设置格子颜色*/
int index; //作为color下标
if (number == 0)
{
index = 0;
}
else
{
index = log(number) / log(2); //将接收的数字转化为0-15的数字,作为color下标
}
setfillcolor(color[index]); //填充颜色
/*画格子*/
solidroundrect( //画格子
GRID_PADDING*col + GRID_SIZE * (col - 1),
GRID_PADDING*(row+1) + GRID_SIZE * row, //左上角坐标
GRID_PADDING*col + GRID_SIZE * col,
GRID_PADDING*(row+1) + GRID_SIZE * (row+1), //右下角坐标
10, 10);
/*填数字*/
TCHAR num[10];
sprintf_s(num, sizeof(num), _T("%d"), number); //将数字转为字符串类型存入num
if (number != 0)
{
if (number != 4)
settextcolor(WHITE);
else //由于4对应浅色背景,如果为4,将字体颜色设为黑色
settextcolor(BLACK);
setbkmode(TRANSPARENT); //文字背景透明(与底色相同)
settextstyle(50, 0, _T("Arial"), 0, 0, 50, false, false, false, ANSI_CHARSET, OUT_DEFAULT_PRECIS,
CLIP_DEFAULT_PRECIS, ANTIALIASED_QUALITY, DEFAULT_PITCH);
drawtext(num, &r, DT_CENTER | DT_VCENTER | DT_SINGLELINE); //向图形里填充数字
}
}
void Game::printBoard()
{
/*system("cls");
cout << "游戏说明:w,a,s,d控制上下左右方向" << endl;
cout << "x用于撤销一次操作(最多只能退回一步)" << endl;
for (int i = 0; i < ROW; i++)
{
cout << "---------------------------------" << endl;
for (int j = 0; j < COL; j++)
{
if (board[i][j] == 0) //数组元素为0代表空
{
cout << "| \t";
}
else
{
cout << "| " << board[i][j] << "\t";
}
}
cout << "|" << endl;
}
cout << "---------------------------------" << endl;
*/
cleardevice();
for (int i = 1; i <= ROW; i++)
{
for (int j = 1; j <= COL; j++)
{
printGrid(i, j, board[i - 1][j - 1]);
}
}
/*显示当前分数*/
RECT r = {
GRID_PADDING,
GRID_PADDING, //左上角坐标
GRID_PADDING*2 + GRID_SIZE * 2,
GRID_PADDING + GRID_SIZE , //右下角坐标
};
setfillcolor(BLUE);
solidroundrect (
GRID_PADDING,
GRID_PADDING, //左上角坐标
GRID_PADDING*2 + GRID_SIZE*2,
GRID_PADDING + GRID_SIZE , //右下角坐标
10,10);
TCHAR sc[10];
sprintf_s(sc, sizeof(sc), _T("%d"), score); //类型转换
settextcolor(WHITE);
setbkmode(TRANSPARENT);
settextstyle(50, 0, _T("Arial"), 0, 0, 50, false, false, false, ANSI_CHARSET, OUT_DEFAULT_PRECIS,
CLIP_DEFAULT_PRECIS, ANTIALIASED_QUALITY, DEFAULT_PITCH);
drawtext("Your score:", &r, DT_TOP); //填充提示信息
drawtext(sc, &r, DT_SINGLELINE|DT_BOTTOM| DT_CENTER); //向图形里填充数字
}
int Game::getMaxScore()
{
int score = 0;
FILE *fp;
errno_t err; //安全函数必须
err = fopen_s(&fp, "scoreFile.dat", "r");
if (err) //如果文件没有被创建
{
err = fopen_s(&fp, "scoreFile.dat", "w");
fprintf_s(fp, "%d", score, sizeof(int)); //就创建文件并将0写入文件
}
fscanf_s(fp, "%d", &score, sizeof(int)); //将文件内容读到内存
fclose(fp);
return score;
}
void Game::printMaxScore(int score)
{
RECT r = {
GRID_PADDING * 3 + GRID_SIZE * 2,
GRID_PADDING, //左上角坐标
GRID_PADDING * 4 + GRID_SIZE * 4,
GRID_PADDING + GRID_SIZE , //右下角坐标
};
setfillcolor(RED);
solidroundrect(
GRID_PADDING * 3 + GRID_SIZE * 2,
GRID_PADDING, //左上角坐标
GRID_PADDING * 4 + GRID_SIZE * 4,
GRID_PADDING + GRID_SIZE, //右下角坐标
10, 10);
TCHAR sc[10];
sprintf_s(sc, sizeof(sc), _T("%d"), score); //类型转换
settextcolor(WHITE);
setbkmode(TRANSPARENT);
settextstyle(50, 0, _T("Arial"), 0, 0, 50, false, false, false, ANSI_CHARSET, OUT_DEFAULT_PRECIS,
CLIP_DEFAULT_PRECIS, ANTIALIASED_QUALITY, DEFAULT_PITCH);
drawtext("Best score:", &r, DT_TOP); //填充提示信息
drawtext(sc, &r, DT_SINGLELINE | DT_BOTTOM | DT_CENTER); //向图形里填充数字
}
void Game::generateNum()
{
bool isFull = true; //用于记录是否满格,起始值设为满格状态
for (int i = 0; i < ROW; i++)
{
for (int j = 0; j < COL; j++)
{
if (board[i][j] == 0)
isFull = false;
}
}
if (isFull == false) //如果没有满格,则生成随机数
{
srand((unsigned)time(NULL)); //时间作为种子值
int row;
int col;
do { //生成随机的行数row、列数col(同时保证该位置没有数字)
row = rand() % ROW;
col = rand() % COL;
} while (board[row][col] != 0);
int x = rand() % 10;
if (x == 0)
board[row][col] = 4; //十分之一的概率生成4
else
board[row][col] = 2; //十分之九的概率生成2
}
}
void Game::process(char direction)
{
switch (tolower(direction))
{
case'w': //上
for (int j = 0; j < COL; j++) //按列遍历
{
for (int times = 0; times < 3; times++) //最坏情况下,至少需要3次“靠边”操作
{
for (int i = 0; i < ROW - 1; i++) //该列数字全部向上靠
{
if (board[i][j] == 0)
{
board[i][j] = board[i + 1][j];
board[i + 1][j] = 0; //如果board[i][j]为0,将其下方的数字赋给它,同时下方的数字变为0
}
}
}
for (int i = 0; i < ROW - 1; i++) //相同数字合并
{
if (board[i][j] == board[i + 1][j])
{
board[i][j] *= 2;
score+=board[i][j]; //加分
board[i + 1][j] = 0;
}
}
for (int i = 0; i < ROW - 1; i++) //该列数字全部向上靠
{
if (board[i][j] == 0)
{
board[i][j] = board[i + 1][j];
board[i + 1][j] = 0;
}
}
}
break;
case's': //下
for (int j = 0; j < COL; j++)
{
for (int times = 0; times < 3; times++)
{
for (int i = ROW - 1; i > 0; i--) //该列数字全部向下靠
{
if (board[i][j] == 0)
{
board[i][j] = board[i - 1][j];
board[i - 1][j] = 0; //如果board[i][j]为0,将其上方的数字赋给它,同时上方的数字变为0
}
}
}
for (int i = ROW - 1; i > 0; i--) //合并
{
if (board[i][j] == board[i - 1][j])
{
board[i][j] *= 2;
score+=board[i][j]; //加分
board[i - 1][j] = 0;
}
}
for (int i = ROW - 1; i > 0; i--) //该列数字全部向下靠
{
if (board[i][j] == 0)
{
board[i][j] = board[i - 1][j];
board[i - 1][j] = 0;
}
}
}
break;
case'a': //左
for (int i = 0; i < ROW; i++)
{
for (int times = 0; times < 3; times++)
{
for (int j = 0; j < COL - 1; j++) //每行数字向左靠
{
if (board[i][j] == 0)
{
board[i][j] = board[i][j + 1];
board[i][j + 1] = 0; //如果board[i][j]为0,将其右边的数字赋给它,同时右边的数字变为0
}
}
}
for (int j = 0; j < COL - 1; j++) //合并
{
if (board[i][j] == board[i][j + 1])
{
board[i][j] *= 2;
score+=board[i][j]; //加分
board[i][j + 1] = 0;
}
}
for (int j = 0; j < COL - 1; j++) //每行数字向左靠
{
if (board[i][j] == 0)
{
board[i][j] = board[i][j + 1];
board[i][j + 1] = 0;
}
}
}
break;
case'd': //右
for (int i = 0; i < ROW; i++)
{
for (int times = 0; times < 3; times++)
{
for (int j = COL - 1; j > 0; j--) //每行数字向右靠
{
if (board[i][j] == 0)
{
board[i][j] = board[i][j - 1];
board[i][j - 1] = 0; //如果board[i][j]为0,将其左边的数字赋给它,同时左边的数字变为0
}
}
}
for (int j = COL - 1; j > 0; j--) //合并
{
if (board[i][j] == board[i][j - 1])
{
board[i][j] *= 2;
score+=board[i][j]; //加分
board[i][j - 1] = 0;
}
}
for (int j = COL - 1; j > 0; j--) //每行数字向右靠
{
if (board[i][j] == 0)
{
board[i][j] = board[i][j - 1];
board[i][j - 1] = 0;
}
}
}
break;
case'x': //撤销
/*对棋盘的操作*/
for (int i = 0; i < ROW; i++)
{
for (int j = 0; j < COL; j++)
{
board[i][j] = lastBoard[0][i][j];
}
}
/*对分数的操作*/
score = lastScore[0];
break;
default:
; //其他情况下不做任何事情
}
}
void Game::saveStage(char direction)
{
static int score_1; //用于保存score[0],主要用于撤销
static int board_1[4][4]; //用于保存lastBoard[0],主要用于撤销
//基于普通方向键保存操作,lastBoard[0]存放上一次状态,[1]存放当前状态
if (tolower(direction) == 'w' || tolower(direction) == 'a'
|| tolower(direction) == 's' || tolower(direction) == 'd')
{
/*对棋盘的操作*/
for (int i = 0; i < ROW; i++)
{
for (int j = 0; j < COL; j++)
{
board_1[i][j] = lastBoard[0][i][j];
lastBoard[0][i][j] = lastBoard[1][i][j];
lastBoard[1][i][j] = board[i][j];
}
}
/*对分数的操作*/
score_1 = lastScore[0];
lastScore[0] = lastScore[1];
lastScore[1] = score;
}
//按下x为撤销,此为基于撤销的保存操作
else if (tolower(direction) == 'x')
{
/*对棋盘的操作*/
for (int i = 0; i < ROW; i++)
{
for (int j = 0; j < COL; j++)
{
lastBoard[1][i][j] = lastBoard[0][i][j];
lastBoard[0][i][j] = board_1[i][j];
}
}
/*对分数的操作*/
lastScore[1] = lastScore[0];
lastScore[0] = score_1;
}
else; //否则不做任何事情
}
int Game::judge()
{
for (int i = 0; i < ROW; i++) //游戏胜利判断
{
for (int j = 0; j < COL; j++)
{
if (board[i][j] == 2048)
return WIN;
}
}
for (int i = 0; i < ROW; i++) //横向检查
{
for (int j = 0; j < COL - 1; j++)
{
if (board[i][j] == 0 || board[i][j] == board[i][j + 1]) //横向相邻项相等或某一项为空,游戏继续
return CONTINUE;
}
}
for (int j = 0; j < COL; j++) //纵向检查
{
for (int i = 0; i < ROW - 1; i++)
{
if (board[i][j] == 0 || board[i][j] == board[i + 1][j]) //纵向相邻项相等或某一项为空,游戏继续
return CONTINUE;
}
}
return LOSE; //其它情况游戏结束
}
void Game::saveScore()
{
FILE *fp;
errno_t err;
//安全函数必须
int bestScore;
err = fopen_s(&fp,"scoreFile.dat","r");
fscanf_s(fp, "%d", &bestScore, sizeof(int)); //取出文件中的分数存入bestScore
fclose(fp);
if (score > bestScore)
{
err = fopen_s(&fp, "scoreFile.dat", "w");
fprintf_s(fp, "%d", score, sizeof(int)); //将当前分数存入文件中
fclose(fp);
}
}
/*--------------------------AdvancedGame-------------------------------*/
AdvancedGame::AdvancedGame()
{
for (int i = 0; i < ROW; i++) //初始化数组
{
for (int j = 0; j < COL; j++)
{
board[i][j] = 0;
}
}
score = 0;
}
AdvancedGame::~AdvancedGame()
{
}
void AdvancedGame::process(char direction)
{
switch (tolower(direction))
{
case'w': //上
for (int j = 0; j < COL; j++) //按列遍历
{
for (int times = 0; times < 3; times++) //最坏情况下,至少需要3次“靠边”操作
{
for (int i = 0; i < ROW - 1; i++) //该列数字全部向上靠
{
if (board[i][j] == 0)
{
board[i][j] = board[i + 1][j];
board[i + 1][j] = 0; //如果board[i][j]为0,将其下方的数字赋给它,同时下方的数字变为0
}
}
}
for (int i = 0; i < ROW - 1; i++) //相同数字合并
{
if (board[i][j] == board[i + 1][j])
{
board[i][j] *= 2;
score += board[i][j]; //加分
board[i + 1][j] = 0;
}
}
for (int i = 0; i < ROW - 1; i++) //该列数字全部向上靠
{
if (board[i][j] == 0)
{
board[i][j] = board[i + 1][j];
board[i + 1][j] = 0;
}
}
}
break;
case's': //下
for (int j = 0; j < COL; j++)
{
for (int times = 0; times < 3; times++)
{
for (int i = ROW - 1; i > 0; i--) //该列数字全部向下靠
{
if (board[i][j] == 0)
{
board[i][j] = board[i - 1][j];
board[i - 1][j] = 0; //如果board[i][j]为0,将其上方的数字赋给它,同时上方的数字变为0
}
}
}
for (int i = ROW - 1; i > 0; i--) //合并
{
if (board[i][j] == board[i - 1][j])
{
board[i][j] *= 2;
score += board[i][j]; //加分
board[i - 1][j] = 0;
}
}
for (int i = ROW - 1; i > 0; i--) //该列数字全部向下靠
{
if (board[i][j] == 0)
{
board[i][j] = board[i - 1][j];
board[i - 1][j] = 0;
}
}
}
break;
case'a': //左
for (int i = 0; i < ROW; i++)
{
for (int times = 0; times < 3; times++)
{
for (int j = 0; j < COL - 1; j++) //每行数字向左靠
{
if (board[i][j] == 0)
{
board[i][j] = board[i][j + 1];
board[i][j + 1] = 0; //如果board[i][j]为0,将其右边的数字赋给它,同时右边的数字变为0
}
}
}
for (int j = 0; j < COL - 1; j++) //合并
{
if (board[i][j] == board[i][j + 1])
{
board[i][j] *= 2;
score += board[i][j]; //加分
board[i][j + 1] = 0;
}
}
for (int j = 0; j < COL - 1; j++) //每行数字向左靠
{
if (board[i][j] == 0)
{
board[i][j] = board[i][j + 1];
board[i][j + 1] = 0;
}
}
}
break;
case'd': //右
for (int i = 0; i < ROW; i++)
{
for (int times = 0; times < 3; times++)
{
for (int j = COL - 1; j > 0; j--) //每行数字向右靠
{
if (board[i][j] == 0)
{
board[i][j] = board[i][j - 1];
board[i][j - 1] = 0; //如果board[i][j]为0,将其左边的数字赋给它,同时左边的数字变为0
}
}
}
for (int j = COL - 1; j > 0; j--) //合并
{
if (board[i][j] == board[i][j - 1])
{
board[i][j] *= 2;
score += board[i][j]; //加分
board[i][j - 1] = 0;
}
}
for (int j = COL - 1; j > 0; j--) //每行数字向右靠
{
if (board[i][j] == 0)
{
board[i][j] = board[i][j - 1];
board[i][j - 1] = 0;
}
}
}
break;
default:
; //否则不做任何事情
}
}
void AdvancedGame::printTime(int time)
{
RECT r = {
GRID_PADDING * 1 + GRID_SIZE,
GRID_PADDING* 6 + GRID_SIZE * 5, //左上角坐标
GRID_PADDING * 4 + GRID_SIZE * 3,
GRID_PADDING * 6 + GRID_SIZE * 6, //右下角坐标
};
setfillcolor(GREEN);
solidroundrect(
GRID_PADDING * 1 + GRID_SIZE,
GRID_PADDING * 6 + GRID_SIZE * 5, //左上角坐标
GRID_PADDING * 4 + GRID_SIZE * 3,
GRID_PADDING * 6 + GRID_SIZE * 6, //右下角坐标
10, 10);
TCHAR time1[10];
sprintf_s(time1, sizeof(time1), _T("%d"), time); //类型转换
settextcolor(WHITE);
setbkmode(TRANSPARENT);
settextstyle(50, 0, _T("Arial"), 0, 0, 50, false, false, false, ANSI_CHARSET, OUT_DEFAULT_PRECIS,
CLIP_DEFAULT_PRECIS, ANTIALIASED_QUALITY, DEFAULT_PITCH);
drawtext(time1, &r, DT_TOP | DT_CENTER);
drawtext("seconds left", &r, DT_SINGLELINE | DT_BOTTOM | DT_CENTER);
}
char AdvancedGame::timer(long startTime, int limit)
{
//limit为限定时间,在设计的最初版本中,limit只能选择3s,为了使Limit可变,故将其作为函数的第二个形参,可根据玩家的选择而发生变化
char direction;
for (int times = limit; times > 0; times--)
{
printTime(times);
while (clock() - startTime < (limit + 1 - times) * CLOCKS_PER_SEC)
{
if (_kbhit()) //如果有wasd输入,立即停止计时
{
direction = _getch();
if (tolower(direction) == 'w' || tolower(direction) == 'a'
|| tolower(direction) == 's' || tolower(direction) == 'd')
{
return direction;
}
}
}
}
/*printTime(2);
while (clock() - startTime < 2 * CLOCKS_PER_SEC)
{
if (_kbhit()) //如果有wasd输入,立即停止计时
{
direction = _getch();
if (tolower(direction) == 'w' || tolower(direction) == 'a'
|| tolower(direction) == 's' || tolower(direction) == 'd')
{
return direction;
}
}
}
printTime(1);
while (clock() - startTime < 3 * CLOCKS_PER_SEC)
{
if (_kbhit()) //如果有wasd输入,立即停止计时
{
direction = _getch();
if (tolower(direction) == 'w' || tolower(direction) == 'a'
|| tolower(direction) == 's' || tolower(direction) == 'd')
{
return direction;
}
}
}*/
printTime(0);
}