本文实例为大家分享了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
|
#include <stdio.h> #include <stdlib.h> #include <string.h> #include <windows.h> #include <conio.h> char draw[32][60] = {{ " " }, { "[ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ]" }, { " " }, { "[ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ]" }, { " " }, { "[ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ]" }, { " " }, { "[ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ]" }, { " " }, { "[ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ]" }, { " " }, { "[ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ]" }, { " " }, { "[ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ]" }, { " " }, { "[ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ]" }, { " " }, { "[ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ]" }, { " " }, { "[ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ]" }, { " " }, { "[ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ]" }, { " " }, { "[ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ]" }, { " " }, { "[ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ]" }, { " " }, { "[ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ]" }, { " " }, { "[ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ]" }, }; typedef struct //坐标结构体 { int x; int y; } node; typedef struct //栈结构体 { node data[120]; int top; } stack; int win[3], player, step1, step2; stack player1,player2; char ch; node pos; int convY( int y); int convX( int x); void refreshDraw(); void BLUE(); void RED(); void GREEN(); void WHITE(); void printMap(); void printMenu(); void refreshMap(); void playerOperation( char cheese); void jundge( int p, char cheese); void in( char cheese); void playerVsPlayer(); void playerVscomputer(); void gotoxy( int x, int y); void printStep( int player, int step); void askExit(); void printPos( int player, int x, int y); void printRoad(); int main() { char num[99]; printMenu(); while (1) { WHITE(); refreshDraw(); pos.x = 0; pos.y = 0; win[1] = 0; win[2] = 0; gets (num); if ( strcmp (num, "1" ) == 0) { playerVsPlayer(); system ( "cls" ); printMenu(); } else if ( strcmp (num, "2" ) == 0) { askExit(); continue ; } else { printf ( "请输入正确数字!\n" ); } } return 0; } void BLUE() //字体变蓝 { SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),FOREGROUND_BLUE|FOREGROUND_INTENSITY); } void RED() //字体变红 { SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),FOREGROUND_RED|FOREGROUND_INTENSITY); } void GREEN() //字体变绿 { SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),FOREGROUND_GREEN|FOREGROUND_INTENSITY); } void WHITE() //字体变白 { SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),FOREGROUND_GREEN|FOREGROUND_RED|FOREGROUND_BLUE); } void printMap() //打印棋盘和棋子 { for ( int i = 0; i <= 29; i++) { for ( int j = 0; j <= 59; j++) { if (i==convX(pos.x) && (j==convY(pos.y)-1 || j==convY(pos.y)+1) ) { if (player == 1) { GREEN(); } else if (player == 2) { RED(); } } else if (draw[i][j] == '*' ) { RED(); } else if (draw[i][j] == 'O' ) { GREEN(); } printf ( "%c" , draw[i][j]); WHITE(); } printf ( "\n" ); } } void refreshDraw() //清空棋盘 { int i; for (i = 0; i <= 30; i=i+2) { strcpy (draw[i], " " ); strcpy (draw[i+1], "[ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ]" ); } strcpy (draw[31], " " ); } void printMenu() //打印菜单 { WHITE(); printf ( "\n\n ****************************\n" ); printf ( " * *\n" ); printf ( " * 欢迎使用 *\n" ); printf ( " * 五子棋模拟对战系统 *\n" ); printf ( " * *\n" ); printf ( " ****************************\n\n\n" ); printf ( "\n【设计制作:by 软件18-8邵蔚】\n\n" ); printf ( "游戏规则为:\n二人参与游玩,双方分别使用两种棋子,\n" ); printf ( "下在棋盘的 [ ] 上,\n" ); printf ( "先形成5子连线(斜线、竖线或横线)的人获胜。\n\n" ); printf ( "操作说明:\n玩家用键盘“W”“S”“A”“D”来选择下棋位置,选择好后,\n" ); printf ( "按“J”键来下棋,然后本回合结束,下一名玩家操作。\n\n" ); printf ( "输入 1 并按回车开始游戏\n" ); printf ( "输入 2 并按回车退出游戏\n" ); } int convY( int y) //将棋盘坐标转换为字符串数组下标 { return 4*(y+8)-3; } int convX( int x) //将棋盘坐标转换为字符串数组下标 { return 2*(8-x)-1; } void refreshMap() //刷新棋盘画面 { gotoxy(0,0); printMap(); gotoxy(0,0); } void playerOperation( char cheese) //扫描玩家输入的 "W""A""S""D""J""K" , 并且在棋盘显示或删除棋子; { while (1) { refreshMap(); ch = getch(); if (ch == 'a' && pos.y > -7) { pos.y--; } else if (ch == 'd' && pos.y < 7) { pos.y++; } else if (ch == 'w' && pos.x < 7) { pos.x++; } else if (ch == 's' && pos.x > -7) { pos.x--; } else if (ch == 'j' && draw[convX(pos.x)][convY(pos.y)] == ' ' ) { draw[convX(pos.x)][convY(pos.y)] = cheese; in(cheese); refreshMap(); break ; } else if (ch == 'k' && player1.top > 0) { int tx, ty; if (cheese == '*' ) { tx = player1.data[player1.top].x; ty = player1.data[player1.top].y; player1.top--; draw[convX(tx)][convY(ty)] = ' ' ; refreshMap(); break ; } else if (cheese == 'O' ) { tx = player2.data[player2.top].x; ty = player2.data[player2.top].y; player2.top--; } draw[convX(tx)][convY(ty)] = ' ' ; refreshMap(); break ; } else if (ch == 'k' && player1.top <= 0) { system ( "cls" ); printf ( "提示:没有棋子了!\n" ); system ( "pause" ); refreshMap(); printStep(1, step1); printStep(2, step2); gotoxy(65,16); printf ( " 按 K 来悔棋。" ); gotoxy(65, 23); GREEN(); printf ( "(玩家1 : O)" ); gotoxy(65, 24); RED(); printf ( "(玩家1 : *)" ); } } } void jundge( int p, char cheese) //判断是否有赢家 { int tx, ty, sum = 0; tx = pos.x; ty = pos.y; sum = 0; while (draw[convX(tx)][convY(ty)] == cheese && sum <= 5) { if (draw[convX(tx)][convY(ty)] == cheese) sum++; tx++; } tx = pos.x - 1; ty = pos.y; while (draw[convX(tx)][convY(ty)] == cheese && sum <= 5) { if (draw[convX(tx)][convY(ty)] == cheese) sum++; tx--; } if (sum >= 5) { win[p] = 1; return ; } //-------------------------------------------------------------------- tx = pos.x; ty = pos.y; sum = 0; while (draw[convX(tx)][convY(ty)] == cheese && sum <= 5) { if (draw[convX(tx)][convY(ty)] == cheese) sum++; ty++; } tx = pos.x; ty = pos.y - 1; while (draw[convX(tx)][convY(ty)] == cheese && sum <= 5) { if (draw[convX(tx)][convY(ty)] == cheese) sum++; ty--; } if (sum >= 5) { win[p] = 1; return ; } //---------------------------------------------------------------------- tx = pos.x; ty = pos.y; sum = 0; while (draw[convX(tx)][convY(ty)] == cheese && sum <= 5) { if (draw[convX(tx)][convY(ty)] == cheese) sum++; ty++; tx++; } tx = pos.x - 1; ty = pos.y - 1; while (draw[convX(tx)][convY(ty)] == cheese && sum <= 5) { if (draw[convX(tx)][convY(ty)] == cheese) sum++; ty--; tx--; } if (sum >= 5) { win[p] = 1; return ; } //---------------------------------------------------------------------- tx = pos.x; ty = pos.y; sum = 0; while (draw[convX(tx)][convY(ty)] == cheese && sum <= 5) { if (draw[convX(tx)][convY(ty)] == cheese) sum++; tx--; ty++; } tx = pos.x + 1; ty = pos.y - 1; while (draw[convX(tx)][convY(ty)] == cheese && sum <= 5) { if (draw[convX(tx)][convY(ty)] == cheese) sum++; tx++; ty--; } if (sum >= 5) { win[p] = 1; return ; } return ; } void in( char cheese) //将棋子坐标入栈 { if (cheese == '*' ) { player2.top++; player2.data[player2.top].x = pos.x; player2.data[player2.top].y = pos.y; } else if (cheese == 'O' ) { player1.top++; player1.data[player1.top].x = pos.x; player1.data[player1.top].y = pos.y; } } void playerVsPlayer() { step1 = 0; step2 = 0; player1.top = 0; player2.top = 0; player1.data[0].x = -1; player1.data[0].y = -1; player2.data[0].x = -1; player2.data[0].y = -1; player = 2; printStep(1, step1); printStep(2, step2); gotoxy(65, 16); printf ( " 按 K 来悔棋。" ); gotoxy(65, 17); printf ( " 按 J 来下子。" ); gotoxy(65, 18); printf ( " 按 W A S D 控制方向。" ); gotoxy(65, 23); GREEN(); printf ( "(玩家1 : O)" ); gotoxy(65, 24); RED(); printf ( "(玩家2 : *)" ); WHITE(); while (win[1] == 0 && win[2] == 0) { int tx, ty; if (win[1] == 0 && win[2] == 0) { player = 1; playerOperation( 'O' ); if (ch == 'k' || ch == 'K' ) step2--; else step1++; tx = player1.data[player1.top].x; ty = player1.data[player1.top].y; printStep(1, step1); printStep(2, step2); printPos(1, tx, ty); } jundge(1, 'O' ); if (win[1] == 0 && win[2] == 0) { player = 2; playerOperation( '*' ); if (ch == 'k' || ch == 'K' ) step1--; else step2++; tx = player2.data[player2.top].x; ty = player2.data[player2.top].y; printStep(1, step1); printStep(2, step2); printPos(2, tx, ty); } jundge(2, '*' ); } if (win[1] == 1) { gotoxy(0,31); printf ( "【" ); GREEN(); printf ( "玩家1" ); WHITE(); printf ( "】 赢了 !\n" ); } else if (win[2] == 1) { gotoxy(0,31); printf ( "【" ); RED(); printf ( "玩家2" ); WHITE(); printf ( "】 赢了 !\n" ); } printf ( "按回车查看双方下棋路线!\n" ); getchar (); system ( "cls" ); if (win[1] == 1) { GREEN(); printf ( "【玩家1】 赢了 !\n" ); } else if (win[2] == 1) { RED(); printf ( "【玩家2】 赢了 !\n" ); } printRoad(); getchar (); return ; } void askExit() //询问退出 { char dis[20]; system ( "cls" ); printf ( "确定退出?\n Y/N?\n" ); gets (dis); if ( strcmp (dis, "y" ) == 0 || strcmp (dis, "Y" ) == 0) { system ( "cls" ); printf ( "感谢你的使用!\n" ); exit (0); } else { system ( "cls" ); printMenu(); } return ; } void gotoxy( int x, int y) //移动光标函数 { COORD pos = {x,y}; HANDLE hOut = GetStdHandle(STD_OUTPUT_HANDLE); SetConsoleCursorPosition(hOut, pos); } void printStep( int player, int step) //打印已走步数 { if (player == 1) { gotoxy(65,4); GREEN(); printf ( "【玩家1】 :%02d" , step); WHITE(); } else if (player == 2) { gotoxy(65,6); RED(); printf ( "【玩家2】 :%02d" , step); WHITE(); } return ; } void printPos( int player, int x, int y) //打印下子坐标 { if (player == 1) { gotoxy(65, 10); GREEN(); if (x == -1 && y == -1) { printf ( " " ); } else { printf ( "玩家1下棋位置:第%d行,第%d列" ,8 + x, 8 + y); } WHITE(); } else if (player == 2) { gotoxy(65, 10); RED(); if (x == -1 && y == -1) { printf ( " " ); } else { printf ( "玩家2下棋位置:第%d行,第%d列" ,8 + x, 8 + y); } WHITE(); } return ; } void printRoad() //打印所有下子坐标 { int i; GREEN(); printf ( "玩家1的下棋路线为:\n" ); for (i = 1; i <= player1.top; i++) { printf ( "[ 第%d步 : (%d, %d) ] " , i, 8 + player1.data[i].x, 8 + player1.data[i].y); if (i % 5 == 0) printf ( "\n" ); } printf ( "\n" ); RED(); printf ( "玩家2的下棋路线为:\n" ); for (i = 1; i <= player2.top; i++) { printf ( "[ 第%d步 : (%d, %d) ] " , i, 8 + player2.data[i].x, 8 + player2.data[i].y); if (i % 5 == 0) printf ( "\n" ); } printf ( "\n" ); WHITE(); printf ( "输入 E 结束查看并返回菜单!" ); while (1) { scanf ( "%c" , &ch); if (ch == 'E' || ch == 'e' ) break ; } return ; } |
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。
原文链接:https://blog.csdn.net/qq_43038891/article/details/85227560