-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathplayer.cpp
More file actions
360 lines (345 loc) · 11.4 KB
/
player.cpp
File metadata and controls
360 lines (345 loc) · 11.4 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
#include "player.h"
#include <stdlib.h>
#include <ctime>
Player::Player(Color c)
:color(c) {}
Person::Person(Color c)
:Player(c) {}
Bot::Bot(Color c)
:Player(c) {}
/**
* @brief allow board to react on clicks
*/
void Person::setBoardActive()
{
board->setActivity(true);
}
/**
* @brief allow board to react on fields
*/
void Bot::setBoardActive()
{
board->setActivity(true);
}
/**
* @brief forbid board to react on fields
*/
void Bot::setBoardUnactive()
{
board->setActivity(false);
}
void Person::move()
{
setBoardActive();
}
/**
* @brief artificial intelligence
* randomly choose where to move
* @return structure Move
*/
Move Bot::easythink()
{
Move moves;
int from = -1, to = -1;
srand(static_cast<unsigned int>(time(nullptr)));
QVector<int> beat, move;
if(color == Color::WHITE)
{
beat = board->getWhiteBeat();
move = board->getWhiteMove();
}
else
{
beat = board->getBlackBeat();
move = board->getBlackMove();
}
if(!beat.empty())
{
int i = rand()%beat.size();
if(i != 0) i--;
from = beat[i];
}
else if(!move.empty())
{
int i = rand()%move.size();
if(i != 0) i--;
from = move[i];
}
if(from != -1)
{
beat = board->getFieldBeats(from);
move = board->getFieldsMoves(from);
if(!beat.empty())
{
int i = rand()%beat.size();
if(i != 0) i--;
to = beat[i];
}
else if(!move.empty())
{
int i = rand()%move.size();
if(i != 0) i--;
to = move[i];
}
}
moves.from = from;
moves.to = to;
return moves;
}
/*int Bot::minimax(Board *board, int depth, bool maximizer)
{
int score = board->evaluateBoard(color);
if(depth == DEPTH)
{
//qDebug("deapth");
return score;}
if(board->getWhiteNumber() == 0 || board->getBlackNumber() == 0)
{
//qDebug("No figures");
return score;}
if(board->getWhiteBeat().empty() && board->getWhiteMove().empty() && maximizer && color == Color::WHITE)
{
//qDebug("1");
return score;}
if(board->getBlackBeat().empty() && board->getBlackMove().empty() && maximizer && color == Color::BLACK)
{
//qDebug("2");
return score;}
if(board->getWhiteBeat().empty() && board->getWhiteMove().empty() && !maximizer && color == Color::BLACK)
{
//qDebug("3");
return score;}
if(board->getBlackBeat().empty() && board->getBlackMove().empty() && !maximizer && color == Color::WHITE)
{
//qDebug("4");
return score;}
//QVector<int> kings = board->getKings();
//bool wasKing = false;
if(maximizer)
{
//qDebug("MAXIMIZER");
int best = INT_MIN;
QVector<int> fromMoves, toMoves;
if(color == Color::BLACK)
{
fromMoves = board->getBlackBeat();
if(fromMoves.empty()) fromMoves = board->getBlackMove();
}
else
{
fromMoves = board->getWhiteBeat();
if(fromMoves.empty()) fromMoves = board->getWhiteMove();
}
int from = -1, to = -1;
for (int i = 0; i < fromMoves.size(); ++i)
{
from = fromMoves[i];
//wasKing = board->isKing(from, kings);
toMoves = board->getFieldBeats(from);
if(toMoves.empty()) toMoves = board->getFieldsMoves(from);
for(int j = 0; j < toMoves.size(); ++j)
{
to = toMoves[j];
/*bool king = false;
bool hadBeat = false;
int beatfield = board->fieldToBeat(from, to);
hadBeat = board->getField(from).getFigure()->getBeat();
if(beatfield != -1)
{
king = board->getField(beatfield).getFigure()->isKing();
}*/
// qDebug("FROM/TO: ");
//qDebug(std::to_string(from).c_str());
//qDebug(std::to_string(to).c_str());
//qDebug("before maximizer move");
/*bool wasKing = false;
wasKing = board->getField(from).getFigure()->isKing();*/
//if(wasKing) board->setText(QString::number(from)+" wasKing\n");
//else board->setText(QString::number(from)+" was not king\n");
//QVector<int> Kings = board->getKings();*/
/*field* fields = board->getBoardFields();
//board->move(from, to, true);
//qDebug("after max move");
//Board* b = new Board(*board);
best = max(best, minimax(board, depth+1, !maximizer));
//qDebug("before undo move in max");
//if(b)delete b;
//board->undoMove(from, to, wasKing, Kings, king, hadBeat, true);
//qDebug("after undo move in max");
board->setBoardFields(fields);
board->setActivePrevactive(from, to);
board->correctBoard();
//qDebug("after correct board in max");
//qDebug("end maximizer");
}
}
//qDebug("BEST");
//qDebug(std::to_string(best).c_str());
return best;
}
else
{
int best = INT_MAX;
//qDebug("MINIMIZER");
QVector<int> fromMoves, toMoves;
if(color == Color::WHITE)
{
fromMoves = board->getBlackBeat();
if(fromMoves.empty()) fromMoves = board->getBlackMove();
}
else
{
fromMoves = board->getWhiteBeat();
if(fromMoves.empty()) fromMoves = board->getWhiteMove();
}
int from = -1, to = -1;
for (int i = 0; i < fromMoves.size(); ++i)
{
from = fromMoves[i];
//wasKing = board->isKing(from, kings);
toMoves = board->getFieldBeats(from);
if(toMoves.empty()) toMoves = board->getFieldsMoves(from);
for(int j = 0; j < toMoves.size(); ++j)
{
to = toMoves[j];
/*bool king = false;
bool hadBeat = false;
int beatfield = board->fieldToBeat(from, to);
hadBeat = board->getField(from).getFigure()->getBeat();
if(beatfield != -1)
{
king = board->getField(beatfield).getFigure()->isKing();
}*/
//qDebug("FROM/TO: ");
// qDebug(std::to_string(from).c_str());
// qDebug(std::to_string(to).c_str());
//qDebug("before min move");
//bool wasKing = false;
//wasKing = board->getField(from).getFigure()->isKing();
//if(wasKing) board->setText(QString::number(from)+"wasKing\n");
//else board->setText(QString::number(from)+"was not king\n");
//QVector<int> Kings = board->getKings();*/
/*field* fields = board->getBoardFields();
board->move(from, to);
//qDebug("after min move");
//Board*b = new Board(*board);
best = min(best, minimax(board, depth+1, maximizer));
//qDebug("before undo move in min");
//if(b)delete b;
//board->undoMove(from, to, wasKing, Kings, king, hadBeat, true);
//qDebug("after undo move in min");
board->setBoardFields(fields);
board->setActivePrevactive(from, to);
board->correctBoard();
//qDebug("after correct board in min");
//qDebug("end minimizer");
}
}
//qDebug("BEST");
//qDebug(std::to_string(best).c_str());
return best;
}
}*/
/*Move Bot::hardThink()
{
int bestVal = INT_MIN;
int from = -1, to = -1;
Move bestMove;
bestMove.from = bestMove.to = -1;
QVector<int> fromMoves, toMoves;
if(color == Color::BLACK)
{
fromMoves = board->getBlackBeat();
if(fromMoves.empty()) fromMoves = board->getBlackMove();
}
else
{
fromMoves = board->getWhiteBeat();
if(fromMoves.empty()) fromMoves = board->getWhiteMove();
}
//QVector<int> kings = board->getKings();
//bool wasKing = false;
for (int i = 0; i < fromMoves.size(); ++i)
{
from = fromMoves[i];
//wasKing = board->isKing(from, kings);
//board->setText("WASKING FIRST "+QString::number(from)+" "+QString::number(wasKing)+'\n');
toMoves = board->getFieldBeats(from);
if(toMoves.empty()) toMoves = board->getFieldsMoves(from);
for(int j = 0; j < toMoves.size(); ++j)
{
to = toMoves[j];
//bool king = false;
//int beatfield = board->fieldToBeat(from, to);
//bool hadBeat = false;
//hadBeat = board->getField(from).getFigure()->getBeat();
/*if(beatfield != -1)
{
king = board->getField(beatfield).getFigure()->isKing();
}*/
/*qDebug("FROM/TO: ");
qDebug(std::to_string(from).c_str());
qDebug(std::to_string(to).c_str());
qDebug("Before move in hard think");*/
/*bool wasKing = false;
wasKing = board->getField(from).getFigure()->isKing();*/
/*if(wasKing) board->setText(QString::number(from)+"wasKing\n");
else board->setText(QString::number(from)+"was not king\n");*/
//board->setText("WASKING SECOND "+QString::number(from)+" "+QString::number(wasKing)+'\n');
//QVector<int> Kings = board->getKings();*/
/* qDebug("before get Board fields in hard think");
field *fields = board->getBoardFields();
qDebug("after get Board fields in hard think");
board->move(from, to);
//Board* b = new Board(*board);
qDebug("after move in hard think");
int moveVal = minimax(board, 0, false);
qDebug("before setBoardFields in hard think");
//if(b)delete b;
//board->setText("WASKING THIRD "+QString::number(from)+" "+QString::number(wasKing)+'\n');
//board->undoMove(from, to, wasKing, Kings, king, hadBeat, true);
//qDebug("after undo in hard think");
board->setBoardFields(fields);
//qDebug("after setBoardFields in hard think");
board->setActivePrevactive(from, to);
//qDebug("before correct in hard think");
board->correctBoard();
board->setActivity(true);
if(moveVal > bestVal)
{
bestMove.from = from;
bestMove.to = to;
bestVal = moveVal;
}
}
}
return bestMove;
}
*/
/**
* @brief move
*/
void Bot::move()
{
setBoardUnactive(); //forbid board to rect on clicks
while(canMove)
{
/*wait*/
QTime time;
time.start();
for(;time.elapsed() < 700;) {
QApplication::processEvents(nullptr);
}
int from = -1, to = -1;
Move move = easythink();
from = move.from;
to = move.to;
if(from != -1 && to != -1)
{
board->setActivePrevactive(from, to);
board->move(from, to);
}
}
setBoardActive();//allow board to react on clicks
emit(board->moved(this));
}