-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcore.cpp
312 lines (300 loc) · 9.92 KB
/
core.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
//All code is released to the public domain under the terms of [http://unlicense.org]
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <vector>
#include <utility>
//returns a pair <direction, coordinate> where direction is North, Northeast, etc (clockwise)
//and coordinate is in the form x*8 + y
std::vector< std::pair<int, int> > get_directions(int board[][8], int x, int y, int color) {
std::vector< std::pair<int, int> > directions;
if(board[x][y]) {
return directions;
}
//north
if((y < 6) && (board[x][y+1] == -color)) {
for(int i=y+2; i < 8; i++) {
if(!board[x][i]) {
break;
} else if(board[x][i] == color) {
directions.push_back(std::make_pair<int, int>(0, x*8+i));
break;
}
}
}
//northeast
if((y < 6) && (x < 6) && (board[x+1][y+1] == -color)) {
for(int i=0; (x+i+2 < 8) && (y+i+2 < 8); i++) {
if(!board[x+i+2][y+i+2]) {
break;
} else if(board[x+i+2][y+i+2] == color) {
directions.push_back(std::make_pair<int, int>(1, (x+i+2)*8+(y+i+2)));
break;
}
}
}
//east
if((x < 6) && (board[x+1][y] == -color)) {
for(int i=x+2; i < 8; i++) {
if(!board[i][y]) {
break;
} else if(board[i][y] == color) {
directions.push_back(std::make_pair<int, int>(2, i*8+y));
break;
}
}
}
//southeast
if((y > 1) && (x < 6) && (board[x+1][y-1] == -color)) {
for(int i=0; (x+i+2 < 8) && (y-i-2 >= 0); i++) {
if(!board[x+i+2][y-i-2]) {
break;
} else if(board[x+i+2][y-i-2] == color) {
directions.push_back(std::make_pair<int, int>(3, (x+i+2)*8+(y-i-2)));
break;
}
}
}
//south
if((y > 1) && (board[x][y-1] == -color)) {
for(int i=y-2; i >= 0; i--) {
if(!board[x][i]) {
break;
} else if(board[x][i] == color) {
directions.push_back(std::make_pair<int, int>(4, x*8+i));
break;
}
}
}
//southwest
if((y > 1) && (x > 1) && (board[x-1][y-1] == -color)) {
for(int i=0; (x-i-2 >= 0) && (y-i-2 >= 0); i++) {
if(!board[x-i-2][y-i-2]) {
break;
} else if(board[x-i-2][y-i-2] == color) {
directions.push_back(std::make_pair<int, int>(5, (x-i-2)*8+(y-i-2)));
break;
}
}
}
//west
if((x > 1) && (board[x-1][y] == -color)) {
for(int i=x-2; i >= 0; i--) {
if(!board[i][y]) {
break;
} else if(board[i][y] == color) {
directions.push_back(std::make_pair<int, int>(6, i*8+y));
break;
}
}
}
//northwest
if((y < 6) && (x > 1) && (board[x-1][y+1] == -color)) {
for(int i=0; (x-i-2 >= 0) && (y+i+2 < 8); i++) {
if(!board[x-i-2][y+i+2]) {
break;
} else if(board[x-i-2][y+i+2] == color) {
directions.push_back(std::make_pair<int, int>(7, (x-i-2)*8+(y+i+2)));
break;
}
}
}
return directions;
}
//returns all moves for a players on the current board. Each pair has a coordinate in the form
//x*8+y, and a vector of pairs, each pair contains a direction and an endpoint in the form x*8+y
std::vector< std::pair<int, std::vector< std::pair<int, int> > > > get_moves(int board[][8], int color) {
std::vector< std::pair<int, std::vector< std::pair<int, int> > > > moves;
for(int i=0; i < 8; i++) {
for(int j=0; j < 8; j++) {
moves.push_back(std::make_pair<int, std::vector< std::pair<int, int> > >(i*8+j, get_directions(board, i, j, color)));
if(!moves.back().second.size()) {
moves.pop_back();
}
}
}
return moves;
}
//makes a move and flips the appropriate pieces
void make_move(int board[][8], int x, int y, int color, std::vector< std::pair<int, int> > directions) {
for(auto it=directions.begin(); it != directions.end(); it++) {
int i = x;
int j = y;
while((i != ((*it).second/8)) || (j != ((*it).second&7))) {
board[i][j] = color;
if(i < ((*it).second/8)) {
i++;
} else if((i > (*it).second/8)) {
i--;
}
if(j < ((*it).second&7)) {
j++;
} else if(j > ((*it).second&7)) {
j--;
}
}
}
}
//undoes a move (needs directions data so it can unflip stuff)
void undo_move(int board[][8], int x, int y, int color, std::vector< std::pair<int, int> > directions) {
for(auto it=directions.begin(); it != directions.end(); it++) {
int i = x;
int j = y;
while((i != ((*it).second/8)) || (j != ((*it).second&7))) {
board[i][j] = -color;
if(i < ((*it).second/8)) {
i++;
} else if((i > (*it).second/8)) {
i--;
}
if(j < ((*it).second&7)) {
j++;
} else if(j > ((*it).second&7)) {
j--;
}
}
board[i][j] = color;
}
board[x][y] = 0;
}
void print(int board[][8]) {
for(int i=7; i >= 0; i--) {
printf("%d ", i);
for(int j=0; j < 8; j++) {
if(!board[j][i]) {
printf("-");
} else if(board[j][i] == 1) {
printf("O");
} else {
printf("X");
}
}
printf("\n");
}
printf(" ");
for(int i=0; i < 8; i++) {
printf("%d", i);
}
printf("\n\n");
}
int score(int board[][8], int color) {
int sum = 0;
for(int i=0; i < 8; i++) {
for(int j=0; j < 8; j++) {
sum += board[i][j];
}
}
return sum * color;
}
//Largely the same pseudocode from the negamax wikipedia article, but adapted for the rules of othello
int negamax_aux(int board[][8], int color, int depth, int alpha, int beta) {
if(depth == 0) {
return score(board, color);
}
std::vector< std::pair<int, std::vector< std::pair<int, int> > > > moves = get_moves(board, color);
if(moves.size() == 0) {
if(get_moves(board, -color).size() == 0) {
return score(board, color);
}
int val = -negamax_aux(board, -color, depth-1, -beta, -alpha);
if(val >= beta) {
return val;
}
if(val > alpha) {
alpha = val;
}
} else {
for(auto it=moves.begin(); it != moves.end(); it++) {
make_move(board, (*it).first/8, (*it).first&7, color, (*it).second);
int val = -negamax_aux(board, -color, depth-1, -beta, -alpha);
undo_move(board, (*it).first/8, (*it).first&7, color, (*it).second);
if(val >= beta) {
return val;
}
if(val > alpha) {
alpha = val;
}
}
}
return alpha;
}
//parent function to negamax_aux, this function actually maintains the current best move
int negamax(int board[][8], int color, int depth) {
int alpha = -65;
int beta = 65;
std::vector< std::pair<int, std::vector< std::pair<int, int> > > > moves = get_moves(board, color);
int move = moves[0].first;
for(auto it=moves.begin(); it != moves.end(); it++) {
make_move(board, (*it).first/8, (*it).first&7, color, (*it).second);
int val = -negamax_aux(board, -color, depth-1, -beta, -alpha);
undo_move(board, (*it).first/8, (*it).first&7, color, (*it).second);
if(val >= beta) {
return (*it).first;
}
if(val > alpha) {
alpha = val;
move = (*it).first;
}
}
return move;
}
//run with ./reversi [int] where int is the number of ply the AI searches deep. Default is 3 ply.
int main(int argc, char **argv) {
int depth = 3;
if(argc > 1) {
depth = atol(argv[1]);
}
depth *= 2;
int board[8][8];
memset(board, 0, sizeof(board));
board[3][3] = board[4][4] = -1;
board[3][4] = board[4][3] = 1;
int turn = -1;
while(true) {
print(board);
std::vector< std::pair<int, std::vector< std::pair<int, int> > > > moves= get_moves(board, turn);
printf("available moves: ");
for(auto it=moves.begin(); it != moves.end(); it++) {
printf("(%d, %d) ", (*it).first/8, (*it).first%8);
}
printf("\n");
if(moves.size() == 0) {
turn = -turn;
moves = get_moves(board, turn);
if(moves.size() == 0) {
printf("final score: %d\n", score(board, -1));
return 0;
}
} else {
int x, y;
if(turn == -1) {
scanf("%d %d", &x, &y);
for(auto it=moves.begin(); it != moves.end(); it++) {
if(x*8+y == ((*it).first)) {
printf("chose: %d %d\n", x, y);
make_move(board, x, y, turn, (*it).second);
turn = -turn;
break;
}
}
} else {
x = negamax(board, turn, depth);
for(auto it=moves.begin(); it != moves.end(); it++) {
if(x == ((*it).first)) {
printf("chose: %d %d\n", x/8, x%8);
make_move(board, x/8, x%8, turn, (*it).second);
turn = -turn;
break;
}
}
}
}
}
return 0;
}
//Music while coding:
//Willow Beats - (All of their music)
//https://soundcloud.com/willowbeats
//
//Peter Kusiv - Someone Told Me
//https://soundcloud.com/peer-kusiv/peer-kusiv-someone-told-me