-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGame of the Amazons
621 lines (603 loc) · 15.8 KB
/
Game of the Amazons
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
614
615
616
617
618
619
620
621
#include <iostream>
#include <algorithm>
#include <string>
#include <cstdlib>
#include <ctime>
#include <fstream>
#define GRIDSIZE 8
#define OBSTACLE 2
#define grid_black 1
#define grid_white -1
#define MAX_VAL 65000
#define MIN_VAL -65000
using namespace std;
int currBotColor; // 我所执子颜色(1为黑,-1为白,棋盘状态亦同)
int gridInfo[GRIDSIZE][GRIDSIZE] = { 0 };
int temp[GRIDSIZE][GRIDSIZE] = { 0 };
int currRound = 0;
int black[4][2];
int white[4][2];
int dx[8] = { 1,0,1,1,0,-1,-1,-1 };
int dy[8] = { 1,1,-1,0,-1,1,0,-1 };
int dir[2][8] = { {1,1,1,0,0,-1,-1,-1},{0,1,-1,1,-1,0,1,-1} };
//***********************************************************************************************************************************//
bool inMap(int x, int y), isLined(int x0, int y0, int x1, int y1), isWay(int x0, int y0, int x1, int y1, int x2, int y2, int color);
bool isValid(int x0, int y0, int x1, int y1, int x2, int y2, int color), isDigit(string a), check_win(int coor[][2]);
void mainMenu(), pvpMenu(), pvcMenu(), pvpUI(int a), pvcUI(int a), computer_move(),scanPosition(),savedPvp(),savedPvc(),loadPvp(),loadPvc();
void procStep(int x0, int y0, int x1, int y1, int x2, int y2, int color), printTable();
//***********************************************************************************************************************************//
// 判断是否在地图内
bool inMap(int x, int y)
{
if (x < 0 || x >= GRIDSIZE || y < 0 || y >= GRIDSIZE)
return false;
return true;
}
//判断棋子与落点是否处于斜线
bool isLined(int x0, int y0, int x1, int y1)
{
if (abs(x1 - x0) == abs(y1 - y0))return true;
return false;
}
// 判断棋子的落点是否合法
bool isWay(int x0, int y0, int x1, int y1, int x2, int y2, int color)
{
if (color == OBSTACLE && x1 == x2 && y1 == y2)return true;
if (x0 == x1 && y0 == y1)return false; //如果重叠
if (gridInfo[y1][x1] != 0)return false; //如果已经被占领
if (x0 == x1) //直线
{
if (y1 - y0 > 0)
{
for (int i = y0 + 1; i <= y1; i++)
{
if (gridInfo[i][x0] != 0)return false;
}
}
else
{
for (int i = y1; i < y0; i++)
{
if (gridInfo[i][x0] != 0)return false;
}
}
}
if (y0 == y1) //横线
{
if (x1 - x0 > 0)
{
for (int i = x0 + 1; i <= x1; i++)
{
if (gridInfo[y0][i] != 0)return false;
}
}
else
{
for (int i = x1; i < x0; i++)
{
if (gridInfo[y0][i] != 0)return false;
}
}
}
if (x1 != x0 && y1 != y0 && !isLined(x0, y0, x1, y1))return false;
int gap = abs(y1 - y0);
if (x1 - x0 > 0 && y1 - y0 > 0)
{
for (int i = 1; i <= gap; i++)
{
if (gridInfo[y0 + i][x0 + i] != 0)return false;
}
}
if (x1 - x0 > 0 && y1 - y0 < 0)
{
for (int i = 1; i <= gap; i++)
{
if (gridInfo[y0 - i][x0 + i] != 0)return false;
}
}
if (x1 - x0 < 0 && y1 - y0 > 0)
{
for (int i = 1; i <= gap; i++)
{
if (gridInfo[y0 + i][x0 - i] != 0)return false;
}
}
if (x1 - x0 < 0 && y1 - y0 < 0)
{
for (int i = 1; i <= gap; i++)
{
if (gridInfo[y0 - i][x0 - i] != 0)return false;
}
}
return true;
}
// 检查步法是否合法
bool isValid(int x0, int y0, int x1, int y1, int x2, int y2, int color)
{
if ((!inMap(x0, y0)) || (!inMap(x1, y1)) || (!inMap(x2, y2))) //任何一点不在棋盘内的话,就返回false
return false;
if (gridInfo[y0][x0] != color || gridInfo[y1][x1] != 0) //自己的棋子不在选择的起点上或者要去的地方已经被占领了,就返回false
return false;
if ((gridInfo[y2][x2] != 0) && !(x2 == x0 && y2 == y0)) //选择的障碍位置已经被占领,除非障碍位置是自己的起始位置
return false;
if (!(isWay(x0, y0, x1, y1, x2, y2, color) && isWay(x1, y1, x2, y2, x0, y0, OBSTACLE))) //如果是非法步法,返回false
return false;
return true;
}
// 在坐标处落子
void procStep(int x0, int y0, int x1, int y1, int x2, int y2, int color)
{
gridInfo[y0][x0] = 0;
gridInfo[y1][x1] = color;
gridInfo[y2][x2] = OBSTACLE;
}
// 绘制棋盘
void printTable()
{
system("cls");
int x1 = 0, x2 = 0, y1 = 0, y2 = 0;
cout << endl << endl;
cout << " Black : @ White : O" << endl;
cout << " ==================================" << endl;
cout << " 0 1 2 3 4 5 6 7 " << endl;
cout << " ==================================" << endl;
for (int i = 0; i < GRIDSIZE; i++)
{
cout << " " << i << " |";
for (int j = 0; j < GRIDSIZE; j++)
{
if (gridInfo[i][j] == grid_black)cout << " @ |";
else if (gridInfo[i][j] == grid_white)cout << " O |";
else if (gridInfo[i][j] == OBSTACLE)cout << " # |";
else cout << " |";
}
cout << endl << " ==================================" << endl;
}
cout << " ==================================" << endl;
cout << " Round " << currRound << " ";
cout << (currBotColor == grid_white ? " White's turn" : " Black's turn") << endl;
cout << " ==================================" << endl;
}
// 判断输入是否合法
bool isDigit(string a)
{
if (a.length() == 1 && a[0] >= '0' && a[0] <= '9')return true;
return false;
}
// 获取黑白棋的位置
void scanPosition()
{
int b1 = 0;
int w1 = 0;
for (int i = 0; i < GRIDSIZE; i++)
{
for (int j = 0; j < GRIDSIZE; j++)
{
if (gridInfo[i][j] == grid_black)
{
black[b1][0] = i;
black[b1++][1] = j;
}
else if (gridInfo[i][j] == grid_white)
{
white[w1][0] = i;
white[w1++][1] = j;
}
}
}
}
// 判断游戏结束
bool check_win(int coor[][2])
{
for (int i = 0; i < 4; i++)
{
for (int j = 0; j < 8; j++)
{
int y1 = coor[i][0] + dir[0][j];
int x1 = coor[i][1] + dir[1][j];
if (!(!inMap(x1,y1) || gridInfo[y1][x1] != 0))
{
return false;
}
}
}
return true;
}
// 初始化棋盘
void resetGame()// 初始化棋盘
{
currRound = 0;
memset(gridInfo, 0, sizeof(gridInfo));
gridInfo[0][2] = gridInfo[2][0] = gridInfo[5][0] = gridInfo[7][2] = grid_black;
gridInfo[0][5] = gridInfo[2][7] = gridInfo[5][7] = gridInfo[7][5] = grid_white;
}
// 界面
void mainMenu()
{
system("CLS");
cout << endl;
cout << endl;
cout << " =================================" << endl;
cout << " | Welcome to Amazons! |" << endl;
cout << " |===============================|" << endl;
cout << " | GAMEPLAY MODE INPUT |" << endl;
cout << " | |" << endl;
cout << " | Player vs Player 1 |" << endl;
cout << " | Player vs Bot 2 |" << endl;
cout << " | Exit 3 |" << endl;
cout << " | |" << endl;
cout << " =================================" << endl;
cout << " Please Enter:";
string n;
cin >> n;
if (n == "1")return pvpMenu();
else if (n == "2")return pvcMenu();
else if (n == "3")return;
else return mainMenu();
}
void pvpMenu()
{
system("CLS");
cout << endl;
cout << endl;
cout << " =================================" << endl;
cout << " | Player vs Player |" << endl;
cout << " |===============================|" << endl;
cout << " | |" << endl;
cout << " | New Game 1 |" << endl;
cout << " | Load Game 2 |" << endl;
cout << " | Return 3 |" << endl;
cout << " | |" << endl;
cout << " =================================" << endl;
cout << " Please Enter: ";
string n;
cin >> n;
if (n == "1")
{
resetGame();
return pvpUI(1);
}
else if (n == "2")
{
loadPvp();
return pvpUI(2);
}
else if (n == "3")return mainMenu();
return pvpMenu();
}
void pvcMenu()
{
system("CLS");
cout << endl;
cout << endl;
cout << " =================================" << endl;
cout << " | Player vs Bot |" << endl;
cout << " |===============================|" << endl;
cout << " | |" << endl;
cout << " | New Game 1 |" << endl;
cout << " | Load Game 2 |" << endl;
cout << " | Return 3 |" << endl;
cout << " | |" << endl;
cout << " =================================" << endl;
cout << " Please Enter: ";
string n;
cin >> n;
if (n == "1")
{
resetGame();
return pvcUI(1);
}
else if (n == "2")
{
loadPvc();
return pvcUI(2);
}
else if (n == "3")return mainMenu();
return pvcMenu();
}
void pvpUI(int a)
{
if (a == 1)
{
system("cls");
cout << " Player vs Player " << endl;
cout << " ==================================" << endl;
cout << " White: 1 Black: 2 " << endl;
cout << " ==================================" << endl;
cout << " Please Select Your Side: ";
string n;
cin >> n;
if (n == "1")currBotColor = grid_white;
else if (n == "2")currBotColor = grid_black;
else
{
cout << " Invalid Input!" << endl;
system("pause");
system("cls");
return pvpUI(1);
}
}
while (true)
{
string temp1, temp2;
bool flag = true;
int coor[2][3] = { 0 };
system("cls");
printTable();
cout << " Please Enter:";
for (int i = 0; i < 3; i++)
{
cin >> temp1;
if (temp1 == "reset")
{
system("cls");
cout << " ==================================" << endl;
cout << " The game is resetted. " << endl;
cout << " ==================================" << endl;
system("pause");
resetGame();
flag = false;
break;
}
else if (temp1 == "menu")return mainMenu();
else if (temp1 == "save")
{
savedPvp();
return mainMenu();
}
cin >> temp2;
if (!isDigit(temp1) || !isDigit(temp2))
{
cout << " ==================================" << endl;
cout << " Invalid Input! " << endl;
cout << " ==================================" << endl;
flag = false;
break;
}
coor[0][i] = temp1[0] - '0';
coor[1][i] = temp2[0] - '0';
}
if (!flag)continue;
if (isValid(coor[0][0], coor[1][0], coor[0][1], coor[1][1], coor[0][2], coor[1][2], currBotColor))
{
procStep(coor[0][0], coor[1][0], coor[0][1], coor[1][1], coor[0][2], coor[1][2], currBotColor);
currBotColor = -1 * currBotColor;
currRound++;
scanPosition();
if (check_win(white))
{
system("cls");
printTable();
cout << " ==================================" << endl;
cout << " White Wins! " << endl;
cout << " ==================================" << endl;
break;
}
else if (check_win(black))
{
system("cls");
printTable();
cout << " ==================================" << endl;
cout << " Black Wins! " << endl;
cout << " ==================================" << endl;
break;
}
}
else
{
cout << " ==================================" << endl;
cout << " Invalid Move! " << endl;
cout << " ==================================" << endl;
system("pause");
}
}
system("pause");
return mainMenu();
}
void pvcUI(int a)
{
if (a == 1)
{
system("cls");
cout << " Player vs Bot " << endl;
cout << " ==================================" << endl;
cout << " White: 1 Black: 2 " << endl;
cout << " ==================================" << endl;
cout << " Please Select Your Side: ";
string n;
cin >> n;
if (n == "1")currBotColor = grid_white;
else if (n == "2")currBotColor = grid_black;
else
{
cout << " Invalid Input!" << endl;
system("pause");
return pvcUI(1);
}
}
int det = 0;
if (currBotColor == grid_black)det = 1;
while (true)
{
if (det % 2 == 0)
{
system("cls");
printTable();
string temp1, temp2;
bool flag = true;
int coor[2][3] = { 0 };
cout << " Please Enter:";
for (int i = 0; i < 3; i++)
{
cin >> temp1;
if (temp1 == "reset")
{
system("cls");
cout << " ==================================" << endl;
cout << " The game is resetted. " << endl;
cout << " ==================================" << endl;
resetGame();
flag = false;
break;
}
else if (temp1 == "menu")return mainMenu();
else if (temp1 == "save")
{
savedPvc();
return mainMenu();
}
cin >> temp2;
if (!isDigit(temp1) || !isDigit(temp2))
{
cout << " ==================================" << endl;
cout << " Invalid Input! " << endl;
cout << " ==================================" << endl;
flag = false;
break;
}
coor[0][i] = temp1[0] - '0';
coor[1][i] = temp2[0] - '0';
}
if (!flag)continue;
if (isValid(coor[0][0], coor[1][0], coor[0][1], coor[1][1], coor[0][2], coor[1][2], currBotColor))
{
procStep(coor[0][0], coor[1][0], coor[0][1], coor[1][1], coor[0][2], coor[1][2], currBotColor);
det++;
currRound++;
scanPosition();
if (check_win(white))
{
system("cls");
printTable();
cout << " ==================================" << endl;
cout << " White Wins! " << endl;
cout << " ==================================" << endl;
break;
}
else if (check_win(black))
{
system("cls");
printTable();
cout << " ==================================" << endl;
cout << " Black Wins! " << endl;
cout << " ==================================" << endl;
system("pause");
break;
}
}
else
{
cout << " ==================================" << endl;
cout << " Invalid Move! " << endl;
cout << " ==================================" << endl;
system("pause");
}
}
else
{
computer_move();
currRound++;
det++;
scanPosition();
if (check_win(white))
{
system("cls");
printTable();
cout << " ==================================" << endl;
cout << " White Wins! " << endl;
cout << " ==================================" << endl;
break;
}
else if (check_win(black))
{
system("cls");
printTable();
cout << " ==================================" << endl;
cout << " Black Wins! " << endl;
cout << " ==================================" << endl;
break;
}
}
}
return mainMenu();
}
// Crys
void savedPvc()
{
fstream outfile("pvcmap.txt");
for (int i = 0; i < GRIDSIZE; i++)
{
for (int j = 0; j < GRIDSIZE; j++)
{
outfile << gridInfo[i][j] << " ";
}
outfile << endl;
}
outfile << currBotColor << endl;
outfile << currRound << endl;
outfile.close();
}
void savedPvp()
{
fstream outfile("pvpmap.txt");
for (int i = 0; i < GRIDSIZE; i++)
{
for (int j = 0; j < GRIDSIZE; j++)
{
outfile << gridInfo[i][j] << " ";
}
outfile << endl;
}
outfile << currBotColor << endl;
outfile << currRound << endl;
outfile.close();
}
void loadPvc()
{
fstream infile("pvcmap.txt", ios::in);
for (int i = 0; i < GRIDSIZE; i++)
{
for (int j = 0; j < GRIDSIZE; j++)
{
infile >> gridInfo[i][j];
}
}
infile >> currBotColor;
infile >> currRound;
infile.close();
}
void loadPvp()
{
fstream infile("pvpmap.txt", ios::in);
for (int i = 0; i < GRIDSIZE; i++)
{
for (int j = 0; j < GRIDSIZE; j++)
{
infile >> gridInfo[i][j];
}
}
infile >> currBotColor;
infile >> currRound;
infile.close();
}
// 算法
void computer_move()
{
for (int x0 = 0; x0 < GRIDSIZE; x0++)
for (int y0 = 0; y0 < GRIDSIZE; y0++)
for (int x1 = 0; x1 < GRIDSIZE; x1++)
for (int y1 = 0; y1 < GRIDSIZE; y1++)
for (int x2 = 0; x2 < GRIDSIZE; x2++)
for (int y2 = 0; y2 < GRIDSIZE; y2++)
{
if (isValid(x0, y0, x1, y1, x2, y2, -1 * currBotColor))
{
procStep(x0, y0, x1, y1, x2, y2, -1 * currBotColor);
return;
}
}
}
int main()
{
mainMenu();
}