-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcmdChess.cpp
393 lines (356 loc) · 14.1 KB
/
cmdChess.cpp
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
#include"cmdChess.h"
//cmd显示胜利者及 A B胜利次数
void GameBoard::WinnerCount(int x)
{
if (x == 1) {
A++;
std::cout << "***** Player A win! *****" << '\n';
}
if (x == 2) {
B++;
std::cout << "***** Player B win! *****" << '\n';
}
std::cout<< "Win Time Counts | A : B " << A << ":" << B << '\t';
}
//cmd显示游戏开始界面
void GameBoard::GameStart()
{
std::cout << "_____________Start Game Gobang_________" << '\n';
std::cout << " " << '\n';
std::cout << "_________ 1.enter game : E _____" << '\n';
std::cout << "_________ 2.quit game : Q _____" << '\n';
std::cout << "_________ 3.chose model: M ____" << '\n';
std::cout << "Model:Human or Computer | Default Config:Computer"<<'\n';
std::cout << "_______________________________________" << '\n';
std::cout << "Please input instruction:" << '\n';
}
//更新棋盘
void GameBoard::RenewBoard()
{
std::cout << "plaese entry - x - y " << '\n';
for (int i = 1; i < N + 1; i++)
{
std::cout << setw(6) << setfill(' ') << right << i;
}
std::cout << '\n';
std::cout << " |";
for (int i = 1; i < N + 1; i++) { std::cout << "-----|"; }//输出第一行
std::cout << '\n';
for (int i = 1; i < N + 1; i++)
{
for (int j = 0; j < N + 1; j++)
{
if (j == 0) {
std::cout << setw(2) << setfill(' ') << left << i << '|';
}else {
std::cout << " " << chessBoard[i][j] << " |";
}
}
std::cout << '\n';
std::cout << " |";
for (int i = 1; i < N + 1; i++) { std::cout << "-----|";} //输出最后一行
std::cout << '\n';
}
}
//初始化chessBoard为全' '字符串数组
void GameBoard::initchessBoard()
{
for (int i = 0; i < N + 1; i++)
for (int j = 0; j < N + 1; j++)
{
chessBoard[i][j] = ' ';
}
}
void GameBoard::Chesspainter(int x, int y) //下棋
{
if (-1 == check(x, y))
{
std::cout << "invalid coordinate!please input again!"<<'\n';
}
else if (0 == check(x, y))
{
std::cout << "already input x y ,dont input again!" << '\n';
}
else
{
system("CLS");
if (flag == 0)
{
chessBoard[x][y] = 'x';
std::cout << "Palyer " << "Last action by Player A with 'x' is: " << "X: " << x << " Y: " << y << '\n';
flag = 1;
}
else if (flag == 1)
{
chessBoard[x][y] = 'o';
std::cout << "Palyer " << "Last action by Player B with 'o' is: " << "X: " << x << " Y: " << y << '\n';
flag = 0;
}
RenewBoard();
}
}
/*
* 检查输入坐标合法性
* @para: int x,y
* @return:int -1超限 0已占 1合法
*/
int GameBoard::check(int x, int y)
{
if (x > N + 1 || y > N + 1 || x < 1 || y < 1)
{
return -1;
}
if (chessBoard[x][y] != ' ')
{
return 0;
}
return 1;
}
/*
* 判断获胜和棋盘下满
* @para:int i,j char flag
* @return: int
*/
int GameBoard::CheckBoard(int i,int j ,char flag)
{
int x = i, y = j;
int begin = 0;
int end = 0;
int begin1 = 0;
int end1 = 0;
//判断列是否满足条件
(i - 4) > 0 ? begin = i - 4 : begin = 1; //已经将列减4 或者从边界算
(i + 4) < N ? end = N : end = (i + 4);
for (j, i = begin; i + 4 <= end; i++)
{
if (chessBoard[i][j] == flag&&chessBoard[i + 1][j] == flag&&chessBoard[i + 2][j] == flag&&chessBoard[i + 3][j] == flag&&chessBoard[i + 4][j] == flag)
{
return 1;
}
}
//判断行是否满足条件
i = x, j = y;
(j - 4) > 0 ? begin = j - 4 : begin = 1; //已经将横坐标减4
(j + 4) < N ? end = N : end = (j + 4); //如果末尾大于N则,从N算最后一个
for (i, j = begin; j + 4 <= end; j++)
{
if (chessBoard[i][j] == flag&&chessBoard[i][j + 1] == flag&&chessBoard[i][j + 2] == flag&&chessBoard[i][j + 3] == flag&&chessBoard[i][j + 4] == flag)
{
return 1;
}
}
//判断正对角线是否满足
int len = 0;
i = x, j = y;
i >j ? len = j - 1 : len = i - 1; //判断i>j的作用,按照离边距小的坐标减做初始值
if (len > 4) len = 4;
begin = i - len; //横坐标的起始位置
begin1 = j - len; //纵坐标,,因为是正斜边所以两边要减相同数
i > j ? len = (N - i) : len = (N - j); //结束坐标也要按照距离边界近的点去减
if (len>4) len = 4;
end= i + len; //横坐标的结束位置
end1 = j + len; //纵坐标的结束位置
for (int i = begin, j = begin1; (i + 4 < end) && (j + 4 < end1); i++, j++)
{
if (chessBoard[i][j] == flag&&chessBoard[i + 1][j + 1] == flag&&chessBoard[i + 2][j + 2] == flag&&chessBoard[i + 3][j + 3] == flag&&chessBoard[i + 4][j + 4] == flag)
{
return 1;
}
}
//判断负对角线是否满足
i = x, j = y;
(i - 1) >(N - j) ? len = (N - j) : len = i - 1;//判断负对角线坐标的位置是否在上下减小的距离
if (len > 4) len = 4;
begin = i - len; //横坐标的起始位置
begin1 = j + len; //纵坐标的起始位置
(N - i) > (j - 1) ? len = (j - 1) : len = (N - i);
if (len>4) len = 4;
end = i + len; //横坐标的结束位置
end1 = j - len; //纵坐标的结束位置
for (int i = begin, j = begin1; (i + 4 < end) && (j - 4 > end1); i++, j--)
{
if (chessBoard[i][j] == flag&&chessBoard[i + 1][j - 1] == flag&&chessBoard[i + 2][j - 2] == flag&&chessBoard[i + 3][j - 3] == flag&&chessBoard[i + 4][j - 4] == flag)
{
return 1;
}
}
//判断棋盘是否下满 遍历chessBoard[][]
for (int l = 1; l < N + 1; l++) //棋盘有没有下满
{
for (int c = 1; c < N + 1; c++)
{
if (chessBoard[l][c] == ' ')
return -1; //说明棋盘中还有空格
}
}
return 0;
}
void GameBoard::calculateScore(){
int personNum = 0; // 玩家连成子的个数
int botNum = 0; // AI连成子的个数
int emptyNum = 0; // 各方向空白位的个数
scoreBoard[N+1][N+1]={0};
for (int row = 0; row < N; row++)
for (int col = 0; col < N; col++){
if (row > 0 && col > 0 &&
chessBoard[row][col] == ' ')
{
for (int y = -1; y <= 1; y++)
for (int x = -1; x <= 1; x++)
{
personNum = 0;
botNum = 0;
emptyNum = 0;
if (!(y == 0 && x == 0))
{
// 每个方向延伸4个子
for (int i = 1; i <= 4; i++)
{
if (row + i * y > 0 && row + i * y < N &&
col + i * x > 0 && col + i * x < N &&
chessBoard[row + i * y][col + i * x] == 'x')
{
personNum++;
}else if (row + i * y > 0 && row + i * y < N &&
col + i * x > 0 && col + i * x < N &&
chessBoard[row + i * y][col + i * x] == ' ')
{
emptyNum++;
break;
}
else
{
break;
}
}
for (int i = 1; i <= 4; i++)
{
if (row - i * y > 0 && row - i * y < N &&
col - i * x > 0 && col - i * x < N &&
chessBoard[row - i * y][col - i * x] == 'x')
{personNum++;}
else if(row - i * y > 0 && row - i * y < N &&
col - i * x > 0 && col - i * x < N &&
chessBoard[row - i * y][col - i * x] == ' ')
{
emptyNum++;
break;
}
else
{
break;
}
}
switch (personNum)
{
case 1:
scoreBoard[row][col]+=10; break;
case 2:
if (emptyNum == 1)
scoreBoard[row][col] += 30;
else if (emptyNum == 2)
scoreBoard[row][col] += 40;
break;
case 3:
if (emptyNum == 1)
scoreBoard[row][col] += 60;
else if (emptyNum == 2)
scoreBoard[row][col] += 110;
break;
case 4:
scoreBoard[row][col]+=10100; break;
default:
break;
}
emptyNum=0;
for (int i = 1; i <= 4; i++)
{
if (row + i * y > 0 && row + i * y < N &&
col + i * x > 0 && col + i * x < N &&
chessBoard[row + i * y][col + i * x] == 'x') // 玩家的子
{
botNum++;
}
else if (row + i * y > 0 && row + i * y < N &&
col + i * x > 0 && col + i * x < N &&
chessBoard[row +i * y][col + i * x] == ' ') // 空白位
{
emptyNum++;
break;
}
else // 出边界
break;
}
for (int i = 1; i <= 4; i++)
{
if (row - i * y > 0 && row - i * y < N &&
col - i * x > 0 && col - i * x < N &&
chessBoard[row - i * y][col - i * x] == ' ') // AI的子
{
botNum++;
}
else if (row - i * y > 0 && row - i * y < N &&
col - i * x > 0 && col - i * x < N &&
chessBoard[row - i * y][col - i * x] == ' ') // 空白位
{
emptyNum++;
break;
}
else // 出边界
break;
}
if (botNum == 0) // 普通下子
scoreBoard[row][col] += 5;
else if (botNum == 1) // 活二
scoreBoard[row][col] += 10;
else if (botNum == 2)
{
if (emptyNum == 1) // 死三
scoreBoard[row][col] += 25;
else if (emptyNum == 2)
scoreBoard[row][col] += 50; // 活三
}
else if (botNum == 3)
{
if (emptyNum == 1) // 死四
scoreBoard[row][col] += 55;
else if (emptyNum == 2)
scoreBoard[row][col] += 100; // 活四
}
else if (botNum >= 4)
scoreBoard[row][col] += 10000; // 活五
}
}
}
}
}
void GameBoard::actionByComputer(int &row,int &col){
calculateScore();
// 从评分中找出最大分数的位置
int maxScore = 0;
std::vector<std::pair<int, int>> maxPoints;
for (int row = 1; row < N; row++)
for (int col = 1; col < N; col++)
{
// 前提是这个坐标是空的
if (chessBoard[row][col] == ' ')
{
if (scoreBoard[row][col] > maxScore) // 找最大的数和坐标
{
maxPoints.clear();
maxScore = scoreBoard[row][col];
maxPoints.push_back(std::make_pair(row, col));
}
else if (scoreBoard[row][col] == maxScore) // 如果有多个最大的数,都存起来
maxPoints.push_back(std::make_pair(row, col));
}
}
// 随机落子,如果有多个点的话
// srand((unsigned)time(0));
// int index = rand() % maxPoints.size();
int pi=maxPoints.size();
int index=pi/2;
std::pair<int, int> pointPair = maxPoints.at(index);
row = pointPair.first; // 记录落子点
col = pointPair.second;
}