-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathplayer.cpp
More file actions
217 lines (198 loc) · 6.44 KB
/
player.cpp
File metadata and controls
217 lines (198 loc) · 6.44 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
#include "player.hpp"
#include "common.hpp"
#include <climits>
/*
* 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.
*/
/*
* aldjfhlakjfhlakjsdhf
*/
Player::Player(Side side) {
// Will be set to true in test_minimax.cpp.
testingMinimax = false;
if (side == WHITE)
{
//std::cerr << 'd' << std::endl;
pside = WHITE;
oside = BLACK;
} else {
//std::cerr << 'e' << std::endl;
pside = BLACK;
oside = WHITE;
}
board = new Board();
/*
* TODO: Do any initialization you need to do here (setting up the board,
* precalculating things, etc.) However, remember that you will only have
* 30 seconds.
*/
}
/*
* Destructor for the player.
*/
Player::~Player() {
delete board;
}
int Player::heuristic(Board *b)
{
int sum = 0;
if (!testingMinimax)
{
// Greater value for corner pieces
sum += 3 * (b->get(pside, 0, 0) + b->get(pside, 0, 7) +
b->get(pside, 7, 0) + b->get(pside, 7, 7));
// Lower value for pieces adjacent to a corner
sum -= 2 * (b->get(pside, 0, 1) + b->get(pside, 1, 0) +
b->get(pside, 6, 0) + b->get(pside, 0, 6) +
b->get(pside, 7, 1) + b->get(pside, 1, 7) +
b->get(pside, 7, 6) + b->get(pside, 6, 7));
// Lower value for pieces adjacent and diagonal to a corner
sum -= 3 * (b->get(pside, 1, 1) + b->get(pside, 1, 6) +
b->get(pside, 6, 1) + b->get(pside, 6, 6));
// Greater value for side pieces
sum += 2 * (b->get(pside, 0, 2) + b->get(pside, 0, 3) +
b->get(pside, 0, 4) + b->get(pside, 0, 5) +
b->get(pside, 7, 2) + b->get(pside, 7, 3) +
b->get(pside, 7, 4) + b->get(pside, 7, 5) +
b->get(pside, 2, 0) + b->get(pside, 3, 0) +
b->get(pside, 4, 0) + b->get(pside, 5, 0) +
b->get(pside, 2, 7) + b->get(pside, 3, 7) +
b->get(pside, 4, 7) + b->get(pside, 5, 7));
}
if (pside == WHITE)
{
return b->countWhite() - b->countBlack() + sum;
}
else
{
//sum -= 5* (b->get(oside, 0, 0) + b->get(oside, 0, 7) +
//b->get(oside, 7, 0) + b->get(oside, 7, 7));
return b->countBlack() - b->countWhite() + sum;
}
}
void Player::printt(Board *b) {
for (int i = 0; i < 8; ++i)
{
for (int j = 0; j < 8; ++j)
{
if (b->get(WHITE, j, i)) std::cerr << 'w';
else if (b->get(BLACK, j, i)) std::cerr << 'b';
else std::cerr << '-';
}
std::cerr << std::endl;
}
}
int Player::minimax(bool turn, int depth, Board *bd, int a, int b) {
if (depth == 0)
{
int count = heuristic(bd);
//printt(b);
//std::cerr << "c " << count << std::endl;
return count;
}
int best = 0;
if (!turn)
{
//oside
best = INT_MAX;
if (!bd->hasMoves(oside))
{
Board *newb = bd->copy();
return minimax(!turn, depth-1, newb, a, b);
}
for (int i = 0; i < 8; i++) {
for (int j = 0; j < 8; j++) {
Move move(i, j);
if (bd->checkMove(&move, oside)) {
Board *newb = bd->copy();
newb->doMove(&move, oside);
//printt(newb);
int v = minimax(!turn, depth-1, newb, a, b);
best = min(v, best);
b = min(b, best);
if (b <= a)
{
return best;
}
}
}
}
return best;
} else {
//pside
best = INT_MIN;
if (!bd->hasMoves(pside))
{
Board *newb = bd->copy();
return minimax(!turn, depth-1, newb, a, b);
}
for (int i = 0; i < 8; i++) {
for (int j = 0; j < 8; j++) {
Move move(i, j);
if (bd->checkMove(&move, pside)) {
//std::cerr << move.getX() << " " << move.getY() << std::endl;
Board *newb = bd->copy();
newb->doMove(&move, pside);
int v = minimax(!turn, depth-1, newb, a, b);
best = max(v, best);
a = max(a, best);
if (b <= a)
{
return best;
}
}
}
}
return best;
}
return 0;
}
/*
* 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.
*/
Move *Player::doMove(Move *opponentsMove, int msLeft) {
/*
* TODO: Implement how moves your AI should play here. You should first
* process the opponent's opponents move before calculating your own move
*/
Move *m = new Move(0,0);
if (opponentsMove != nullptr) board->doMove(opponentsMove, oside);
if (!board->hasMoves(pside)) return nullptr;
int best = INT_MIN;
for (int i = 0; i < 8; i++) {
for (int j = 0; j < 8; j++) {
Move move(i, j);
if (board->checkMove(&move, pside)) {
//std::cerr << move.getX() << " " << move.getY() << std::endl;
Board *newb = board->copy();
newb->doMove(&move, pside);
int v;
if (testingMinimax) v = minimax(false, 1, newb, INT_MIN, INT_MAX);
else v = minimax(false, 6, newb, INT_MIN, INT_MAX);
//std::cerr << v << std::endl;
if (v > best)
{
best = v;
m->setX(i);
m->setY(j);
}
}
}
}
if (!board->checkMove(m, pside)) return nullptr;
board->doMove(m, pside);
//std::cerr << m->getX() << " " << m->getY() << std::endl;
return m;
}