-
Notifications
You must be signed in to change notification settings - Fork 0
/
player.cpp
executable file
·240 lines (207 loc) · 6.98 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
#include "player.hpp"
/*
* Constructor for the player; initialize everything here. The side your AI is
* on (BLACK or WHITE) is passed in as "side". The constructor must finish
* within 30 seconds.
*/
Player::Player(Side side) {
// Will be set to true in test_minimax.cpp.
testingMinimax = false;
board = new Board();
myside = side;
if (myside == BLACK)
{
other = WHITE;
} else if (myside == WHITE){
other = BLACK;
}
}
/*
* Destructor for the player.
*/
Player::~Player() {
}
/*
* Compute the next move given the opponent's last move. Your AI is
* expected to keep track of the board on its own. If this is the first move,
* or if the opponent passed on the last move, then opponentsMove will be
* nullptr.
*
* msLeft represents the time your AI has left for the total game, in
* milliseconds. doMove() must take no longer than msLeft, or your AI will
* be disqualified! An msLeft value of -1 indicates no time limit.
*
* The move returned must be legal; if there are no valid moves for your side,
* return nullptr.
*/
vector<Move*> Player::getPossibleMoves(Side side, Board * board){
vector<Move*> listMoves;
for (int i = 0; i < 8; i++){
for (int j = 0; j < 8; j++){
Move *newMove = new Move(i, j);
if (board -> checkMove(newMove, side)){
listMoves.push_back(newMove);
}
else {
delete newMove;
}
}
}
return listMoves;
}
int Player::calcScore(Move * possibleMove, Side side, Board * board_){
Board * copyBoard = board_ -> copy();
int score;
copyBoard -> doMove(possibleMove, side);
if (side == BLACK){
score = (copyBoard -> countBlack()) - (copyBoard -> countWhite());
} else {
score = (copyBoard -> countWhite()) - (copyBoard -> countBlack());
}
if ((possibleMove -> getX() == 0) && (possibleMove -> getY() == 0)){
score *= 3;
} else if ((possibleMove-> getX() == 7) && (possibleMove -> getY() == 0)){
score *= 3;
} else if ((possibleMove -> getX() == 0) && (possibleMove -> getY() == 7)){
score *= 3;
} else if ((possibleMove -> getX() == 7) && (possibleMove -> getY() == 0)){
score *= 3;
} else if ((possibleMove -> getX() == 1) && (possibleMove-> getY() == 0)){
score *= -1;
} else if ((possibleMove -> getX() == 6) && (possibleMove -> getY() == 0)){
score *= -1;
} else if ((possibleMove -> getX() == 0) && (possibleMove -> getY() == 1)){
score *= -1;
} else if ((possibleMove -> getX() == 7) && (possibleMove -> getY() == 1)){
score *= -1;
} else if ((possibleMove -> getX() == 0) && (possibleMove -> getY() == 6)){
score *= -1;
} else if ((possibleMove -> getX() == 7) && (possibleMove -> getY() == 6)){
score *= -1;
} else if ((possibleMove -> getX() == 1) && (possibleMove -> getY() == 7)){
score *= -1;
} else if ((possibleMove -> getX() == 6) && (possibleMove -> getY () == 7)){
score *= -1;
} else if ((possibleMove -> getX() == 1) && (possibleMove -> getY() == 1)){
score *= -3;
} else if ((possibleMove -> getX() == 6) && (possibleMove -> getY() == 1)){
score *= -3;
} else if ((possibleMove -> getX() == 1) && (possibleMove -> getY() == 6)){
score *= -3;
} else if ((possibleMove -> getX() == 6) && (possibleMove -> getY() == 6)){
score *= -3;
} else if ((possibleMove -> getX() == 0) && (possibleMove -> getY() == 2)){
score *= 1.25;
} else if ((possibleMove -> getX() == 0) && (possibleMove -> getY() == 3)){
score *= 1.25;
} else if ((possibleMove -> getX() == 0) && (possibleMove -> getY() == 4)){
score *= 1.25;
} else if ((possibleMove -> getX() == 0) && (possibleMove -> getY() == 5)){
score *= 1.25;
} else if ((possibleMove -> getX() == 2) && (possibleMove -> getY() == 0)){
score *= 1.25;
} else if ((possibleMove -> getX() == 3) && (possibleMove -> getY() == 0)){
score *= 1.25;
} else if ((possibleMove -> getX() == 4) && (possibleMove -> getY() == 0)){
score *= 1.25;
} else if ((possibleMove -> getX() == 5) && (possibleMove -> getY() == 0)){
score *= 1.25;
} else if ((possibleMove -> getX() == 7) && (possibleMove -> getY() == 2)){
score *= 1.25;
} else if ((possibleMove -> getX() == 7) && (possibleMove -> getY() == 3)){
score *= 1.25;
} else if ((possibleMove -> getX() == 7) && (possibleMove -> getY() == 4)){
score *= 1.25;
} else if ((possibleMove -> getX() == 7) && (possibleMove -> getY() == 5)){
score *= 1.25;
} else if ((possibleMove -> getX() == 7) && (possibleMove -> getY() == 2)){
score *= 1.25;
} else if ((possibleMove -> getX() == 2) && (possibleMove -> getY() == 7)){
score *= 1.25;
} else if ((possibleMove -> getX() == 3) && (possibleMove -> getY() == 7)){
score *= 1.25;
} else if ((possibleMove -> getX() == 4) && (possibleMove -> getY() == 7)){
score *= 1.25;
} else if ((possibleMove -> getX() == 5) && (possibleMove -> getY() == 7)){
score *= 1.25;
}
delete copyBoard;
return score;
}
Move *Player::doMove(Move *opponentsMove, int msLeft) {
board -> doMove(opponentsMove, other);
vector<Move*> listMoves = this -> getPossibleMoves(this->myside, this->board);
if ((listMoves.size() == 0) || (msLeft == 0)){
return nullptr;
}
Move *finalmove = Minimax(listMoves, myside, board);
board -> doMove(finalmove, myside);
return finalmove;
}
Move *Player::GetBestMove(vector<Move*> listMoves, Side side, Board * board_)
{
Move *highmove = listMoves[0];
int highScore = calcScore(highmove, side, board_);
for (unsigned int i = 1; i < listMoves.size(); i++)
{
int tempScore = calcScore(listMoves[i], side, board_);
if (tempScore > highScore){
highScore = tempScore;
highmove = listMoves[i];
}
}
return highmove;
}
Move *Player::Minimax(vector<Move*> listMoves, Side side, Board * board_)
{
Side other_side;
int tempScore = 10000;
if (side == myside)
{
other_side = other;
}
else
{
other_side = myside;
}
Move * bestMove;
for (unsigned int i = 0; i < listMoves.size(); i++)
{
Board * copyBoard = board_ -> copy();
copyBoard -> doMove(listMoves[i], side);
vector<Move*> theirMoves = this -> getPossibleMoves(this->other, copyBoard);
if (theirMoves.size() == 0)
{
return listMoves[i];
}
if (calcScore(GetBestMove(theirMoves, other_side, copyBoard), other_side, copyBoard) < tempScore)
{
tempScore = calcScore(GetBestMove(theirMoves, other_side, copyBoard), other_side, copyBoard);
bestMove = listMoves[i];
}
delete copyBoard;
}
return bestMove;
}
Move *Player::MiniMinimax(vector<Move*> listMoves)
{
int tempScore = 10000;
Move * bestMove;
for (unsigned int i = 0; i < listMoves.size(); i++)
{
Board * copyBoard = board -> copy();
copyBoard -> doMove(listMoves[i], myside);
vector<Move*> theirMoves = getPossibleMoves(this->other, copyBoard);
if (theirMoves.size() == 0)
{
return listMoves[i];
}
if (calcScore(Minimax(theirMoves, other, copyBoard), other, copyBoard) < tempScore)
{
tempScore = calcScore(Minimax(theirMoves, other, copyBoard), other, copyBoard);
bestMove = listMoves[i];
}
delete copyBoard;
}
return bestMove;
}