基于C语言实现五子棋游戏

时间:2022-11-07 14:51:05

采用二维数组制作五子棋,用坐标下棋。

请看代码。

基于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
include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<windows.h>
#include<conio.h>
#include<time.h>
 
void menu()//目录
{
 char xuanze = 0,xuanze2;
 printf("\n");
 printf("\n");
 printf("       #############################################\n");
 printf("           *五子棋小游戏*\n");
 printf("           *1.双人游戏*\n");
 printf("           *2.人机对战(暂未开放)\n");
 printf("           *3.游戏须知\n");
 printf("       #############################################\n");
 printf("          请选择选项,目前仅有 1 选项\n");
 printf("\n");
 printf("           ");
 xuanze = _getch();
 switch (xuanze)
 {
 case 1:
 break;
 case 3:
 printf("本游戏是键盘输入坐标来落子的,白棋先落,第二次落子为黑色,依次落子。\n");
 printf("若落子坐标非法则需重新落子。\n");
 printf("键盘输入1直接进入游戏\n");
 
 }
}
int t = 2;
int pan[20][20] =
{
 {1,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,3},
 {4,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,6},
 {4,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,6},
 {4,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,6},
 {4,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,6},
 {4,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,6},
 {4,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,6},
 {4,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,6},
 {4,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,6},
 {4,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,6},
 {4,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,6},
 {4,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,6},
 {4,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,6},
 {4,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,6},
 {4,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,6},
 {4,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,6},
 {4,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,6},
 {4,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,6},
 {4,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,6},
 {7,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,9}
};
void qp()//棋盘
{
 int i, j, k = 0;
 printf(" ");
 for (i = 0; i < 10; i++)
 printf(" %d", i + 1);
 for (i = 10; i < 20; i++)
 printf("%d", i + 1);
 printf("\n");
 printf("1 ");
 //printf("┌ ┬ ┐├ ┼ ┤└ ┴ ┘│ ");©○●┼
 for (i = 0; i < 20; i++)
 for (j = 0; j < 20; j++)
 {
 switch (pan[i][j])
 {
 case 1:
 printf("┌ ");
 k++;
 if (k % 20 == 0)
 {
  printf("\n");
 
 break;
 case 2:
 printf("┬ ");
 k++;
 if (k % 20 == 0)
  printf("\n");
 break;
 case 3:
 printf("┐ ");
 k++;
 if (k % 20 == 0)
 {
  printf("%d", i+1);
  printf("\n");
  printf("%d ", i + 1);
 }
 break;
 case 4:
 printf("├ ");
 k++;
 if (k % 20 == 0)
  printf("\n");
 break;
 case 5:
 printf("┼ ");
 k++;
 if (k % 20 == 0)
  printf("\n");
 break;
 case 6:
 printf("┤ ");
 k++;
 if (k % 20 == 0)
 {
  printf("%d", i+1);
  printf("\n");
  if(i<8)
  printf("%d ", i + 2);
  if(i>7)
  printf("%d", i + 2);
 
 break;
 case 7:
 printf("└ ");
 k++;
 if (k % 20 == 0)
  printf("\n");
 break;
 case 8:
 printf("┴ ");
 k++;
 if (k % 20 == 0)
  printf("\n");
 break;
 case 9:
 printf("┘ ");
 k++;
 if (k % 20 == 0)
 {
  printf("%d", i + 1);
  printf("\n"); 
 
 break;
 case 10:
 printf("○");
 k++;
 if (k % 20 == 0)
 {
  printf("%d", i+1);
  printf("\n");
 }
 break;
 case 11:
 printf("●");
 k++;
 if (k % 20 == 0)
 {
  printf("%d", i+1);
  printf("\n");
 }
 break;
 }
 }
 printf(" ");
 for (i = 0; i < 10; i++)
 printf(" %d", i + 1);
 for (i = 10; i < 20; i++)
 printf("%d", i + 1);
 printf("\n");
 printf("\n");
}
void play()
{
 int i, j;
 while (1)
 {
 printf("*请选择落子坐标(x,y): ");
 scanf_s("%d %d", &j, &i);
 printf("\n");
 if(i>0&&i<21&&j>0&&j<21)
 {
 if (pan[i - 1][j - 1] != 10 && pan[i - 1][j - 1] != 11)
 {
  if (t % 2 == 1)
  {
  t++;
  pan[i - 1][j - 1] = 11;
  break;
  }
  else if (t % 2 == 0)
  {
  t++;
  pan[i - 1][j - 1] = 10; 
  break;
  }
 }
 else if (pan[i-1][j-1] == 10 || pan[i-1][j-1] == 11)
 {
  printf("此处已经有棋子,请重新输入\n");
  continue;
 }
 }
 else if (i < 1 || i > 21 || j < 1 || j > 21)
 {
 printf("坐标不合法,请重新输入\n");
 continue;
 }
 }
}
void test()
{
 int i, j;
 for(i=0;i<20;i++)
 for (j = 0; j < 20; j++)
 {
 if (pan[i][j] != 1 || 2 || 3 || 4 || 5 || 6 || 7 || 8 || 9)
 {
 if (pan[i][j] == pan[i][j + 1]&&pan[i][j] == pan[i][j + 2]&&pan[i][j] == pan[i][j + 3]&&pan[i][j] == pan[i][j + 4])
 {
  if (pan[i][j] == 11)
  {
  printf("白棋获胜\n"); 
  
  if (pan[i][j] == 10)
  {
  printf("黑棋获胜\n"); 
  
 }
 if (pan[i][j] == pan[i + 1][j]&&pan[i][j] == pan[i + 2][j]&&pan[i][j] == pan[i + 3][j]&&pan[i][j] == pan[i + 4][j])
 {
  if (pan[i][j] == 11)
  {
  printf("白棋获胜\n"); 
  
  if (pan[i][j] == 10)
  {
  printf("黑棋获胜\n"); 
  
 }
 if (pan[i][j] == pan[i + 1][j + 1]&&pan[i][j] == pan[i + 2][j + 2]&&pan[i][j] == pan[i + 3][j + 3] &&pan[i][j]== pan[i + 4][j + 4])
 {
  if (pan[i][j] == 11)
  {
  printf("白棋获胜\n"); 
  
  if (pan[i][j] == 10)
  {
  printf("黑棋获胜\n"); 
  
 }
 if (pan[i][j] == pan[i - 1][j + 1]&&pan[i][j] == pan[i - 2][j + 2] &&pan[i][j]== pan[i - 3][j + 3]&&pan[i][j] == pan[i - 4][j + 4])
 {
  if (pan[i][j] == 11)
  {
  printf("白棋获胜\n"); 
  
  if (pan[i][j] == 10)
  {
  printf("黑棋获胜\n"); 
  }
 }
 }
 }
}
int main()
{
 system("mode con cols=98 lines=48");
 SetConsoleTitleA("五子棋小游戏"); //设置窗口
 menu();
 system("cls");
 while (1)
 {
 qp();
 test();
 play();
 system("cls");
 }
 system("pause");
 return 0;
}

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

原文链接:https://blog.csdn.net/may10/article/details/109448197