-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplayer.cpp
executable file
·440 lines (387 loc) · 12.6 KB
/
player.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
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
// ECE 469: Artificial Intelligence, Prof. Sable
// Checkers - by Miraj Patel
// player.cpp - Defines player class (player's pieces and next available moves)
#include "player.h"
#include <cstdlib>
#include <cstdio>
#include <memory.h>
#include <iostream>
#include <sstream>
#include <sys/time.h>
#include <unistd.h>
#define NEGINFINITY -1000000000
#define INFINITY 1000000000
#define MAXDEPTH 20
using namespace std;
bool outOfTime;
int bestMove, maxDepthReached;
struct timeval startTime, curTime;
double millisecStart, millisecNow;
extern double timeLimit;
Player::Player() {
playerNumber = 0;
}
// get the number of project moves it would take to reach the end location
int getDistance(Location (&one), Location (&two)) {
int distance = 0;
int startRow = one.row, endRow = two.row, startColumn = one.column, endColumn = two.column;
while (startRow != endRow && endColumn != startColumn) {
if (startRow % 2 == 0) {
if (startRow == endRow) {
startRow++;
if (endColumn > startColumn) {
startColumn++;
}
distance++;
}
else {
if (startRow < endRow) {
startRow++;
}
else {
startRow--;
}
if (endColumn > startColumn) {
startColumn++;
}
distance++;
}
}
else {
if (startRow == endRow) {
if (startRow < 7) {
startRow++;
}
else {
startRow--;
}
if (endColumn < startColumn) {
startColumn--;
}
distance++;
}
else {
if (startRow < 7) {
startRow++;
}
else {
startRow--;
}
if (endColumn < startColumn) {
startColumn--;
}
distance++;
}
}
}
return distance;
}
int Player::evaluateState(Game (&game)) {
int score;
int piecesValue = 0, positionValue = 0, differenceValue = 0, advantageValue = 0;
int playerAdvantage = -1;
int pieceRow, pieceColumn;
int p1AdvDistance = 0, p2AdvDistance = 0;
if (game.pieces[1].size() > game.pieces[0].size())
playerAdvantage = 1;
else if (game.pieces[1].size() < game.pieces[0].size())
playerAdvantage = 0;
for (int p1Iter = 0; p1Iter < game.pieces[1].size(); p1Iter++) {
pieceRow = game.pieces[1][p1Iter].row;
pieceColumn = game.pieces[1][p1Iter].column;
if (game.board[pieceRow][pieceColumn] > 2) {
// Kings
piecesValue += 5;
// If behind in pieces and about to lose, retreat to double corners
if ((playerAdvantage == 0) && (game.pieces[1].size() < 3)) {
if ((pieceRow > 5 && pieceColumn == 3) || (pieceRow < 2 && pieceColumn == 0)) {
advantageValue += 9;
}
}
// Else: If player 1 has advantage, smaller bonus for being near the double corner (closer the better)
else if ((playerAdvantage == 1) && (game.pieces[0].size() < 3)) {
// If Player 2 occupying top left corner
if (((game.board[0][0] > 0) && (game.board[0][0] % 2 == 0)) || ((game.board[1][0] > 0) && (game.board[1][0] % 2 == 0))) {
// Right on the corner
if ((pieceRow < 2) && (pieceColumn == 0)) {
advantageValue += 7;
}
// One move away from the corner
if ((pieceRow == 2 && pieceColumn == 0) || (pieceRow == 1 && pieceColumn == 1)) {
advantageValue += 5;
}
// Two moves away from the corner
else if ((pieceRow == 3 && pieceColumn == 0) || (pieceRow == 3 && pieceColumn == 1)) {
advantageValue += 3;
}
else if ((pieceRow == 2 && pieceColumn == 1) || (pieceRow == 0 && pieceColumn == 1)) {
advantageValue += 3;
}
}
// If Player 2 occupying bottom right corner
else if (((game.board[6][3] > 0) && (game.board[6][3] % 2 == 0)) || ((game.board[7][3] > 0) && (game.board[7][3] % 2 == 0))) {
// Right on the corner
if ((pieceRow > 5) && (pieceColumn == 3)) {
advantageValue += 7;
}
// One move away from the corner
if ((pieceRow == 6 && pieceColumn == 2) || (pieceRow == 5 && pieceColumn == 3)) {
advantageValue += 5;
}
// Two moves away from the corner
else if ((pieceRow == 4 && pieceColumn == 3) || (pieceRow == 4 && pieceColumn == 2)) {
advantageValue += 3;
}
else if ((pieceRow == 5 && pieceColumn == 2) || (pieceRow == 7 && pieceColumn == 2)) {
advantageValue += 3;
}
}
}
}
else {
// Regular pieces
piecesValue += 3;
// Staying in back row to prevent opponent kinging
if (pieceRow == 7) {
positionValue += 9;
}
else {
// Closer to being king, row = 0, the better
positionValue += (7 - pieceRow);
}
}
}
for (int p2Iter = 0; p2Iter < game.pieces[0].size(); p2Iter++) {
pieceRow = game.pieces[0][p2Iter].row;
pieceColumn = game.pieces[0][p2Iter].column;
if (game.board[pieceRow][pieceColumn] > 2) {
// Kings
piecesValue -= 5;
// If behind in pieces and about to lose, retreat to double corners
if ((playerAdvantage == 1) && (game.pieces[0].size() < 3)) {
if ((pieceRow > 5 && pieceColumn == 3) || (pieceRow < 2 && pieceColumn == 0)) {
advantageValue -= 9;
}
}
// Else: If player 2 has advantage, smaller bonus for being near the double corner (closer the better)
else if ((playerAdvantage == 0) && (game.pieces[1].size() < 3)) {
// If Player 1 occupying top left corner
if (((game.board[0][0] > 0) && (game.board[0][0] % 2 == 1)) || ((game.board[1][0] > 0) && (game.board[1][0] % 2 == 1))) {
// Right on the corner
if ((pieceRow < 2) && (pieceColumn == 0)) {
advantageValue -= 7;
}
// One move away from the corner
if ((pieceRow == 2 && pieceColumn == 0) || (pieceRow == 1 && pieceColumn == 1)) {
advantageValue -= 5;
}
// Two moves away from the corner
else if ((pieceRow == 3 && pieceColumn == 0) || (pieceRow == 3 && pieceColumn == 1)) {
advantageValue -= 3;
}
else if ((pieceRow == 2 && pieceColumn == 1) || (pieceRow == 0 && pieceColumn == 1)) {
advantageValue -= 3;
}
}
// If Player 1 occupying bottom right corner
else if (((game.board[6][3] > 0) && (game.board[6][3] % 2 == 1)) || ((game.board[7][3] > 0) && (game.board[7][3] % 2 == 1))) {
// Right on the corner
if ((pieceRow > 5) && (pieceColumn == 3)) {
advantageValue -= 7;
}
// One move away from the corner
if ((pieceRow == 6 && pieceColumn == 2) || (pieceRow == 5 && pieceColumn == 3)) {
advantageValue -= 5;
}
// Two moves away from the corner
else if ((pieceRow == 4 && pieceColumn == 3) || (pieceRow == 4 && pieceColumn == 2)) {
advantageValue -= 3;
}
else if ((pieceRow == 5 && pieceColumn == 2) || (pieceRow == 7 && pieceColumn == 2)) {
advantageValue -= 3;
}
}
}
}
else {
// Regular pieces
piecesValue -= 3;
// Staying in back row to prevent opponent kinging
if (pieceRow == 0) {
positionValue -= 9;
}
else {
// Closer to being king, row = 7, the better
positionValue -= pieceRow;
}
}
}
piecesValue = piecesValue * 1000000;
positionValue = positionValue * 100000;
// Difference in number of pieces
differenceValue = (game.pieces[1].size() - game.pieces[0].size()) * 1000;
advantageValue = advantageValue * 10;
score = piecesValue + positionValue + differenceValue + advantageValue;
return score;
}
void cloneGame(Game (&destGame), Game (&srcGame), int moveNum) {
destGame.curTurn = srcGame.curTurn;
for (int rowIter = 0; rowIter < 8; rowIter++) {
for (int columnIter = 0; columnIter < 4; columnIter++) {
destGame.board[rowIter][columnIter] = srcGame.board[rowIter][columnIter];
}
}
for (int p2PieceIter = 0; p2PieceIter < srcGame.pieces[0].size(); p2PieceIter++) {
Location p2Piece;
p2Piece.row = srcGame.pieces[0][p2PieceIter].row;
p2Piece.column = srcGame.pieces[0][p2PieceIter].column;
destGame.pieces[0].push_back(p2Piece);
}
for (int p1PieceIter = 0; p1PieceIter < srcGame.pieces[1].size(); p1PieceIter++) {
Location p1Piece;
p1Piece.row = srcGame.pieces[1][p1PieceIter].row;
p1Piece.column = srcGame.pieces[1][p1PieceIter].column;
destGame.pieces[1].push_back(p1Piece);
}
destGame.movesAvailable[0].startPiece = srcGame.movesAvailable[moveNum-1].startPiece;
destGame.movesAvailable[0].end.row = srcGame.movesAvailable[moveNum-1].end.row;
destGame.movesAvailable[0].end.column = srcGame.movesAvailable[moveNum-1].end.column;
destGame.movesAvailable[0].numJumps = srcGame.movesAvailable[moveNum-1].numJumps;
for (int jumpIter = 0; jumpIter < srcGame.movesAvailable[moveNum-1].numJumps; jumpIter++) {
destGame.movesAvailable[0].jumpedLocations[jumpIter].row = srcGame.movesAvailable[moveNum-1].jumpedLocations[jumpIter].row;
destGame.movesAvailable[0].jumpedLocations[jumpIter].column = srcGame.movesAvailable[moveNum-1].jumpedLocations[jumpIter].column;
}
}
bool terminalState(Game (&game)) {
if ((game.numMoves == 0) || (game.numNonJumpMoves == MOVESTODRAW)) {
return true;
}
return false;
}
int utility(Game (&game), int depth) {
if (game.numNonJumpMoves == MOVESTODRAW)
return 0;
if (game.curTurn) {
// Player 1's turn and has 0 moves available, higher depth = closer to winning
return (-100000000*(depth+1));
}
else {
// Player 2's turn and has 0 moves available
return (100000000*(depth+1));
}
}
int Player::alphabeta(Game (&gameNode), int depth, int alpha, int beta) {
if (!outOfTime) {
gettimeofday(&curTime, NULL);
millisecNow = (curTime.tv_sec*1000) + (curTime.tv_usec/1000);
if (((millisecNow - millisecStart)/timeLimit) > 0.75) {
outOfTime = true;
return 0;
}
gameNode.getNextMoves();
// Check for terminal state: if either player has 0 moves = someone wins, or if tie, and return that utility score
if (terminalState(gameNode)) {
return (utility(gameNode, depth));
}
if (depth == 0) {
//cout << "\t\tReturning " << this->evaluateState(gameNode) << endl;
return (this->evaluateState(gameNode));
}
if (gameNode.curTurn) {
int bestValue = alpha;
for (int child = 1; child <= gameNode.numMoves; child++) {
Game childGame;
cloneGame(childGame, gameNode, child);
childGame.chooseMove(1);
int ret = alphabeta(childGame, (depth-1), bestValue, beta);
if (ret > bestValue) {
bestValue = ret;
if (depth == maxDepthReached) {
bestMove = child;
}
}
else if ((ret == bestValue) && (depth == maxDepthReached)) {
if (rand() % 2) {
bestMove = child;
}
}
if (beta <= bestValue) {
// Beta cut-off = prune remaining branches
break;
}
}
return bestValue;
}
else {
int bestValue = beta;
for (int child = 1; child <= gameNode.numMoves; child++) {
//cout << "Depth = " << depth << ", child = " << child << " of " << gameNode.numMoves << ", alpha = " << alpha << ", beta = " << beta << endl;
Game childGame;
cloneGame(childGame, gameNode, child);
childGame.chooseMove(1);
int ret = alphabeta(childGame, (depth-1), alpha, bestValue);
if (ret < bestValue) {
bestValue = ret;
if (depth == maxDepthReached) {
bestMove = child;
}
}
else if ((ret == bestValue) && (depth == maxDepthReached)) {
if (rand() % 2) {
bestMove = child;
}
}
if (bestValue <= alpha) {
// Alpha cut-off = prune remaining branches
break;
}
}
return bestValue;
}
}
else {
return 0;
}
}
int Player::AIChooseMove(Game (&game)) {
// Assumes the 1st layer of moves have been given to it already
gettimeofday(&startTime, NULL);
millisecStart = (startTime.tv_sec*1000) + (startTime.tv_usec/1000);
bestMove = -1;
srand(time(NULL));
int bestValue, completedBestMove;
int startingDepth = 5;
if (timeLimit > 4000)
startingDepth = 7;
if (game.pieces[0].size() + game.pieces[1].size() <= 10) {
startingDepth += 1;
}
if (game.pieces[0].size() + game.pieces[1].size() <= 5) {
startingDepth += 1;
}
outOfTime = false;
for (int depth = startingDepth; depth < MAXDEPTH; depth++) {
//for (int depth = 2; depth < 3; depth++) {
maxDepthReached = depth;
bestValue = alphabeta(game, depth, NEGINFINITY, INFINITY);
if (!outOfTime) {
completedBestMove = bestMove;
}
else {
maxDepthReached--;
}
gettimeofday(&curTime, NULL);
millisecNow = (curTime.tv_sec*1000) + (curTime.tv_usec/1000);
if (outOfTime || (((millisecNow - millisecStart)/timeLimit) > 0.5)) {
//cout << "Diff in milliseconds = " << (millisecNow - millisecStart) << endl;
break;
}
}
int millisecDiff = millisecNow - millisecStart;
cout << "After " << (millisecDiff/1000) << " seconds: reached depth " << maxDepthReached << "." << endl;
if (outOfTime)
cout << "Ran out of time searching depth " << (maxDepthReached+1) << "." << endl;
cout << "AI chooses move #" << completedBestMove << "." << endl;
game.getNextMoves();
return completedBestMove;
}