在用C语言实现三子棋小游戏之前,我们应当了解三子棋的游戏规则:在九宫格棋盘上,哪一方三个棋子连成一线(一行或一列或对角线)即判定哪一方胜利。
以下为源代码:
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
|
void menu() { printf ( "**********************************\n" ); printf ( "**********************************\n" ); printf ( "***** 0.play *************\n" ); printf ( "***** 1.exit *************\n" ); printf ( "**********************************\n" ); } menu(); do { printf ( "请输入要选的的选项: " ); scanf ( "%d" ,&input); switch (input) { case 0: game(); break ; case 1: printf ( "退出游戏" ); break ; default : printf ( "请输入要选的选项:\n" ); break ; } } while (input); |
2.创建一个二维数组board用来储存三子棋的元素,并对其进行初始化。
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] = ' ' ; } } } |
3.打印九宫格棋盘。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
|
void Displayboard( char board[ROW][COL], int row, int col) //打印棋盘 { int i; int j; for (i = 0; i < row; i++) { for (j = 0; j < col ; j++) { printf ( " %c " , board[i][j]); if (j == col - 1) continue ; printf ( "|" ); } printf ( "\n" ); if (i < row - 1) printf ( "-----------\n" ); } } |
4.使用Playermove和Computermove函数来对board数组进行更改,模拟玩家和电脑落子情况。
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
|
void Playermove( char board[ROW][COL]) //玩家输入坐标 { while (1) { int x = 0; int y = 0; printf ( "请玩家输入坐标" ); scanf_s( "%d %d" , &x, &y); if (x <= ROW && x >= 1 && y <= COL && y >= 1) //判断输入的坐标是否越界 { if (board[x - 1][y - 1] == ' ' ) //输入的坐标必须为空才可以落子 { board[x - 1][y - 1] = '*' ; break ; } else printf ( "坐标被占用,请重新输入\n" ); } else printf ( "坐标有误,请重新输入\n" ); } } void Computermove( char board[ROW][COL]) //电脑输入坐标 { int x = 0; int y = 0; while (1) { x = rand () % 3; //使用rand函数生成随机数并取余得到电脑落子坐标 y = rand () % 3; if (board[x][y] == ' ' ) { printf ( "电脑输入坐标%d %d\n" , x+1, y+1); board[x][y] = '#' ; break ; //打印后跳出循环 } } } |
5.玩家和电脑每落一次子,就要进行一次判断是否获胜,根据返回值来进行判断游戏结果。
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
|
int Iswin( char board[ROW][COL], int row, int col) //判断谁获胜 { int i; int j; //1为玩家获胜 0为电脑获胜 2为平局 for (i = 0; i < row; i++) { if (board[i][0] == board[i][1] && board[i][1] == board[i][2] && board[i][1] == '*' ) //判断同一行是否相等 return 1; else if (board[i][0] == board[i][1] && board[i][1] == board[i][2] && board[i][1] == '#' ) return 0; } for (j = 0; j < col; j++) { if (board[0][j] == board[1][j] && board[1][j] == board[2][j] && board[1][j] == '*' ) //判断同一列是否相等 return 1; else if (board[0][j] == board[1][j] && board[1][j] == board[2][j] && board[1][j] == '#' ) return 0; } if (board[0][0] == board[1][1] && board[1][1] == board[2][2] && board[1][1] == '*' ) //判断左对角线是否相等 return 1; else if (board[0][0] == board[1][1] && board[1][1] == board[2][2] && board[1][1] == '#' ) return 0; if (board[0][2] == board[1][1] && board[1][1] == board[2][0] && board[1][1] == '*' ) //判断右对角线是否相等 return 1; else if (board[0][2] == board[1][1] && board[1][1] == board[2][0] && board[1][1] == '#' ) return 0; if (Full(board, ROW, COL)) //判断是否平局 return 2; return 3; //返回3为继续进行游戏 } |
6.函数声明,宏定义,函数的定义实现,测试。
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
|
#include<stdio.h> #include<string.h> #include<time.h> #include<stdlib.h> #include"game.h" # define _CRT_SECURE_NO_WARNINGS #define ROW 3 #define COL 3 void Initboard( char board[ROW][COL], int row, int col); void Displayboard( char board[ROW][COL], int row, int col); void Playermove( char board[ROW][COL]); void Computer( char board[ROW][COL]); int Iswin( char board[ROW][COL], int row, int col); int Full( char board[ROW][COL], int row, int col); void menu() { printf ( "**********************************\n" ); printf ( "**********************************\n" ); printf ( "***** 0.play *************\n" ); printf ( "***** 1.exit *************\n" ); printf ( "**********************************\n" ); } void game() { char board[ROW][COL] = { 0 }; int ret; Initboard(board, ROW, COL); //用Initboard函数初始化数组,并用‘ '初始化数组 printf ( "游戏开始\n" ); while (1) { Playermove(board); //玩家输入坐标 Displayboard(board, ROW, COL); ret = Iswin(board, ROW, COL); //判断是否获胜 if (ret == 1) { printf ( "玩家获胜" ); break ; } else if (ret == 0) { printf ( "电脑获胜" ); break ; } else if (ret == 2) { printf ( "平局" ); break ; } printf ( "\n\n" ); Computer(board); //电脑输入坐标 Displayboard(board, ROW, COL); ret = Iswin(board, ROW, COL); //判断是否获胜 if (ret == 1) { printf ( "玩家获胜" ); break ; } else if (ret == 0) { printf ( "电脑获胜" ); break ; } else if (ret == 2) { printf ( "平局" ); break ; } printf ( "\n\n" ); } } int main() { int input = 0; srand ((unsigned int ) time (NULL)); menu(); do { printf ( "请输入要选的的选项: " ); scanf ( "%d" ,&input); switch (input) { case 0: game(); break ; case 1: printf ( "退出游戏" ); break ; default : printf ( "请输入要选的选项:\n" ); break ; } } while (input); } 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; int j; for (i = 0; i < row; i++) { for (j = 0; j < col ; j++) { printf ( " %c " , board[i][j]); if (j == col - 1) continue ; printf ( "|" ); } printf ( "\n" ); if (i < row - 1) printf ( "-----------\n" ); } } void Playermove( char board[ROW][COL]) //玩家输入坐标 { while (1) { int x = 0; int y = 0; printf ( "请玩家输入坐标" ); scanf_s( "%d %d" , &x, &y); if (x <= ROW && x >= 1 && y <= COL && y >= 1) { if (board[x - 1][y - 1] == ' ' ) { board[x - 1][y - 1] = '*' ; break ; } else printf ( "坐标被占用,请重新输入\n" ); } else printf ( "坐标有误,请重新输入\n" ); } } void Computer( char board[ROW][COL]) //电脑输入坐标 { int x = 0; int y = 0; while (1) { x = rand () % 3; y = rand () % 3; if (board[x][y] == ' ' ) { printf ( "电脑输入坐标%d %d\n" , x+1, y+1); board[x][y] = '#' ; break ; //打印后跳出循环 } } } int Iswin( char board[ROW][COL], int row, int col) //判断谁获胜 { int i; int j; //1为玩家获胜 0为电脑获胜 2为平局 for (i = 0; i < row; i++) { if (board[i][0] == board[i][1] && board[i][1] == board[i][2] && board[i][1] == '*' ) //判断同一行是否相等 return 1; else if (board[i][0] == board[i][1] && board[i][1] == board[i][2] && board[i][1] == '#' ) return 0; } for (j = 0; j < col; j++) { if (board[0][j] == board[1][j] && board[1][j] == board[2][j] && board[1][j] == '*' ) //判断同一列是否相等 return 1; else if (board[0][j] == board[1][j] && board[1][j] == board[2][j] && board[1][j] == '#' ) return 0; } if (board[0][0] == board[1][1] && board[1][1] == board[2][2] && board[1][1] == '*' ) //判断左对角线是否相等 return 1; else if (board[0][0] == board[1][1] && board[1][1] == board[2][2] && board[1][1] == '#' ) return 0; if (board[0][2] == board[1][1] && board[1][1] == board[2][0] && board[1][1] == '*' ) //判断右对角线是否相等 return 1; else if (board[0][2] == board[1][1] && board[1][1] == board[2][0] && board[1][1] == '#' ) return 0; if (Full(board, ROW, COL)) //判断是否平局 return 2; return 3; //返回3为继续进行游戏 } int Full( char board[ROW][COL], int row, int col) //判断棋盘是否下满 { int i; int j; for (i = 0; i < row; i++) { for (j = 0; j < col; j++) { if (board[i][j] == ' ' ) return 0; //此时棋盘未满 } } return 1; //此时棋盘已满 } |
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。
原文链接:https://blog.csdn.net/weixin_45177279/article/details/109192037