服务器之家:专注于VPS、云服务器配置技术及软件下载分享
分类导航

PHP教程|ASP.NET教程|Java教程|ASP教程|编程技术|正则表达式|C/C++|IOS|C#|Swift|Android|VB|R语言|JavaScript|易语言|vb.net|

服务器之家 - 编程语言 - C/C++ - 2048小游戏C语言实现代码

2048小游戏C语言实现代码

2021-06-24 13:18Little_Sword C/C++

这篇文章主要为大家详细介绍了2048小游戏C语言实现代码,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

本文实例为大家分享了C语言实现2048游戏的具体代码,供大家参考,具体内容如下

大一时学c语言写的,写的不好但当时感觉还行。

环境运行 vc6.0 ,cpp文件。

基本上是c写的,但是改变字体颜色,在控制台移动光标等等好像不是c中的。

2048小游戏C语言实现代码2048小游戏C语言实现代码

2048小游戏C语言实现代码

代码:

?
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
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
#include<time.h>
#include<stdlib.h>
#include<conio.h>
#include<stdio.h>
#include<windows.h>
#define x0 26
#define y0 1
HANDLE hOutput=GetStdHandle(STD_OUTPUT_HANDLE);
/*显示声明*/
void p(int*a,int i,int x,int y);
/*部分刷新声明*/
void shua(void);
/*胜利画面测试函数声明*/
int test(void);
/*字体空格声明*/
void kg(int*a);
/*画长条函数的声明*/
void ct(int x,int y,int l,int k,int c);
/*标题字幕2048*/
void p2048(int x,int y);
/*单个颜色返回值声明*/
int col(int*a);
/*移动光标*/
void gotoxy(int x,int y)
{
 COORD coordScreen={0,0};
 coordScreen.X=x;
 coordScreen.Y=y;
 SetConsoleCursorPosition(hOutput,coordScreen);
}
/*字体颜色*/
void textcolor(int color)
{
 SetConsoleTextAttribute(hOutput,color);
}
/*显示函数*/
void prin(int*a,int*score,int*scoremax)
{
 int i;
 for (i = 0; i < 16; i++)
 {
  p(a,i,x0,y0);
 }
 printf("\n\n       score=%d MAXscore=%d     \n", *score, *scoremax);
}
/* 产生随机数 */
int shu(void)
{
 int s = 0;
 s = 1 + (int)(12.0 * rand() / (RAND_MAX + 1.0));
 if (s == 12)
  return 4;
 else
  return 2;
}
 
/* 决定位置 */
int rands(void)
{
 return (int)(16.0 * rand() / (RAND_MAX + 1.0));
}
/* 主函数*/
int main(void)
{
 int flag = 0, c = 0, pd = 1, n = 0, i = 0, m = 0, j =
  0, sj, sj1, sj2, a[16] = { 0 }, b[16] ={0}, x = 0, score = 0, t = 0, jx = 0, scoremax = 0,ks=1,yx=0;
  char sr,an;
  //开始画面
  p2048(17,1);
  textcolor(255);
  gotoxy(35,15);
  for(;;)
  {
   an=getch();
  switch(an)
  {
  case 72:
   if(ks==1)
    ks=0;
   else
    ks=1;
  break;
  case 80:
   if(ks==1)
    ks=0;
   else
    ks=1;
  break;
  case '\r':
   yx=1;
   break;
  default:
  break;
  }
  if(ks==1)
   gotoxy(35,15);
  else
   gotoxy(34,18);
  if(ks==1&&yx==1)
   break;
  else if(ks==0&&yx==1)
  {
  textcolor(240);
  gotoxy(0,0);
  for(i=0;i<=500;i++)
  {
   printf("  ");
  }
  gotoxy(0,0);
  ct(12,5,58,10,223);
  gotoxy(13,6);
  printf("      2048游戏说明");
  gotoxy(13,8);
  printf(" 通过按数字键移动数字,合并相同的数字来的取得更大的数。");
  gotoxy(13,9);
  printf(" 得到2048时游戏即胜利,当然您也可以选择继续挑战。");
  gotoxy(13,11);
  printf(" 按键操作:");
  gotoxy(13,13);
  printf(" 上:↑ 下:↓ 左:← 右:→ 重新开始:r 退出游戏:e");
  textcolor(240);
  printf("\n\n\n\n\n               I know ");
  getch();
  p2048(17,1);
  textcolor(255);
  gotoxy(34,18);
  }
  yx=0;
  }
  //隐藏光标
  HANDLE hOut=GetStdHandle(STD_OUTPUT_HANDLE);
  CONSOLE_CURSOR_INFO cci;
  GetConsoleCursorInfo(hOut,&cci);
  cci.bVisible=FALSE;
  SetConsoleCursorInfo(hOut,&cci);
  //刷新画面
  textcolor(240);
  gotoxy(0,0);
  for(i=0;i<=500;i++)
  {
   printf("  ");
  }
  gotoxy(0,0);
  /*设置时间种子*/
  srand((int)time(0));
  /* 设置初始值 */
  sj = rands();
  sj1 = rands();
  sj2 = rands();
  for (;;)
  {
   if (sj == sj1 || sj == sj2 || sj1 == sj2)
   {
    sj1 = rands();
    sj2 = rands();
   }
   else
    break;
  }
  a[sj] = shu();
  a[sj1] = shu();
  a[sj2] = shu();
  /*显示*/
  prin(a,&score,&scoremax);
  for (;;)
  {
   /* 无回显输入 */
   sr = getch();
   /* scanf("%d",&n); *//* 记录之前的位置,将用以比较移动是否有效 */
   for (j = 0; j < 16; j++)
    b[j] = a[j];
   switch (sr)
   {
    /* 向上移动 */
   case 72:
    /* 全部移到上边 */
    for (j = 0; j < 4; j++)
    {
     for (c = 0; c < 3; c++)
     {
      if (a[j] == 0)
      {
       a[j] = a[j + 4];
       a[j + 4] = 0;
      }
      if (a[j + 4] == 0)
      {
       a[j + 4] = a[j + 8];
       a[j + 8] = 0;
      }
      if (a[j + 8] == 0)
      {
       a[j + 8] = a[j + 12];
       a[j + 12] = 0;
      }
     }
     /* 移到上面后的合并操作 */
     if (a[j] == a[j + 4])
     {
      a[j] = a[j] * 2;
      score = score + a[j];
      a[j + 4] = a[j + 8];
      a[j + 8] = a[j + 12];
      a[j + 12] = 0;
      if (a[j + 4] == a[j + 8])
      {
       a[j + 4] = 2 * a[j + 4];
       score = score + a[j + 4];
       a[j + 8] = 0;
      }
     }
     else if (a[j + 4] == a[j + 8])
     {
      a[j + 4] = 2 * a[j + 4];
      score = score + a[j + 4];
      a[j + 8] = a[j + 12];
      a[j + 12] = 0;
     }
     else if (a[j + 8] == a[j + 12])
     {
      a[j + 8] = 2 * a[j + 8];
      score = score + a[j + 8];
      a[j + 12] = 0;
     }
     else;
    }
    break;
    /* 向下移动 */
   case 80:
    /* 全部移动到下面 */
    for (j = 12; j < 16; j++)
    {
     for (c = 0; c < 3; c++)
     {
      if (a[j] == 0)
      {
       a[j] = a[j - 4];
       a[j - 4] = 0;
      }
      if (a[j - 4] == 0)
      {
       a[j - 4] = a[j - 8];
       a[j - 8] = 0;
      }
      if (a[j - 8] == 0)
      {
       a[j - 8] = a[j - 12];
       a[j - 12] = 0;
      }
     }
     /* 移到下面后的合并操作 */
     if (a[j] == a[j - 4])
     {
      a[j] = a[j] * 2;
      score = score + a[j];
      a[j - 4] = a[j - 8];
      a[j - 8] = a[j - 12];
      a[j - 12] = 0;
      if (a[j - 4] == a[j - 8])
      {
       a[j - 4] = 2 * a[j - 4];
       score = score + a[j - 4];
       a[j - 8] = 0;
      }
     }
     else if (a[j - 4] == a[j - 8])
     {
      a[j - 4] = 2 * a[j - 4];
      score = score + a[j - 4];
      a[j - 8] = a[j - 12];
      a[j - 12] = 0;
     }
     else if (a[j - 8] == a[j - 12])
     {
      a[j - 8] = 2 * a[j - 8];
      score = score + a[j - 8];
      a[j - 12] = 0;
     }
     else;
    }
    break;
    /* 向左移动 */
   case 75:
    /* 全部移动到左面 */
    for (j = 0; j <= 12; j = j + 4)
    {
     for (c = 0; c < 3; c++)
     {
      if (a[j] == 0)
      {
       a[j] = a[j + 1];
       a[j + 1] = 0;
      }
      if (a[j + 1] == 0)
      {
       a[j + 1] = a[j + 2];
       a[j + 2] = 0;
      }
      if (a[j + 2] == 0)
      {
       a[j + 2] = a[j + 3];
       a[j + 3] = 0;
      }
     }
     /* 移到左面后的合并操作 */
     if (a[j] == a[j + 1])
     {
      a[j] = a[j] * 2;
      score = score + a[j];
      a[j + 1] = a[j + 2];
      a[j + 2] = a[j + 3];
      a[j + 3] = 0;
      if (a[j + 1] == a[j + 2])
      {
       a[j + 1] = 2 * a[j + 1];
       score = score + a[j + 1];
       a[j + 2] = 0;
      }
     }
     else if (a[j + 1] == a[j + 2])
     {
      a[j + 1] = 2 * a[j + 1];
      score = score + a[j + 1];
      a[j + 2] = a[j + 3];
      a[j + 3] = 0;
     }
     else if (a[j + 2] == a[j + 3])
     {
      a[j + 2] = 2 * a[j + 2];
      score = score + a[j + 2];
      a[j + 3] = 0;
     }
     else;
    }
    break;
    /* 向右移动 */
   case 77:
    /* 全部移动到右面 */
    for (j = 3; j <= 16; j = j + 4)
    {
     for (c = 0; c < 3; c++)
     {
      if (a[j] == 0)
      {
       a[j] = a[j - 1];
       a[j - 1] = 0;
      }
      if (a[j - 1] == 0)
      {
       a[j - 1] = a[j - 2];
       a[j - 2] = 0;
      }
      if (a[j - 2] == 0)
      {
       a[j - 2] = a[j - 3];
       a[j - 3] = 0;
      }
     }
     /* 移到右面后的合并操作 */
     if (a[j] == a[j - 1])
     {
      a[j] = a[j] * 2;
      score = score + a[j];
      a[j - 1] = a[j - 2];
      a[j - 2] = a[j - 3];
      a[j - 3] = 0;
      if (a[j - 1] == a[j - 2])
      {
       a[j - 1] = 2 * a[j - 1];
       score = score + a[j - 1];
       a[j - 2] = 0;
      }
     }
     else if (a[j - 1] == a[j - 2])
     {
      a[j - 1] = 2 * a[j - 1];
      score = score + a[j - 1];
      a[j - 2] = a[j - 3];
      a[j - 3] = 0;
     }
     else if (a[j - 2] == a[j - 3])
     {
      a[j - 2] = 2 * a[j - 2];
      score = score + a[j - 2];
      a[j - 3] = 0;
     }
     else;
    }
    break;
    /* 重新开始 */
   case 'r':
    for (j = 0; j < 16; j++)
     a[j] = 0;
    sj1 = rands();
    sj2 = rands();
    for (;;)
    {
     if (sj1 == sj2)
     {
      sj1 = rands();
      sj2 = rands();
     }
     else
      break;
    }
    a[sj1] = shu();
    a[sj2] = shu();
    flag = 0;
    score = 0;
    jx = 0;
    break;
    /* 退出 */
   case 'e':
    shua();
    ct(32,19,15,3,207);
    printf(" 游戏已退出!");
    textcolor(255);
    gotoxy(0,0);
    getch();
    return 0;
    break;
    /* 其他情况(刷新) */
   default:
    ;
    break;
  }      
  /*windows下的系统清屏函数*/
  //system ("cls");
  /* 判断最高分 */
  if (score > scoremax)
   scoremax = score;
  /* 判断是否胜利(是否含有2048) */
  for (j = 0; j < 16; j++)
  {
   if (a[j] == 2048 && jx == 0)
   {
    /* 胜利时输出游戏结果 */
    /*显示*/
    prin(a,&score,&scoremax);
    shua();
    ct(20,19,15,3,207);
    printf(" 游戏胜利!");
    textcolor(159);
    gotoxy(38,19);
    printf(" Continue    ");
    textcolor(239);
    gotoxy(49,20);
    printf(" Restart ");
    textcolor(175);
    gotoxy(49,21);
    printf(" Exit  ");
    t=2;
    an=75;
    yx=0;
    for(;;)
    {
     an=getch();
     switch(an)
     {
     case 72:
      if(t==2)
      {
       t=3;
     textcolor(240);
     gotoxy(36,21);
     printf("       ");
     textcolor(175);
     gotoxy(38,21);
     printf(" Exit     ");
     textcolor(240);
     gotoxy(36,19);
     printf("       ");
     textcolor(159);
     gotoxy(47,19);
     printf("  Continue ");
     textcolor(240);
     gotoxy(36,20);
     printf("       ");
     textcolor(239);
     gotoxy(49,20);
     printf(" Restart ");
      }
      else if(t==1)
      {
       t=2;
       textcolor(240);
     gotoxy(36,19);
     printf("       ");
     textcolor(159);
     gotoxy(38,19);
     printf(" Continue    ");
     textcolor(240);
     gotoxy(36,20);
     printf("       ");
     textcolor(239);
     gotoxy(47,20);
     printf("  Restart ");
     textcolor(240);
     gotoxy(36,21);
     printf("       ");
     textcolor(175);
     gotoxy(49,21);
     printf(" Exit  ");
      }
      else
      {
       t=1;
       textcolor(240);
     gotoxy(36,20);
     printf("       ");
     textcolor(239);
     gotoxy(38,20);
     printf(" Restart     ");
     textcolor(240);
     gotoxy(36,21);
     printf("       ");
     textcolor(175);
     gotoxy(47,21);
     printf("  Exit  "); 
     textcolor(240);
     gotoxy(36,19);
     printf("       ");
     textcolor(159);
     gotoxy(49,19);
     printf(" Continue ");
      }
      break;
     case 80:
      if(t==2)
      {
       t=1;
        textcolor(240);
     gotoxy(36,20);
     printf("       ");
     textcolor(239);
     gotoxy(38,20);
     printf(" Restart     ");
     textcolor(240);
     gotoxy(36,19);
     printf("       ");
     textcolor(159);
     gotoxy(47,19);
     printf("  Continue ");
     textcolor(240);
     gotoxy(36,21);
     printf("       ");
     textcolor(175);
     gotoxy(49,21);
     printf(" Exit  ");
      }
      else if(t==1)
      {
       t=3;
       textcolor(240);
     gotoxy(36,21);
     printf("       ");
     textcolor(175);
     gotoxy(38,21);
     printf(" Exit     ");
     textcolor(240);
     gotoxy(36,20);
     printf("       ");
     textcolor(239);
     gotoxy(47,20);
     printf("  Restart ");
     textcolor(240);
     gotoxy(36,19);
     printf("       ");
     textcolor(159);
     gotoxy(49,19);
     printf(" Continue ");
      }
      else
      {
      t=2;
      textcolor(240);
     gotoxy(36,19);
     printf("       ");
     textcolor(159);
     gotoxy(38,19);
     printf(" Continue    ");
     textcolor(240);
     gotoxy(36,21);
     printf("       ");
     textcolor(175);
     gotoxy(47,21);
     printf("  Exit  "); 
     textcolor(240);
     gotoxy(36,20);
     printf("       ");
     textcolor(239);
     gotoxy(49,20);
     printf(" Restart ");
      }
      break;
     case '\r':
      yx=1;
      break;
     }
     if(yx==1)
     {
      yx=0;
      break;
     }
    }
    shua();
    textcolor(255);
    gotoxy(0,0);
    /* 重新开始游戏 */
    if (t == 1)
    {
     shua();
     pd = 1;
     x = 0;
     for (j = 0; j < 16; j++)
      a[j] = 0;
     sj1 = rands();
     sj2 = rands();
     for (;;)
     {
      if (sj1 == sj2)
      {
       sj1 = rands();
       sj2 = rands();
      }
      else
       break;
     }
     a[sj1] = shu();
     a[sj2] = shu();
     for (j = 0; j < 16; j++)
      b[j] = a[j];
     flag = 0;
     score = 0;
     break;
    }
    /* 继续游戏 */
    else if (t == 2)
    {
     jx = 1;
     shua();
    }
    else
    {
     shua();
     ct(32,19,15,3,207);
     printf(" 游戏已退出!");
     textcolor(255);
     gotoxy(0,0);
     getch();
     return 0;
    }
   }
  }      
  /* 决定是否产生新的数及其位置以及游戏是否失败 */
  /* flag,pd,x赋初值 */
  flag = 0;
  pd = 1;
  x = 0;
  /* 判断移动后是否有空位:pd=0为有空位,pd=1为无空位 */
  for (j = 0; j < 16; j++)
  {
   if (a[j] == 0)
    pd = 0;
  }
  /* 移动是否有效:x=1有效,x=0无效 */
  for (j = 0; j < 16; j++)
  {
   if (a[j] != b[j])
   {
    x = 1;
    break;
   }
  }
  /* 移动有效时且有空位时产生新数 */
  if (pd == 0 && x == 1)
  {
   for (;;)
   {
    sj = rands();
    if (a[sj] == 0)
    {
     a[sj] = shu();
     break;
    }
    else;
   }
  }
  /* 游戏是否失败的判定 */
  /* 移动后无空位 */
  else if (pd != 0)
  {
   /* 判断是否还有可合并的项 */
   for (j = 0; j < 16; j++)
   {
    if (j != 3 && j != 7 && j != 11 && j != 15 && a[j] == a[j + 1])
    {
     flag = flag + 1;
    }
    if (j != 12 && j != 13 && j != 14 && j != 15
     && a[j] == a[j + 4])
    {
     flag = flag + 1;
    }
    if (j != 0 && j != 4 && j != 8 && j != 12 && a[j] == a[j - 1])
    {
     flag = flag + 1;
    }
    if (j != 0 && j != 1 && j != 2 && j != 3 && a[j] == a[j - 4])
    {
     flag = flag + 1;
    }
   }     
   /* 无空位且没有可合并的项时游戏失败 */
   if (flag == 0)
   {
    /* 失败时输出游戏结果 */
    /*显示*/
    prin(a,&score,&scoremax);
    shua();
    ct(20,19,15,3,271);
    printf(" 游戏结束!");
    textcolor(240);
    gotoxy(38,19);
    printf("Whether continue the game ?");
    gotoxy(44,21);
    printf("@Yes  No");
    gotoxy(44,21);
    t=1;
    an=75;
    yx=0;
    for(;;)
    {
     an=getch();
     switch(an)
     {
     case 75:
      if(t==1)
      {
       t=0;
       gotoxy(53,21);
       printf("@");
       gotoxy(44,21);
       printf(" ");
      }
      else
      {
       t=1;
       gotoxy(44,21);
       printf("@");
       gotoxy(53,21);
       printf(" ");
      }
      break;
     case 77:
      if(t==1)
      {
       t=0;
       gotoxy(53,21);
       printf("@");
       gotoxy(44,21);
       printf(" ");
 
      }
      else
      {
       t=1;
       gotoxy(44,21);
       printf("@");
       gotoxy(53,21);
       printf(" ");
      }
      break;
     case '\r':
      yx=1;
      break;
     }
     if(yx==1)
     {
      yx=0;
      break;
     }
    }
    shua();
    textcolor(255);
    gotoxy(0,0);
    if (t == 1)
    {
     for (j = 0; j < 16; j++)
      a[j] = 0;
     sj=rands();
     sj1 = rands();
     sj2 = rands();
     for (;;)
     {
      if (sj1 ==sj2||sj1==sj||sj2==sj)
      {
       sj1 = rands();
       sj2 = rands();
      }
      else
       break;
     }
     a[sj]=shu();
     a[sj1] = shu();
     a[sj2] = shu();
     score = 0;
     jx = 0;
    }
    else
    {
     shua();
     ct(32,19,15,3,207);
     printf(" 游戏已退出!");
     textcolor(255);
     gotoxy(0,0);
     getch();
     return 0;
    }
   }
  }
  else;
  /* 显示结果 */
  prin(a,&score,&scoremax);
 }
 return 0;
}
/*显示函数*/
void p(int*a,int i,int x,int y)
{
 int x1=x,y1=y;
 textcolor(col(a+i));
 x1=x+(i%4)*7;
 y1=y+(i/4)*4;
 gotoxy(x1,y1);
 printf("  ");
 gotoxy(x1,y1+1);
 kg(a+i);
 gotoxy(x1,y1+2);
 printf("  ");
 textcolor(240);
}
/*字体空格声明*/
void kg(int*a)
{
 if(*a==0)
  printf("  ",*a);
 else if(*a<10)
  printf(" %d ",*a);
 else if(*a<100)
  printf(" %d ",*a);
 else if(*a<1000)
  printf(" %d ",*a);
 else if(*a<10000)
  printf("%d ",*a);
 else
  printf("%d",*a);
}
/*单个颜色返回值*/
int col(int*a)
{
int co=127;
if(*(a)==0)
co=127;
else if(*(a)==2)
co=143;
else if(*(a)==4)
co=191;
else if(*(a)==8)
co=175;
else if(*(a)==16)
co=239;
else if(*(a)==32)
co=223;
else if(*(a)==64)
co=207;
else if(*(a)==128)
co=95;
else if(*(a)==256)
co=159;
else if(*(a)==512)
co=63;
else if(*(a)==1024)
co=111;
else if(*(a)==2048)
co=79;
else if(*(a)==4096)
co=287;
else
co=271;
return co;
}
/*标题字幕2048*/
void p2048(int x,int y)
{
 int i;
 textcolor(240);
 gotoxy(0,0);
 for(i=0;i<=500;i++)
  {
   printf("  ");
  }
 //2
 textcolor(207);
 gotoxy(x,y);
 printf("   ");
 gotoxy(x,y+1);
 printf("   ");
 gotoxy(x+7,y+2);
 printf(" ");
 gotoxy(x+7,y+3);
 printf(" ");
 gotoxy(x,y+4);
 printf("   ");
 gotoxy(x,y+5);
 printf("   ");
 gotoxy(x,y+6);
 printf(" ");
 gotoxy(x,y+7);
 printf(" ");
 gotoxy(x,y+8);
 printf("   ");
 gotoxy(x,y+9);
 printf("   ");
 //0
 textcolor(239);
 gotoxy(x+13,y);
 printf("   ");
 gotoxy(x+13,y+1);
 printf("   ");
 gotoxy(x+13,y+2);
 printf(" ");
 gotoxy(x+13+7,y+2);
 printf(" ");
 gotoxy(x+13,y+3);
 printf(" ");
 gotoxy(x+13+7,y+3);
 printf(" ");
 gotoxy(x+13,y+4);
 printf(" ");
 gotoxy(x+13+7,y+4);
 printf(" ");
 gotoxy(x+13,y+5);
 printf(" ");
 gotoxy(x+13+7,y+5);
 printf(" ");
 gotoxy(x+13,y+6);
 printf(" ");
 gotoxy(x+13+7,y+6);
 printf(" ");
 gotoxy(x+13,y+7);
 printf(" ");
 gotoxy(x+13+7,y+7);
 printf(" ");
 gotoxy(x+13,y+8);
 printf("   ");
 gotoxy(x+13,y+9);
 printf("   ");
 //4
 textcolor(159);
 gotoxy(x+26,y);
 printf(" ");
 gotoxy(x+26+7,y);
 printf(" ");
 gotoxy(x+26,y+1);
 printf(" ");
 gotoxy(x+26+7,y+1);
 printf(" ");
 gotoxy(x+26,y+2);
 printf(" ");
 gotoxy(x+26+7,y+2);
 printf(" ");
 gotoxy(x+26,y+3);
 printf(" ");
 gotoxy(x+26+7,y+3);
 printf(" ");
 gotoxy(x+26,y+4);
 printf("   ");
 gotoxy(x+26,y+5);
 printf("   ");
 gotoxy(x+26,y+6);
 printf(" ");
 gotoxy(x+26,y+7);
 printf(" ");
 gotoxy(x+26,y+8);
 printf(" ");
 gotoxy(x+26,y+9);
 printf(" ");
 //8
 textcolor(175);
 for(i=0;i<10;i++)
 {
 gotoxy(x+39,y+i);
 printf("   ");
 }
 textcolor(240);
 gotoxy(x+42,y+2);
 printf(" ");
 gotoxy(x+42,y+3);
 printf(" ");
 gotoxy(x+42,y+6);
 printf(" ");
 gotoxy(x+42,y+7);
 printf(" ");
 //长条
  ct(35,15,20,1,240);
  printf(" Game Sart");
  ct(35,18,20,1,240);
  printf("How to play");
  printf("\n");
  printf("\n\n\n               Made by Mr Yan");
  gotoxy(35-25,15);
  textcolor(255);
  gotoxy(0,0);
}
/*画长条函数*/
void ct(int x,int y,int l,int k,int c)
{
 int i=0,j=0;
 textcolor(c);
 gotoxy(x,y);
 for(i=0;i<k;i++)
 {
 gotoxy(x,y+i);
 for(j=0;j<l;j++)
 {
 printf(" ");
 }
 }
 gotoxy(x+1,y+k/2);
}
void shua(void)
{
 gotoxy(0,18);
 textcolor(255);
 printf("                 \n");
 printf("                 \n");
 printf("                 \n");
 printf("                 \n");
 printf("                 \n");
 printf("                 \n");
 gotoxy(0,0);
}
/*胜利画面测试函数*/
int test(void)
{
 int i,t,yx;
 char an;
  textcolor(240);
  gotoxy(0,0);
  for(i=0;i<=500;i++)
  {
   printf("  ");
  }
  gotoxy(0,0);
  HANDLE hOut=GetStdHandle(STD_OUTPUT_HANDLE);
  CONSOLE_CURSOR_INFO cci;
  GetConsoleCursorInfo(hOut,&cci);
  cci.bVisible=FALSE;
  SetConsoleCursorInfo(hOut,&cci);
    //printf("\n游戏胜利!t\n重新开始游戏请按1\n继续游戏请按2\n退出请按其他键3\n"); 2 1 3
                      
/*
 2 继续选中 textcolor(240);
     gotoxy(36,19);
     printf("       ");
     textcolor(159);
     gotoxy(38,19);
     printf(" Continue    ");
 
 2 继续平常 textcolor(240);
     gotoxy(36,19);
     printf("       ");
     textcolor(159);
     gotoxy(49,19);
     printf(" Continue ");
 
 1 重新选中 textcolor(240);
     gotoxy(36,20);
     printf("       ");
     textcolor(239);
     gotoxy(38,20);
     printf(" Restart     ");
 
 1 重新平常 textcolor(240);
     gotoxy(36,20);
     printf("       ");
     textcolor(239);
     gotoxy(49,20);
     printf(" Restart ");
 
 3 退出选中 textcolor(240);
     gotoxy(36,21);
     printf("       ");
     textcolor(175);
     gotoxy(38,21);
     printf(" Exit     ");
 
 3 退出平常 textcolor(240);
     gotoxy(36,21);
     printf("       ");
     textcolor(175);
     gotoxy(49,21);
     printf(" Exit  ");
   //  0000000000000000000000000 24 
  */
    shua();
    ct(20,19,15,3,207);
    printf(" 游戏胜利!");
    textcolor(159);
    gotoxy(38,19);
    printf(" Continue    ");
 
    textcolor(239);
    gotoxy(49,20);
    printf(" Restart ");
    textcolor(175);
    gotoxy(49,21);
    printf(" Exit  ");
    t=2;
    an=75;
    yx=0;
    for(;;)
    {
     an=getch();
     switch(an)
     {
     case 72:
      if(t==2)
      {
       t=3;
     textcolor(240);
     gotoxy(36,21);
     printf("       ");
     textcolor(175);
     gotoxy(38,21);
     printf(" Exit     ");
     textcolor(240);
     gotoxy(36,19);
     printf("       ");
     textcolor(159);
     gotoxy(47,19);
     printf("  Continue ");
     textcolor(240);
     gotoxy(36,20);
     printf("       ");
     textcolor(239);
     gotoxy(49,20);
     printf(" Restart ");
      }
      else if(t==1)
      {
       t=2;
       textcolor(240);
     gotoxy(36,19);
     printf("       ");
     textcolor(159);
     gotoxy(38,19);
     printf(" Continue    ");
     textcolor(240);
     gotoxy(36,20);
     printf("       ");
     textcolor(239);
     gotoxy(47,20);
     printf("  Restart ");
     textcolor(240);
     gotoxy(36,21);
     printf("       ");
     textcolor(175);
     gotoxy(49,21);
     printf(" Exit  ");
      }
      else
      {
       t=1;
       textcolor(240);
     gotoxy(36,20);
     printf("       ");
     textcolor(239);
     gotoxy(38,20);
     printf(" Restart     ");
     textcolor(240);
     gotoxy(36,21);
     printf("       ");
     textcolor(175);
     gotoxy(47,21);
     printf("  Exit  "); 
     textcolor(240);
     gotoxy(36,19);
     printf("       ");
     textcolor(159);
     gotoxy(49,19);
     printf(" Continue ");
      }
      break;
     case 80:
      if(t==2)
      {
       t=1;
        textcolor(240);
     gotoxy(36,20);
     printf("       ");
     textcolor(239);
     gotoxy(38,20);
     printf(" Restart     ");
     textcolor(240);
     gotoxy(36,19);
     printf("       ");
     textcolor(159);
     gotoxy(47,19);
     printf("  Continue ");
     textcolor(240);
     gotoxy(36,21);
     printf("       ");
     textcolor(175);
     gotoxy(49,21);
     printf(" Exit  ");
      }
      else if(t==1)
      {
       t=3;
       textcolor(240);
     gotoxy(36,21);
     printf("       ");
     textcolor(175);
     gotoxy(38,21);
     printf(" Exit     ");
     textcolor(240);
     gotoxy(36,20);
     printf("       ");
     textcolor(239);
     gotoxy(47,20);
     printf("  Restart ");
     textcolor(240);
     gotoxy(36,19);
     printf("       ");
     textcolor(159);
     gotoxy(49,19);
     printf(" Continue ");
      }
      else
      {
      t=2;
      textcolor(240);
     gotoxy(36,19);
     printf("       ");
     textcolor(159);
     gotoxy(38,19);
     printf(" Continue    ");
     textcolor(240);
     gotoxy(36,21);
     printf("       ");
     textcolor(175);
     gotoxy(47,21);
     printf("  Exit  "); 
     textcolor(240);
     gotoxy(36,20);
     printf("       ");
     textcolor(239);
     gotoxy(49,20);
     printf(" Restart ");
      }
      break;
     case '\r':
      yx=1;
      break;
     }
     if(yx==1)
     {
      yx=0;
      break;
     }
    }
    shua();
    textcolor(255);
    gotoxy(0,0);
    textcolor(240);
    if(t==2)
     printf("Continue  ");
    else if(t==1)
     printf("Restart  ");
    else
     printf("Exit   ");
    return 0;
}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。

原文链接:https://blog.csdn.net/yanweibujian/article/details/50878782

延伸 · 阅读

精彩推荐
  • C/C++c/c++实现获取域名的IP地址

    c/c++实现获取域名的IP地址

    本文给大家汇总介绍了使用c/c++实现获取域名的IP地址的几种方法以及这些方法的核心函数gethostbyname的详细用法,非常的实用,有需要的小伙伴可以参考下...

    C++教程网10262021-03-16
  • C/C++C语言main函数的三种形式实例详解

    C语言main函数的三种形式实例详解

    这篇文章主要介绍了 C语言main函数的三种形式实例详解的相关资料,需要的朋友可以参考下...

    ieearth6912021-05-16
  • C/C++C语言实现双人五子棋游戏

    C语言实现双人五子棋游戏

    这篇文章主要为大家详细介绍了C语言实现双人五子棋游戏,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下...

    两片空白7312021-11-12
  • C/C++关于C语言中E-R图的详解

    关于C语言中E-R图的详解

    今天小编就为大家分享一篇关于关于C语言中E-R图的详解,小编觉得内容挺不错的,现在分享给大家,具有很好的参考价值,需要的朋友一起跟随小编来看看...

    Struggler095962021-07-12
  • C/C++深入C++拷贝构造函数的总结详解

    深入C++拷贝构造函数的总结详解

    本篇文章是对C++中拷贝构造函数进行了总结与介绍。需要的朋友参考下...

    C++教程网5182020-11-30
  • C/C++使用C++制作简单的web服务器(续)

    使用C++制作简单的web服务器(续)

    本文承接上文《使用C++制作简单的web服务器》,把web服务器做的功能稍微强大些,主要增加的功能是从文件中读取网页并返回给客户端,而不是把网页代码...

    C++教程网5492021-02-22
  • C/C++c/c++内存分配大小实例讲解

    c/c++内存分配大小实例讲解

    在本篇文章里小编给大家整理了一篇关于c/c++内存分配大小实例讲解内容,有需要的朋友们可以跟着学习参考下。...

    jihite5172022-02-22
  • C/C++OpenCV实现拼接图像的简单方法

    OpenCV实现拼接图像的简单方法

    这篇文章主要为大家详细介绍了OpenCV实现拼接图像的简单方法,具有一定的参考价值,感兴趣的小伙伴们可以参考一下...

    iteye_183805102021-07-29