本文实例为大家分享了C语言实现五子棋游戏的具体代码,供大家参考,具体内容如下
一、前言
本文将先介绍五子棋运行所需要的函数,最后串联成完整代码。
我们需要实现的功能有:1.菜单menu函数
2.初始化棋盘Initboard函数
3.显示棋盘Displayboard函数
4.实现人机、人人模式的选择
5.落子函数 playermove computermove
6.判断输赢ifwin函数
先来看看运行效果吧!
二、头文件
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
|
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <string.h>
#include<time.h>
#include<stdlib.h>
#include<windows.h>
#define ROW 10 //定义棋盘的宽
#define COL 10 //定义棋盘的长
//定义常量方便全局的修改
void Initboard( char board[ROW][COL], int row, int col);
//初始化棋盘
void Displayboard( char board[ROW][COL], int row, int col);
//展示棋盘
void Playermove_1( char board[ROW][COL], int row, int col, int *px, int *py);
//玩家一下棋
void Playermove_2( char board[ROW][COL], int row, int col, int *px, int *py);
//玩家二下棋
void computermove( char board[ROW][COL], int row, int col);
//电脑下棋
void game1( char board[ROW][COL], int row, int col);
//人机模式
void game2( char board[ROW][COL], int row, int col);
//人人模式
int Ifwin( char board[ROW][COL], int row, int col, int x, int y);
//判断输赢
int check_x( char board[ROW][COL], int row, int col, int x, int y);
//判断竖直五子
int check_y( char board[ROW][COL], int row, int col, int x, int y);
//判断水平五子
int check_xy_up( char board[ROW][COL], int row, int col, int x, int y);
//判断斜上五子
int check_xy_down( char board[ROW][COL], int row, int col, int x, int y);
//判断斜下五子
|
三、menu菜单函数
1
2
3
4
5
6
7
8
9
|
void menu()
{ //完成菜单的初始化
printf ( "***********************\n" );
printf ( "***********************\n" );
printf ( "*****1.play 0.exit****\n" );
printf ( "***********************\n" );
printf ( "***********************\n" );
printf ( "请输入:" );
}
|
很简单,就不多说了
四、初始化棋盘Initboard函数
初始化棋盘,为数组一一存入空格
1
2
3
4
5
6
7
8
9
10
11
12
|
void Initboard( char board[ROW][COL], int row, int col)
{
int i = 0;
int j = 0;
for (i = 0; i < row; i++)
{
for (j = 0; j < col; j++)
{
board[i][j] = ' ' ;
}
}
}
|
五、显示棋盘Displayboard函数
打印简单的方格线,并打印数据内容
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
|
void Displayboard( char board[ROW][COL], int row, int col)
{
int i = 0;
int j = 0;
for (j = 0; j < col; j++) //打印横向y坐标
{
if (j == 0) //考虑到x轴所占的一格
{
printf ( " %d " , j + 1);
}
else
{
printf ( " %d " , j + 1);
}
}
printf ( "\n" );
for (i = 0; i < row; i++) //打印数据
{
for (j = 0; j < col; j++)
{
if (j == 0) //顺带打印竖向x坐标
{
printf ( "%2d" ,i+1); //%md表示域宽为m
printf ( " %c " ,board[i][j]);
}
else
{
printf ( " %c " , board[i][j]);
}
if (j < col - 1)
printf ( "|" );
}
printf ( "\n" );
//打印分割行
if (i < row - 1)
{
for (j = 0; j < col; j++)
{
if (j == 0) //同样考虑到y轴
printf ( " ---" );
else
printf ( "---" );
if (j < col - 1)
printf ( "|" );
}
printf ( "\n" );
}
}
}
|
六、落子函数
玩家一落子——playermove_1
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
|
void Playermove_1( char board[ROW][COL], int row, int col, int *px, int *py) //ROW可以省,COL不行
{
int x = *px;
int y = *py;
while (1)
{
if (x >= 1 && x <= row && y >= 1 && y <= col) //判断输入坐标的合法性
{
if (board[x -1][y- 1] == ' ' ) //保证落子点为空
{
board[x-1 ][y -1] = '*' ;
*px = x; *py = y; //如果开始xy输入不合法,则要对外部xy修改,所以选择传址操作
break ;
}
else
{
printf ( "你不能在此处落子\n" );
printf ( "玩家1落子,请重新输入x y坐标: " );
scanf ( "%d%d" , &x, &y);
}
}
else
{
printf ( "输入坐标不合法\n" );
printf ( "玩家落子,请重新输入x y坐标: " );
scanf ( "%d%d" , &x, &y);
}
}
}
|
玩家2落子完全相同,就不赘叙了
电脑落子
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
|
void computermove( char board[ROW][COL], int row, int col)
{
int x = 0;
int y = 0;
//rand函数的使用需要srand函数的初始化,但srand不可以被重复调用,所以放在外部
while (1)
{
x = rand () % ROW;
y = rand () % COL;
if (board[x][y] == ' ' ) //x的范围在0~row-1,所以这里x不用-1了
{
board[x][y] = '#' ;
break ;
}
}
}
|
七、判断输赢ifwin
根据我们下五子棋的经验,能获胜的地方必定是最后落子的水平线、竖直线、和两个斜对角线,我们只需对其进行检索即可。ifwin的返回值决定之后的switch语句
弱弱的说一句,虽然看起来多,但很多都是重复的。
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
|
int Ifwin( char board[ROW][COL], int row, int col, int x, int y)
{
int ret;
{
ret = check_x(board, row, col, x, y);
if (ret >= 5)
return 0;
ret = check_y(board, row, col, x, y);
if (ret >= 5)
return 0;
ret = check_xy_up(board, row, col, x, y);
if (ret >= 5)
return 0;
ret = check_xy_down(board, row, col, x, y);
if (ret >= 5)
return 0;
}
return 3;
}
int check_x( char board[ROW][COL], int row, int col, int x, int y) //检查x轴竖直方向
{
int sum = 1;
int old_x = x;
while (x < row && board[x - 1][y - 1] == board[x][y - 1])
{
sum++;
x++;
}
x = old_x;
while (x-2>= 0 && board[x - 1][y - 1] == board[x - 2][y - 1] )
{
sum++;
x--;
}
return sum;
}
int check_y( char board[ROW][COL], int row, int col, int x, int y) //检查y轴水平方向
{
int sum = 1;
int old_y = y;
while (y<col && board[x-1][y-1] == board[x-1][y] )
{
sum++;
y++;
}
y = old_y;
while (y-2 >= 0 && board[x-1][y-1] == board[x-1][y -2] )
{
sum++;
y--;
}
return sum;
}
int check_xy_up( char board[ROW][COL], int row, int col, int x, int y) //检查斜上45度
{
int sum = 1;
int old_x = x;
int old_y = y;
while (x<row && y - 2 >= 0 && board[x - 1][y - 1] == board[x][y - 2])
{
sum++;
x++;
y--;
}
x = old_x;
y = old_y;
while (x - 2 >= 0 && y <col && board[x - 1][y - 1] == board[x - 2][y])
{
sum++;
x--;
y++;
}
return sum;
}
int check_xy_down( char board[ROW][COL], int row, int col, int x, int y) //检查斜下45度
{
int sum = 1;
int old_x = x;
int old_y = y;
while (x<row && y <col && board[x - 1][y - 1] == board[x][y])
{
sum++;
x++;
y++;
}
x = old_x;
y = old_y;
while (board[x-1][y-1] == board[x -2][y-2 ] && x-2>=0 && y-2 >=0)
{
sum++;
x--;
y--;
}
return sum;
}
|
八、人机、人人模式的选择
初始选择界面
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
|
void game()
{
int input;
char board[ROW][COL] = { 0 };
Initboard(board, ROW, COL);
//初始化棋盘
Displayboard(board, ROW, COL);
//展示棋盘
printf ( "请选择模式:1.人机;2.玩家对战\n" );
printf ( "请选择:" );
do {
scanf ( "%d" , &input);
printf ( "\n" );
switch (input)
{
case 1: printf ( "游戏模式1开始!(注意:竖为x轴,横为y轴)\n" ); game1(board, ROW, COL); break ;
case 2: printf ( "游戏模式2开始!(注意:竖为x轴,横为y轴)\n" ); ; game2(board, ROW, COL); break ;
default : printf ( "请重新输入\n" ); break ;
}
} while (input != 1 && input != 2); //do while语句在输入错误时反复输入
}
|
game1的具体实现
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
|
void game1( char board[ROW][COL], int row, int col)
{
srand ((unsigned int ) time (NULL)); //srand只要设置一次就可以,所以要放在while循环外
while (1)
{
int ret;
int count = 0;
int flag = 0;
//判读玩家的下棋
int x; int y;
printf ( "玩家落子,请依次输入x y坐标: " );
scanf ( "%d%d" , &x, &y);
Playermove_1(board, row, col,&x, &y);
count++;
Displayboard(board, ROW, COL);
if (count == ROW*COL) //全下满则认为平局
ret = 1;
else
ret = Ifwin(board, ROW, COL, x, y);
switch (ret)
{
case 0: printf ( "玩家获胜\n" ); flag = 1; break ;
case 1: printf ( "平局\n" ); flag = 1; break ;
default : break ;
}
if (flag == 1) //用flag跳出while循环
break ;
//判断计算机的下棋
computermove(board, row, col);
count++;
Displayboard(board, ROW, COL);
if (count == ROW*COL)
ret = 1;
else
ret = Ifwin(board, ROW, COL, x, y);
switch (ret)
{
case 0: printf ( "电脑获胜\n" ); flag = 1; break ;
case 1: printf ( "平局\n" ); flag = 1; break ;
default : break ;
}
if (flag == 1)
break ;
}
}
|
作者觉得写的是挺啰嗦,欢迎留言建议。game2和game1玩家一的内容很相近,就不赘述
九、作者遇到的坑点
1.输入的坐标和存在数组里的下标有减一的关系,在写的时候不要写忘记了
2.传址操作才能改变函数外的变量值
十、完整的代码
头文件就省了
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
|
int main()
{
int input;
do {
system ( "cls" ); //清屏
menu();
scanf ( "%d" , &input);
switch (input)
{
case 1:
printf ( "游戏开始\n\n" ); game(); break ;
case 0:
printf ( "退出游戏\n" ); break ;
default :
printf ( "请重新输入\n" ); break ;
}
Sleep(3000); //暂停3秒
} while (input); //do while语句实现可以反复玩
return 0;
}
void menu()
{ //完成菜单的初始化
printf ( "***********************\n" );
printf ( "***********************\n" );
printf ( "*****1.play 0.exit****\n" );
printf ( "***********************\n" );
printf ( "***********************\n" );
printf ( "请输入:" );
}
void Initboard( char board[ROW][COL], int row, int col)
{
int i = 0;
int j = 0;
for (i = 0; i < row; i++)
{
for (j = 0; j < col; j++)
{
board[i][j] = ' ' ;
}
}
}
void Displayboard( char board[ROW][COL], int row, int col)
{
int i = 0;
int j = 0;
for (j = 0; j < col; j++) //打印横向y坐标
{
if (j == 0) //考虑到x轴所占的一格
{
printf ( " %d " , j + 1);
}
else
{
printf ( " %d " , j + 1);
}
}
printf ( "\n" );
for (i = 0; i < row; i++) //打印数据
{
for (j = 0; j < col; j++)
{
if (j == 0) //顺带打印竖向x坐标
{
printf ( "%2d" ,i+1); //%md表示域宽为m
printf ( " %c " ,board[i][j]);
}
else
{
printf ( " %c " , board[i][j]);
}
//数字表示坐标
if (j < col - 1)
printf ( "|" );
}
printf ( "\n" );
//打印分割行
if (i < row - 1)
{
for (j = 0; j < col; j++)
{
if (j == 0) //同样考虑到y轴
printf ( " ---" );
else
printf ( "---" );
if (j < col - 1)
printf ( "|" );
}
printf ( "\n" ); //别忘记这里也要换行
}
}
}
void game()
{
int input;
char board[ROW][COL] = { 0 };
Initboard(board, ROW, COL);
//初始化棋盘
Displayboard(board, ROW, COL);
//展示棋盘
printf ( "请选择模式:1.人机;2.玩家对战\n" );
printf ( "请选择:" );
do {
scanf ( "%d" , &input);
printf ( "\n" );
switch (input)
{
case 1: printf ( "游戏模式1开始!(注意:竖为x轴,横为y轴)\n" ); game1(board, ROW, COL); break ;
case 2: printf ( "游戏模式2开始!(注意:竖为x轴,横为y轴)\n" ); ; game2(board, ROW, COL); break ;
default : printf ( "请重新输入\n" ); break ;
}
} while (input != 1 && input != 2); //do while语句在输入错误时反复输入
}
void game1( char board[ROW][COL], int row, int col)
{
srand ((unsigned int ) time (NULL)); //srand只要设置一次就可以,所以要放在while循环外
while (1)
{
int ret;
int count = 0;
int flag = 0;
//判读玩家的下棋
int x; int y;
printf ( "玩家落子,请依次输入x y坐标: " );
scanf ( "%d%d" , &x, &y);
Playermove_1(board, row, col,&x, &y);
count++;
Displayboard(board, ROW, COL);
if (count == ROW*COL) //全下满则认为平局
ret = 1;
else
ret = Ifwin(board, ROW, COL, x, y);
switch (ret)
{
case 0: printf ( "玩家获胜\n" ); flag = 1; break ;
case 1: printf ( "平局\n" ); flag = 1; break ;
default : break ;
}
if (flag == 1) //用flag跳出while循环
break ;
//判断计算机的下棋
computermove(board, row, col);
count++;
Displayboard(board, ROW, COL);
if (count == ROW*COL)
ret = 1;
else
ret = Ifwin(board, ROW, COL, x, y);
switch (ret)
{
case 0: printf ( "电脑获胜\n" ); flag = 1; break ;
case 1: printf ( "平局\n" ); flag = 1; break ;
default : break ;
}
if (flag == 1)
break ;
}
}
void game2( char board[ROW][COL], int row, int col)
{
while (1)
{
int ret;
int count = 0;
int flag = 0;
int x; int y;
//判读玩家1的下棋
printf ( "玩家1落子,请输入坐标:(注意:竖为x轴,横为y轴)" );
scanf ( "%d%d" , &x, &y);
Playermove_1(board, row, col, &x, &y);
count++;
Displayboard(board, ROW, COL);
if (count == ROW*COL)
ret = 1;
else
ret = Ifwin(board, ROW, COL, x, y);
switch (ret)
{
case 0: printf ( "玩家1获胜\n" ); flag = 1; break ;
case 1: printf ( "平局\n" ); flag = 1; break ;
default : break ;
}
if (flag == 1)
break ;
//判断玩家2的下棋
printf ( "玩家2落子,请输入坐标:(注意:竖为x轴,横为y轴)" );
scanf ( "%d%d" , &x, &y);
Playermove_2(board, row, col, &x, &y);
count++;
Displayboard(board, ROW, COL);
if (count == ROW*COL)
ret = 1;
else
ret = Ifwin(board, ROW, COL, x, y);
switch (ret)
{
case 0: printf ( "玩家2获胜\n" ); flag = 1; break ;
case 1: printf ( "平局\n" ); flag = 1; break ;
default : break ;
}
if (flag == 1)
break ;
}
}
void Playermove_1( char board[ROW][COL], int row, int col, int *px, int *py) //ROW可以省,COL不行
{
int x = *px;
int y = *py;
while (1)
{
if (x >= 1 && x <= row && y >= 1 && y <= col) //判断输入坐标的合法性
{
if (board[x -1][y- 1] == ' ' ) //保证落子点为空
{
board[x-1 ][y -1] = '*' ;
*px = x; *py = y; //如果开始xy输入不合法,要对外部xy修改,所以要传址操作
break ;
}
else
{
printf ( "你不能在此处落子\n" );
printf ( "玩家1落子,请重新输入x y坐标: " );
scanf ( "%d%d" , &x, &y);
}
}
else
{
printf ( "输入坐标不合法\n" );
printf ( "玩家落子,请重新输入x y坐标: " );
scanf ( "%d%d" , &x, &y);
}
}
}
void Playermove_2( char board[ROW][COL], int row, int col, int *px, int *py)
{
int x = *px;
int y = *py;
while (1)
{
if (x >= 1 && x <= row && y >= 1 && y <= col) //判断输入坐标的合法性
{
if (board[x - 1][y - 1] == ' ' ) //保证落子点为空
{
board[x - 1][y - 1] = '#' ;
*px = x; *py = y;
break ;
}
else
{
printf ( "你不能在此处落子\n" );
printf ( "玩家2落子,请重新输入x y坐标: " );
scanf ( "%d%d" , &x, &y);
}
}
else
{
printf ( "输入坐标不合法\n" );
printf ( "玩家落子,请重新输入x y坐标: " );
scanf ( "%d%d" , &x, &y);
}
}
}
void computermove( char board[ROW][COL], int row, int col)
{
int x = 0;
int y = 0;
while (1)
{
x = rand () % ROW;
y = rand () % COL;
if (board[x][y] == ' ' )
{
board[x][y] = '#' ;
break ;
}
}
}
int Ifwin( char board[ROW][COL], int row, int col, int x, int y)
{
int ret;
{
ret = check_x(board, row, col, x, y);
if (ret >= 5)
return 0;
ret = check_y(board, row, col, x, y);
if (ret >= 5)
return 0;
ret = check_xy_up(board, row, col, x, y);
if (ret >= 5)
return 0;
ret = check_xy_down(board, row, col, x, y);
if (ret >= 5)
return 0;
}
return 3;
}
int check_x( char board[ROW][COL], int row, int col, int x, int y) //检查x轴竖直方向
{
int sum = 1;
int old_x = x;
while (x < row && board[x - 1][y - 1] == board[x][y - 1])
{
sum++;
x++;
}
x = old_x;
while (x-2>= 0 && board[x - 1][y - 1] == board[x - 2][y - 1] )
{
sum++;
x--;
}
return sum;
}
int check_y( char board[ROW][COL], int row, int col, int x, int y) //检查y轴水平方向
{
int sum = 1;
int old_y = y;
while (y<col && board[x-1][y-1] == board[x-1][y] )
{
sum++;
y++;
}
y = old_y;
while (y-2 >= 0 && board[x-1][y-1] == board[x-1][y -2] )
{
sum++;
y--;
}
return sum;
}
int check_xy_up( char board[ROW][COL], int row, int col, int x, int y) //检查斜上45度
{
int sum = 1;
int old_x = x;
int old_y = y;
while (x<row && y - 2 >= 0 && board[x - 1][y - 1] == board[x][y - 2])
{
sum++;
x++;
y--;
}
x = old_x;
y = old_y;
while (x - 2 >= 0 && y <col && board[x - 1][y - 1] == board[x - 2][y])
{
sum++;
x--;
y++;
}
return sum;
}
int check_xy_down( char board[ROW][COL], int row, int col, int x, int y) //检查斜下45度
{
int sum = 1;
int old_x = x;
int old_y = y;
while (x<row && y <col && board[x - 1][y - 1] == board[x][y])
{
sum++;
x++;
y++;
}
x = old_x;
y = old_y;
while (board[x-1][y-1] == board[x -2][y-2 ] && x-2>=0 && y-2 >=0)
{
sum++;
x--;
y--;
}
return sum;
}
|
如果对你有帮助和启发,作者感到很高兴。
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。
原文链接:https://blog.csdn.net/whc18858/article/details/119981279