-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path五子棋游戏
455 lines (416 loc) · 9.54 KB
/
五子棋游戏
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
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <stdarg.h>
#define MAXSIZE 19
#define MAXCOUNT 19*19
#define EMPTY 0
#define WHITE 1
#define BLACK 2
#define NOTEXIST 3
#define FALSE 0
#define TRUE 1
// test
// #define board.value board[columnToNumber(x + 1)][y + 1][0]
// #define board.seq board[columnToNumber(x + 1)][y + 1][1]
int board[MAXSIZE][MAXSIZE][2];
int board2[MAXSIZE][MAXSIZE][2];
int boardWidth = 19, boardLength = 19;
int boardSize = 19;
FILE *fp;
// board
void initBoard(); //finish
void drawBoardTest(); //finish
void drawColumnNum(); //finish
void drawRow(); //finish
void drawInterval(); //finish
void drawBoard(); //finish
// game
void play();
void menu();
void saveData();
void loadData();
void loadData2();
void load();
void newgame();
void save();
void replay();
int judge(int i);
int isNum(char* str);
int toLetter(int num);
int columnToNumber(char num);
int main(int argc, char **argv) {
int board[MAXSIZE][MAXSIZE][2];
int boardWidth = 5, boardLength = 5;
FILE *fp;
if(argc == 1) {
menu();
} else if(argc - 1 == 2){
if(strcmp(argv[1], "goban") == 0) {
if(isNum(argv[2])) {
int size = atoi(argv[2]);
if(size < MAXSIZE) {
boardSize = boardWidth = boardLength = size;
newgame();
return 0;
} else {
printf("error: board size so big!\n");
return 0;
}
} else {
printf("error: board size should be a number!\n");
return 0;
}
} else if(strcmp(argv[1], "load") == 0) {
if((fp = fopen(argv[2], "r")) == NULL) {
printf("error: the files %s can not be opened.\n", argv[2]);
return 0;
}
loadData2(argv[2]);
} else {
printf("error: paramaters!\n");
return 0;
}
} else if(strcmp(argv[1], "-h") == 0) {
;
} else {
printf("error: too much paramaters!\n");
return 0;
}
// initBoard(boardWidth, boardLength, board);
// drawBoard2(board);
//play();
return 0;
}
// function about board
int toLetter(int num) {
return num + 65;
}
char toStone(int n) {
switch(n){
case BLACK:
return 'X';
break;
case WHITE:
return 'O';
break;
case EMPTY:
return '.';
break;
}
}
char toStone2(int n) {
switch(n) {
case EMPTY: {
return '+';
break;
}
case WHITE: {
return 'O';
break;
}
case BLACK: {
return 'X';
break;
}
}
}
void initBoard() {
for(int i = 0; i < MAXSIZE; i++) {
for(int j = 0; j < MAXSIZE; j++) {
board[i][j][0] = NOTEXIST;
}
}
for(int i = 0; i < boardWidth; i++) {
for(int j = 0; j < boardLength; j++) {
board[i][j][0] = EMPTY;
}
}
}
void initBoard2() {
for(int i = 0; i < MAXSIZE; i++) {
for(int j = 0; j < MAXSIZE; j++) {
board2[i][j][0] = NOTEXIST;
}
}
for(int i = 0; i < boardWidth; i++) {
for(int j = 0; j < boardLength; j++) {
board2[i][j][0] = EMPTY;
}
}
}
void drawBoardTest() {
for(int i = 0; i < MAXSIZE; i++) {
for(int j = 0; j < MAXSIZE; j++) {
printf("%d", board[i][j][0]);
}
printf("\n");
}
}
void drawColumnNum() {
for(int i = 0; i < boardSize; i++) {
if(i < 8) {
printf(" %c", i + 65);
} else {
printf(" %c", i + 66);
}
}
printf("\n");
}
void drawRow(int r) {
printf("%2d %c", boardSize - r, toStone2(board[r][0][0]));
for(int i = 1; i < boardSize; i++) {
printf("---%c", toStone2(board[r][i][0]));
}
printf("%3d\n", boardSize - r);
}
void drawRow2(int r) {
printf("%2d %c", boardSize - r, toStone2(board2[r][0][0]));
for(int i = 1; i < boardSize; i++) {
printf("---%c", toStone2(board2[r][i][0]));
}
printf("%3d\n", boardSize - r);
}
void drawInterval() {
for(int i = 0; i < boardSize; i++) {
printf(" |");
}
printf("\n");
}
void drawBoard() {
drawColumnNum();
drawRow(0);
for(int i = 1; i < boardSize; i++) {
drawInterval();
drawRow(i);
}
drawColumnNum();
}
void drawBoard2() {
drawColumnNum();
drawRow2(0);
for(int i = 1; i < boardSize; i++) {
drawInterval();
drawRow2(i);
}
drawColumnNum();
}
// function about game
void load() {
loadData();
menu();
}
void loadData() {
char name[20];
initBoard();
printf("enter you save file's name: \n");
// setbuf(stdin, NULL);
scanf("%s", name);
if((fp = fopen(name, "r")) == NULL) {
printf("error, cannot open or create the file.\n");
return;
}
fscanf(fp, "%d\n", &boardSize);
for(int i = 0; i < boardSize; i++) {
for(int j = 0; j < boardSize; j++) {
fscanf(fp, "%d", &board[i][j][0]);
fscanf(fp, "%d", &board[i][j][1]);
}
}
fclose(fp);
}
void loadData2(char name[]) {
// char name[20];
initBoard();
// printf("enter you save file's name: \n");
// setbuf(stdin, NULL);
// scanf("%s", name);
if((fp = fopen(name, "r")) == NULL) {
printf("error, cannot open or create the file.\n");
return;
}
fscanf(fp, "%d\n", &boardSize);
for(int i = 0; i < boardSize; i++) {
for(int j = 0; j < boardSize; j++) {
fscanf(fp, "%d", &board[i][j][0]);
fscanf(fp, "%d", &board[i][j][1]);
}
}
fclose(fp);
menu();
}
void menu() {
char action[10];
printf("please enter your action : \n");
printf("play: play a new game.\n");
printf("load: load a game.\n");
printf("replay: replay a game.\n");
printf("continue: continue a loaded game.\n");
printf("quit: quit the progame.\n");
scanf("%s", action);
// fgets(action, 10, stdin);
// gets(action);
// printf("%s\n", action);
if(strcmp(action, "play") == 0) {
newgame();
}
if(strcmp(action, "load") == 0) {
load();
}
if(strcmp(action, "replay") == 0) {
replay();
}
if(strcmp(action, "quit") == 0) {
exit(0);
}
if(strcmp(action, "continue") == 0) {
play();
}
}
void newgame() {
initBoard();
play();
}
// int toLetter(int num) {
// return num + 65;
// }
int columnToNumber(char num) {
if(num >= 'I') {
return (int)num - 66;
} else {
return (int)num - 65;
}
}
void play() {
drawBoard();
int num = boardSize * boardSize;
char x;
int y;
for(int i = 0; i < num; i++) {
flag:
printf("enter your position��or 's' to save data or 'q' to back to main menu: ");
setbuf(stdin, NULL);
scanf("%c", &x);
if(x == 'q' || x == 'Q') {
menu();
} else if(x == 's' || x == 'S') {
save();
}
scanf("%d", &y);
if((columnToNumber(x - 1) > boardSize || y > boardSize || board[y - 1][columnToNumber(x - 1)][0] != EMPTY)) {
goto flag;
}
if(i % 2 == 0) {
board[boardSize - y][columnToNumber(x)][0] = BLACK;
board[boardSize - y][columnToNumber(x)][1] = i;
} else {
board[boardSize - y][columnToNumber(x)][0] = WHITE;
board[boardSize - y][columnToNumber(x)][1] = i;
}
drawBoard();
printf("last stone's position: (%c, %d)\n", x, y);
// drawBoardTest();
if(judge(i)) {
if(i % 2 == 0) {
printf("black is win!\n");
} else {
printf("white is win!\n");
}
break;
}
}
menu();
}
int judge(int i) {
// printf("num = %d\n", i);
int a,b;
int color = i % 2 == 0 ? BLACK : WHITE;
// int color;
// if(i % 2 == 0) {
// color = BLACK;
// } else {
// color = WHITE;
// }
for(int i = 0; i < boardSize; i++) {
for(int j = 0; j < boardSize - 4; j++) {
if(board[i][j][0] == color && board[i][j + 1][0] == color && board[i][j + 2][0] == color && board[i][j + 3][0] == color && board[i][j + 4][0] == color) {
return 1;
}
}
}
for(int i = 0; i < boardSize - 4; i++) {
for(int j = 0; j < boardSize; j++) {
if(board[i][j][0] == color && board[i + 1][j][0] == color && board[i + 2][j][0] == color && board[i + 3][j][0] == color && board[i + 4][j][0] == color) {
return 1;
}
}
}
for(int i = 0; i < boardSize - 4; i++) {
for(int j = 0; j < boardSize - 4; j++) {
if(board[i][j][0] == color && board[i + 1][j + 1][0] == color && board[i + 2][j + 2][0] == color && board[i + 3][j + 3][0] == color && board[i + 4][j + 4][0] == color) {
return 1;
}
}
}
for(int i = 0; i < boardSize - 4; i++) {
for(int j = boardSize - 1; j > 3; j--) {
if(board[i][j][0] == color && board[i + 1][j - 1][0] == color && board[i + 2][j - 2][0] == color && board[i + 3][j - 3][0] == color && board[i + 4][j - 4][0] == color) {
return 1;
}
}
}
return 0;
}
void save() {
saveData();
menu();
}
void saveData() {
char name[20];
printf("enter save file's name: \n");
scanf("%s", name);
if((fp = fopen(name, "wt+")) == NULL) {
printf("error, cannot open or create the file.\n");
return;
}
fprintf(fp, "%d\n", boardSize);
for(int i = 0; i < MAXSIZE; i++) {
for(int j = 0; j < MAXSIZE; j++) {
fprintf(fp,"%d ", board[i][j][0]);
fprintf(fp,"%d ", board[i][j][1]);
}
}
fclose(fp);
}
void replay() {
initBoard2();
int num = 0;
int c;
for(int i = 0; i < MAXSIZE; i++) {
for(int j = 0; j < MAXSIZE; j++) {
if(board[i][j][0] != EMPTY && board[i][j][0] != NOTEXIST) {
if(board[i][j][1] == num) {
board2[i][j][0] = board[i][j][0];
board2[i][j][1] = board[i][j][1];
drawBoard2();
num++;
printf("put enter to continue: \n");
if((c = getchar()) != '\n') {
return;
} else {
continue;
}
}
}
}
}
}
int isNum(char *str) {
for(int i = 0; str[i] != '\0'; i++) {
if(str[i] > '9' || str[i] < '0') {
printf("%c ", str[i]);
return 0;
}
}
return 1;
}