-
Notifications
You must be signed in to change notification settings - Fork 188
/
MachineGame.cpp
executable file
·295 lines (244 loc) · 8.14 KB
/
MachineGame.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
// SPDX-License-Identifier: GPL-3.0-or-later
// SPDX-FileCopyrightText: 2019-2024 XMuli & Contributors
// SPDX-GitHub: https://github.com/XMuli/ChineseChess
// SPDX-Author: XMuli <[email protected]>
#include "MachineGame.h"
MachineGame::MachineGame()
{
}
MachineGame::~MachineGame()
{
}
//辅助函: 选棋或移动棋子
void MachineGame::chooseOrMovePieces(int tempID, int& row, int& col)
{
if(m_nSelectID == -1) //选择棋子
{
if(m_nCheckedID != -1)
{
if(m_ChessPieces[m_nCheckedID].m_bRed)
{
m_nSelectID = tempID;
}
else
{
m_nSelectID = -1;
return;
}
}
}
else
{
if(canMove(m_nSelectID, m_nCheckedID, row, col ))
{
//_selectId为第一次点击选中的棋子,
//_clickId为第二次点击||被杀的棋子ID,准备选中棋子下子的地方
m_ChessPieces[m_nSelectID].m_nRow = row;
m_ChessPieces[m_nSelectID].m_nCol = col;
if(m_nCheckedID != -1)
m_ChessPieces[m_nCheckedID].m_bDead = true;
m_nSelectID = -1;
m_bIsRed = !m_bIsRed;
}
}
whoWin();
update();
}
void MachineGame::saveStep(int selectID, int checkedID, int row, int col, QVector<ChessStep *> &steps)
{
ChessStep* step = new ChessStep;
step->m_nRowFrom = m_ChessPieces[selectID].m_nRow;
step->m_nColFrom = m_ChessPieces[selectID].m_nCol;
step->m_nRowTo = row;
step->m_nnColTo = col;
step->m_nMoveID = selectID;
step->m_nKillID = checkedID;
steps.append(step);
}
void MachineGame::getAllPossibleMoveStep(QVector<ChessStep *> &steps)
{
for(int id = 0; id<16; id++) //存在的黑棋, 能否走到这些盘棋盘里面的格子
{
if(m_ChessPieces[id].m_bDead)
continue;
for(int row=0; row<10; row++)
{
for(int col=0; col<9; col++)
{
int i = 16;
for( ; i <= 31; i++)
{
if(m_ChessPieces[i].m_nRow == row && m_ChessPieces[i].m_nCol == col && m_ChessPieces[i].m_bDead == false)
break;
}
if(i!=32)
{
if(canMove(id, i, row, col))
saveStep(id, i, row, col, steps);
}
}
}
}
}
void MachineGame::getAllPossibleMoveStepAndNoKill(QVector<ChessStep *> &steps)
{
for(int id = 0; id<16; id++) //存在的黑棋, 能否走到这些盘棋盘里面的格子
{
if(m_ChessPieces[id].m_bDead)
continue;
for(int row=0; row<10; row++)
{
for(int col=0; col<9; col++)
{
int i = 0;
for(; i <= 31; i++)
{
if(m_ChessPieces[i].m_nRow == row && m_ChessPieces[i].m_nCol == col && m_ChessPieces[i].m_bDead == false)
break;
}
if(id < 16 && i == 32)
{
if(canMove(id, -1, row, col))
saveStep(id, -1, row, col, steps);
}
}
}
}
}
void MachineGame::mousePressEvent(QMouseEvent *ev)
{
if(ev->button() != Qt::LeftButton)
return;
int row, col;
if(!isChecked(ev->pos(), row, col))
return;
m_nCheckedID = -1;
//TODO Fix (升级 Qt6): https://github.com/XMuli/chinessChess/issues/23
int i =0;
for( ; i < 32; i++)
{
if(m_ChessPieces[i].m_nRow == row && m_ChessPieces[i].m_nCol == col && m_ChessPieces[i].m_bDead == false)
break;
}
if(0<=i && i<32)
m_nCheckedID = i;
clickPieces(m_nCheckedID, row, col);
if(m_bIsRed) //红方玩家时间
{
chooseOrMovePieces(i, row, col);
if(!m_bIsRed) //黑方紧接着进行游戏
{
machineChooseAndMovePieces();
//ToDo: 机器 黑方时间
}
}
}
void MachineGame::clickPieces(int checkedID, int &row, int &col)
{
if(m_bIsRed) //红方玩家时间
{
chooseOrMovePieces(checkedID, row, col);
if(!m_bIsRed) //黑方紧接着进行游戏
machineChooseAndMovePieces();
//ToDo: 机器 黑方时间
}
}
//假装移动棋子
void MachineGame::fakeMove(ChessStep *step)
{
if(step->m_nKillID != -1)
m_ChessPieces[step->m_nKillID].m_bDead = true;
m_ChessPieces[step->m_nMoveID].m_nRow = step->m_nRowTo;
m_ChessPieces[step->m_nMoveID].m_nCol = step->m_nnColTo;
m_bIsRed = !m_bIsRed;
}
//撤回先前假装移动棋子的步骤
void MachineGame::unFakeMove(ChessStep *step)
{
if(step->m_nKillID != -1)
m_ChessPieces[step->m_nKillID].m_bDead = false;
m_ChessPieces[step->m_nMoveID].m_nRow = step->m_nRowFrom;
m_ChessPieces[step->m_nMoveID].m_nCol = step->m_nColFrom;
m_bIsRed = !m_bIsRed;
}
//计算最好的局面分
int MachineGame::calcScore()
{
//enum m_emTYPE{JIANG, SHI, XIANG, MA, CHE, PAO, BING};
//黑棋分数 - 红旗分数
int redGrossScore = 0;
int blackGrossScore = 0;
static int chessScore[]={200, 20, 40, 60, 100, 80, 10};
for(int i=0; i<16; i++)
{
if(m_ChessPieces[i].m_bDead)
continue;
blackGrossScore += chessScore[m_ChessPieces[i].m_emType];
}
for(int i=16; i<32; i++)
{
if(m_ChessPieces[i].m_bDead)
continue;
redGrossScore += chessScore[m_ChessPieces[i].m_emType];
}
return (blackGrossScore - redGrossScore);
}
//获得最好的移动步骤
//第一此玩了一把,发现我居然下不赢自己写的算法。哭了哭了哭了555555........
ChessStep* MachineGame::getBestMove()
{
int maxScore = -10000;
ChessStep* retStep = NULL;
//------------------------
//有可击杀的红棋子就走击杀红棋子最优的一步
// 1.看看有那些步骤可以走
QVector<ChessStep*> steps;
getAllPossibleMoveStep(steps); // 黑棋吃红棋的所有可能的步骤
//------------------------
//没有可击杀的红棋子就走最后的一步
QVector<ChessStep*> stepsAndNoKill;
getAllPossibleMoveStepAndNoKill(stepsAndNoKill); // 黑棋移动所有可能的步骤(不吃红棋子)
//2.试着走一下
for(QVector<ChessStep*>::iterator it = steps.begin(); it!=steps.end(); it++)
{
ChessStep* step = *it;
fakeMove(step);
int score = calcScore(); //3.计算最好的局面分
unFakeMove(step);
if(score > maxScore)
{
maxScore = score;
retStep = step;
}
}
if(retStep != NULL)
return retStep;
//2.试着走一下
//从这种不击杀红棋子,只是单纯移动黑棋steps里面,随机抽选一种进行下棋
int nStepsCount = stepsAndNoKill.count();
qsrand(QTime(0,0,0).secsTo(QTime::currentTime())); //随机数种子, 0~MAX
int temp =qrand()% nStepsCount;
QVector<ChessStep*>::iterator it = stepsAndNoKill.begin();
retStep = it[temp];
if(retStep == NULL)
whoWin();
//4.取最好的结果作为参考
return retStep;
}
void MachineGame::machineChooseAndMovePieces()
{
ChessStep* step = getBestMove();
if(step->m_nKillID == -1) //黑棋没有可以击杀的红棋子,只好走能够走的过程中最后一步棋
{
m_ChessPieces[step->m_nMoveID].m_nRow = step->m_nRowTo;
m_ChessPieces[step->m_nMoveID].m_nCol = step->m_nnColTo;
}
else //黑棋有可以击杀的红棋子,故击杀红棋子
{
m_ChessPieces[step->m_nKillID].m_bDead = true;
m_ChessPieces[step->m_nMoveID].m_nRow = m_ChessPieces[step->m_nKillID].m_nRow;
m_ChessPieces[step->m_nMoveID].m_nCol = m_ChessPieces[step->m_nKillID].m_nCol;
m_nSelectID = -1;
}
m_bIsRed = !m_bIsRed;
}