-
Notifications
You must be signed in to change notification settings - Fork 0
/
SingleGame.cpp
258 lines (229 loc) · 6.12 KB
/
SingleGame.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
#include "SingleGame.h"
#include <QTimer>
#include <QDebug>
SingleGame::SingleGame(QWidget *parent) : Board(parent)
{
/* 初始化 AI 智能程度 */
_level = 4;
}
SingleGame::~SingleGame()
{
}
/* 用户选择悔棋,那在电脑这边就得跟着悔棋,所以执行2次 */
void SingleGame::back()
{
if(_bRedTurn)
{
backOne();
backOne();
}
}
/* 根据用户做的事返回对应的事 */
void SingleGame::click(int id, int row, int col)
{
if(_bRedTurn)
{
Board::click(id, row, col);
if(!_bRedTurn)
{
/* 启动0.1秒定时器,在0.1秒后电脑再思考 */
QTimer::singleShot(100, this, SLOT(computerMove()));
}
}
}
/* AI 行动 */
void SingleGame::computerMove()
{
/* 先获取最佳步骤,然后按最佳步骤行动 */
Step* step = getBestMove();
moveStone(step->_moveid, step->_killid, step->_rowTo, step->_colTo);
delete step;
update(); //刷新界面
}
/* 获取最佳步骤 */
Step* SingleGame::getBestMove()
{
/* 创建链表并初始化数据 */
Step* ret = NULL;
QVector<Step*> steps;
getAllPossibleMove(steps);
int maxInAllMinScore = -300000; //初始局面分尽可能低
while(steps.count())
{
/* 防止内存泄露,类栈模式,将最后的取出来,在释放其内存后将其在链表中删除 */
Step* step = steps.last();
steps.removeLast();
/* 走棋并计算局面分 */
fakeMove(step);
int minScore = getMinScore(this->_level-1, maxInAllMinScore);
/* 移动后把棋子走回来 */
unfakeMove(step);
if(minScore > maxInAllMinScore)
{
if(ret)
{
delete ret;
}
ret = step;
maxInAllMinScore = minScore;
}
else
{
delete step;
}
}
return ret;
}
/* 评估局面分 */
int SingleGame::score()
{
/* 存储每个棋子对应的分值 */
static int s[] = {1000, 499, 501, 200, 150000, 100, 100};
/* 黑棋的总分 */
int scoreBlack = 0;
/* 红棋的总分 */
int scoreRed = 0;
/* 计算红棋分数 */
for(int i=0; i<16; ++i)
{
if(_s[i]._dead)
{
continue;
}
scoreRed += s[_s[i]._type];
}
/* 计算黑棋分数 */
for(int i=16; i<32; ++i)
{
if(_s[i]._dead)
{
continue;
}
scoreBlack += s[_s[i]._type];
}
/* 将 黑棋分数 - 红棋分数 作为局面分 */
return scoreBlack - scoreRed;
}
int SingleGame::getMinScore(int level, int curMin)
{
if(level == 0)
{
return score();
}
QVector<Step*> steps;
getAllPossibleMove(steps);
int minInAllMaxScore = 300000;
while(steps.count())
{
/* 防止内存泄露,类栈模式,将最后的取出来,在释放其内存后将其在链表中删除 */
Step* step = steps.last();
steps.removeLast();
fakeMove(step);
int maxScore = getMaxScore(level-1, minInAllMaxScore);
unfakeMove(step);
delete step;
if(maxScore <= curMin)
{
while(steps.count())
{
/* 防止内存泄露,类栈模式,将最后的取出来,在释放其内存后将其在链表中删除 */
Step* step = steps.last();
steps.removeLast();
delete step;
}
return maxScore;
}
if(maxScore < minInAllMaxScore)
{
minInAllMaxScore = maxScore;
}
}
return minInAllMaxScore;
}
int SingleGame::getMaxScore(int level, int curMax)
{
if(level == 0)
{
return score();
}
QVector<Step*> steps;
getAllPossibleMove(steps);
int maxInAllMinScore = -300000;
while(steps.count())
{
/* 防止内存泄露,类栈模式,将最后的取出来,在释放其内存后将其在链表中删除 */
Step* step = steps.last();
steps.removeLast();
fakeMove(step);
int minScore = getMinScore(level-1, maxInAllMinScore);
unfakeMove(step);
delete step;
if(minScore >= curMax)
{
while(steps.count())
{
/* 防止内存泄露,类栈模式,将最后的取出来,在释放其内存后将其在链表中删除 */
Step* step = steps.last();
steps.removeLast();
delete step;
}
return minScore;
}
if(minScore > maxInAllMinScore)
{
maxInAllMinScore = minScore;
}
}
return maxInAllMinScore;
}
/* 模拟行动并计算对应的局面分 */
void SingleGame::fakeMove(Step *step)
{
killStone(step->_killid);
moveStone(step->_moveid, step->_rowTo, step->_colTo);
}
/* 模拟行动完成后再悔棋 */
void SingleGame::unfakeMove(Step *step)
{
reliveStone(step->_killid);
moveStone(step->_moveid, step->_rowFrom, step->_colFrom);
}
/* 获取所有能走的步骤 */
void SingleGame::getAllPossibleMove(QVector<Step *> &steps)
{
/* 根据黑子和红子判断 ID 的取值范围 */
int min, max;
if(this->_bRedTurn)
{
min = 0, max = 16;
}
else
{
min = 16, max = 32;
}
/* 遍历每个棋子在每一个位置是否能移动 */
for(int i=min;i<max; i++)
{
/* 如果棋子已经死了就不用继续判断了 */
if(this->_s[i]._dead)
{
continue;
}
for(int row = 0; row<=9; ++row)
{
for(int col=0; col<=8; ++col)
{
/* 判断两个棋子颜色是否相同,不同就判断能不能移动 */
int killid = this->getStoneId(row, col);
if(sameColor(i, killid))
{
continue;
}
if(canMove(i, killid, row, col))
{
saveStep(i, killid, row, col, steps);
}
}
}
}
}