-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathothello.cu
286 lines (257 loc) · 9 KB
/
othello.cu
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
#include <stdlib.h>
#include <stdio.h>
#include <math.h>
#include <sys/time.h>
#include "othello_kernel.cu"
#include "othello_shared.cu"
#include "alpha_beta.cu"
int counter = 1;
int maxdiffstrategy(int, int *);
int human (int player, int * board) {
int move;
printf("%c to move:", nameof(player)); scanf("%d", &move);
return move;
}
int randomstrategy(int player, int * board) {
int r, * moves;
moves = legalmoves(player, board);
r = moves[(rand() % moves[0]) + 1];
free(moves);
return(r);
}
/* minmax is called to do a "ply" move lookahead, using
evalfn (i.e., the utility function) to evaluate (terminal) boards at
the end of lookahead. Minmax starts by finding and simulating each legal
move by player. The move that leads to the best (maximum backed-up score)
resulting board, is the move (i.e., an integer representing a
board location) that is returned by minmax.
The score of each board (resulting from each move)
is determined by the function diffeval if no player can
move from the resulting board (i.e., game over), by function
maxchoice if only player can move from the resulting board,
or by function minchoice if the opponent can move from the
resulting board.
Importantly, minmax assumes that ply >= 1.
You are to modify minmax so that it exploits alphabeta pruning,
and so that it randomly selects amongst the best moves for player.
*/
int minmax (int player, int * board, int ply) {
int i, max, ntm, newscore, bestmove, * moves, * newboard;
int maxchoice (int, int *, int);
int minchoice (int, int *, int);
moves = legalmoves(player, board); /* get all legal moves for player */
max = LOSS - 1; /* any legal move will exceed this score */
for (i=1; i <= moves[0]; i++) {
newboard = copyboard(board);
makemove(moves[i], player, newboard);
ntm = nexttoplay(newboard, player, 0);
if (ntm == 0) { /* game over, so determine winner */
newscore = diffeval(player, newboard);
if (newscore > 0) newscore = WIN; /* a win for player */
if (newscore < 0) newscore = LOSS; /* a win for opp */
}
if (ntm == player) /* opponent cannot move */
newscore = maxchoice(player, newboard, ply-1);
if (ntm == opponent(player))
newscore = minchoice(player, newboard, ply-1);
if (newscore > max) {
max = newscore;
bestmove = moves[i]; /* a better move found */
}
free(newboard);
}
free(moves);
return(bestmove);
}
int cudaminmax (int player, int * board, int ply) {
Node *n = new Node;
n->player = player;
n->board = board;
n->parent = NULL;
int bestmove = search(n, player, ply);
return(bestmove);
}
/* If ply = 0, then maxchoice should return diffeval(player, board),
else the legal moves that can be made by player from board should
be simulated. maxchoice should return the MAXIMUM board score
from among the possibilities. The backed-up score of each
board (resulting from each player move) is determined
by function maxchoice if only player can move from the resulting board,
by function minchoice if the opponent can move from the resulting board,
is WIN if a win for player, a LOSS if a win for opponent, and
a 0 if a draw.
If two or more boards tie for the maximum backed score, then return
the move that appears first (lowest location) in the moves
array leading to a maximum-score board.
*/
int maxchoice (int player, int * board, int ply) {
int i, max, ntm, newscore, * moves, * newboard;
int minchoice (int, int *, int);
if (ply == 0) return(diffeval (player, board));
moves = legalmoves(player, board);
max = LOSS - 1;
for (i=1; i <= moves[0]; i++) {
newboard = copyboard(board);
makemove(moves[i], player, newboard);
ntm = nexttoplay(newboard, player, 0);
if (ntm == 0) {
newscore = diffeval(player, newboard);
if (newscore > 0) newscore = WIN;
if (newscore < 0) newscore = LOSS;
}
if (ntm == player)
newscore = maxchoice(player, newboard, ply-1);
if (ntm == opponent(player))
newscore = minchoice(player, newboard, ply-1);
if (newscore > max) max = newscore;
free(newboard);
}
free(moves);
return(max);
}
/* If ply = 0, then minchoice should return the diffeval(player, board),
else the legal moves that can be made by player's opponent from board should
be simulated. minchoice should return the MINIMUM backed up board score
from among the possibilities. The backed up score of each board
(resulting from each opponent move) is determined by function maxchoice
if player can move from the resulting board,
by function minchoice if only the opponent can move
from the resulting board, is WIN if a win for player,
a LOSS if a win for opponent, and a 0 if a draw.
If two or more BOARDS tie for the minimum score, then return
the move that appears first (lowest location) in the moves
array leading to a minimum-score board.
Advanced: DO NOT worry about this,
but note that minchoice and maxchoice could be combined
easily into a single function, finding the board
with minimum score, s, is equivalent to finding the board
with maximum -1 * s. One would have to add an additional
parameter to decide whether to multiply by a -1 factor or
not in computing a board score in this combined function.
With a bit more work, one could combine all three
functions, minmax, maxchoice, and minchoice, into a single
function.
*/
int minchoice (int player, int * board, int ply) {
int i, min, ntm, newscore, * moves, * newboard;
if (ply == 0) return(diffeval (player, board));
moves = legalmoves(opponent(player), board);
min = WIN+1;
for (i=1; i <= moves[0]; i++) {
newboard = copyboard(board);
makemove(moves[i], opponent(player), newboard);
ntm = nexttoplay(newboard, opponent(player), 0);
if (ntm == 0) {
newscore = diffeval(player, newboard);
if (newscore > 0) newscore = WIN;
if (newscore < 0) newscore = LOSS;
}
if (ntm == player)
newscore = maxchoice(player, newboard, ply-1);
if (ntm == opponent(player))
newscore = minchoice(player, newboard, ply-1);
if (newscore < min) min = newscore;
free(newboard);
}
free(moves);
return(min);
}
int maxdiffstrategy(int player, int * board) { /* 1 ply lookahead */
return(minmax(player, board, counter)); /* diffeval as utility fn */
}
int cudamaxdiffstrategy(int player, int * board) { /* 1 ply lookahead */
return(cudaminmax(player, board, counter)); /* diffeval as utility fn */
}
void getmove (int (* strategy) (int, int *), int player, int * board,
int printflag) {
int move;
if (printflag) printboard(board);
move = (* strategy)(player, board);
if (legalp(move, player, board)) {
if (printflag) printf("%c moves to %d\n", nameof(player), move);
makemove(move, player, board);
}
else {
printf("Illegal move %d\n", move);
getmove(strategy, player, board, printflag);
}
}
int * randomboard (void) {
int player, oldplayer, i, * board;
board = initialboard();
player = BLACK;
i=1;
do {
if (player == BLACK) getmove(randomstrategy, BLACK, board, 0);
else getmove(randomstrategy, WHITE, board, 0);
oldplayer = player;
player = nexttoplay(board, player, 0);
if (oldplayer == player) {
free(board);
return(randomboard());
}
i++;
}
while (player != 0 && i<=8);
if (player==0) {
free(board);
return(randomboard());
}
else return(board);
}
void othello (int algo, bool disp) {
int * board;
int player;
board = initialboard();
player = BLACK;
if (algo == 0){
do {
if (player == BLACK) getmove(maxdiffstrategy, BLACK, board, disp);
else getmove(maxdiffstrategy, WHITE, board, disp);
player = nexttoplay(board, player, disp);
}
while (player != 0);
}
if (algo == 1) {
do {
if (player == BLACK) getmove(maxdiffstrategy, BLACK, board, disp);
else getmove(cudamaxdiffstrategy, WHITE, board, disp);
player = nexttoplay(board, player, disp);
}
while (player != 0);
}
if (algo == 2) {
do {
if (player == BLACK) getmove(cudamaxdiffstrategy, BLACK, board, disp);
else getmove(cudamaxdiffstrategy, WHITE, board, disp);
player = nexttoplay(board, player, disp);
}
while (player != 0);
}
printf("[%c=%d %c=%d]\n",
nameof(BLACK), count(BLACK, board), nameof(WHITE), count(WHITE, board));
// printboard(board);
}
int main (int argc, char** argv) {
for (int i=1; i < 7; i++) {
counter = i;
for(int j=0; j < 3; j++) {
if (j == 0) {
printf("CPU-CPU Depth: %d \n", counter);
} else if (j == 1) {
printf("CPU-GPU Depth: %d \n", counter);
} else if (j == 2) {
printf("GPU-GPU Depth: %d \n", counter);
}
struct timeval start, end;
gettimeofday(&start, NULL);
othello(j, 0);
gettimeofday(&end, NULL);
long seconds = (end.tv_sec - start.tv_sec);
long micros = ((seconds * 1000000) + end.tv_usec) - (start.tv_usec);
float millis = ((float) micros) / 1000;
printf("The elapsed time is %.2f millis\n", millis);
}
}
return 0;
}