This repository was archived by the owner on Mar 26, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathplayer.cpp
More file actions
205 lines (192 loc) · 6.52 KB
/
player.cpp
File metadata and controls
205 lines (192 loc) · 6.52 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
#include "player.h"
#include <chrono>
Side my_side = WHITE;
Side opp_side = BLACK;
const int board_values[8][8] = {{99,-8,8,6,6,8,-8,99},
{-8,-24,-4,-3,-3,-4,-24,-8},
{8,-4,7,4,4,7,-4,8},
{6,-3,4,0,0,4,-3,6},
{6,-3,4,0,0,4,-3,6},
{8,-4,7,4,4,7,-4,8},
{-8,-24,-4,-3,-3,-4,-24,-8},
{99,-8,8,6,6,8,-8,99}};
/*
* 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;
my_board = Board();
if(side == BLACK) {
my_side = BLACK;
opp_side = WHITE;
}
}
/*
* 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 NULL.
*
* 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 NULL.
*/
Move *Player::doMove(Move *opponentsMove, int msLeft) {
my_board.doMove(opponentsMove, opp_side);
if (testingMinimax) {
Move* my_move = Player::miniMaxNaive();
if (my_move) {
my_board.doMove(my_move, my_side);
}
return my_move;
} else {
Move *my_move = Player::miniMax();
if (my_move) {
my_board.doMove(my_move, my_side);
}
return my_move;
}
}
/*
* Computes best move based off of a single heuristic score
*/
Move* Player::singleHeuristic() {
std::vector<Move*> moves = Player::possibleMoves(my_board, my_side);
Move* best = NULL;
if (moves.size() > 0) {
best = moves[0];
for (int i = 0; i < moves.size(); ++i) {
if (Player::heuristic(my_board, moves[i], my_side) >
Player::heuristic(my_board, moves[0], my_side)) {
best = moves[i];
}
}
} return best;
}
/*
* Gets the best move based on the minmax technique
*/
Move* Player::miniMax(){
std::vector<Move*> ply1_moves = Player::possibleMoves(my_board, my_side);
if (ply1_moves.size() == 0) {
return NULL;
} else {
std::vector < Move * > ply2_moves;
int values[ply1_moves.size()];
for (int i = 0; i < ply1_moves.size(); i++) {
values[i] = Player::heuristic(my_board, ply1_moves[i], my_side);
}
for (int i = 0; i < ply1_moves.size(); i++) {
Board *potential_board = my_board.copy();
potential_board->doMove(ply1_moves[i], my_side);
ply2_moves = Player::possibleMoves(*potential_board, opp_side);
for (int j = 0; j < ply2_moves.size(); ++j) {
if (Player::heuristic(*potential_board, ply2_moves[j], opp_side)
< values[i]) {
values[i] =
Player::heuristic(*potential_board,
ply2_moves[j], opp_side);
}
}
}
int highest = 0;
for (int i = 0; i < ply1_moves.size(); ++i) {
if (values[i] > values[highest]) {
highest = i;
}
}
return ply1_moves[highest];
}
}
/*
* Gets the best move based on the minmax technique using the naive heuristic
*/
Move* Player::miniMaxNaive(){
std::vector<Move*> ply1_moves = Player::possibleMoves(my_board, my_side);
if (ply1_moves.size() == 0) {
return NULL;
} else {
std::vector < Move * > ply2_moves;
int values[ply1_moves.size()];
for (int i = 0; i < ply1_moves.size(); i++) {
values[i] = Player::naiveHeuristic(my_board,
ply1_moves[i], my_side);
}
for (int i = 0; i < ply1_moves.size(); i++) {
Board *potential_board = my_board.copy();
potential_board->doMove(ply1_moves[i], my_side);
ply2_moves = Player::possibleMoves(*potential_board, opp_side);
for (int j = 0; j < ply2_moves.size(); ++j) {
if (Player::naiveHeuristic(*potential_board, ply2_moves[j],
opp_side) < values[i]) {
values[i] =
Player::naiveHeuristic(*potential_board,
ply2_moves[j], opp_side);
}
}
}
int highest = 0;
for (int i = 0; i < ply1_moves.size(); ++i) {
if (values[i] > values[highest]) {
highest = i;
}
}
return ply1_moves[highest];
}
}
/*
* Finds all possible moves for a given side
*
* Returns a vector of pointers to each move
*/
std::vector<Move *> Player::possibleMoves(Board board, Side side){
std::vector<Move*> moves;
for (int i = 0; i < 8; ++i) {
for (int j = 0; j < 8; ++j) {
Move *m = new Move(i,j);
if(board.checkMove(m, side)) {
moves.push_back(m);
}
}
}
return moves;
}
/*
* Calculates a score based on a given move played on the current board
*
* Returns an int value of the score--bigger is better!
*/
int Player::heuristic(Board board, Move *potential_move, Side side) {
Board *potential_board = board.copy();
potential_board->doMove(potential_move, side);
int my_score = 0, opp_score = 0;
for (int i = 0; i < 8; ++i) {
for (int j = 0; j < 8; ++j) {
if(potential_board->get(my_side, i, j)) {
my_score += board_values[i][j];
}
if (potential_board->get(opp_side, i, j)){
opp_score += board_values[i][j];
}
}
}
return my_score - opp_score;
}
/*
* Calculates a score based on the number of pieces on the board
*/
int Player::naiveHeuristic(Board board, Move *potential_move, Side side) {
Board *potential_board = board.copy();
potential_board->doMove(potential_move, side);
return potential_board->countBlack() - potential_board->countWhite();
}