-
Notifications
You must be signed in to change notification settings - Fork 0
/
reversi_bl.c
474 lines (430 loc) · 11.7 KB
/
reversi_bl.c
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
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
#include "reversi_bl.h"
int *reversi_board=NULL;
int reversi_diffficulties[] = {1,2,3,4};//possible didiffficulty levels. this is a global array so it won't be freed at any point on the game.
/* all possible move directions({delta_x,delta_y}) on board*/
int move_directions[8][2] = {{1,1},{-1,-1},{1,-1},{-1,1},{0,1},{1,0},{-1,0},{0,-1}};
/*region score matrix*/
int region_scores[REVERSI_ROWS][REVERSI_COLS] =
{{R5, R4, R3, R3, R3, R3, R4, R5},
{R4, R4, R2, R2, R2, R2, R4, R4},
{R3, R2, R1, R1, R1, R1, R2, R3},
{R3, R2, R1, R1, R1, R1, R2, R3},
{R3, R2, R1, R1, R1, R1, R2, R3},
{R3, R2, R1, R1, R1, R1, R2, R3},
{R4, R4, R2, R2, R2, R2, R4, R4},
{R5, R4, R3, R3, R3, R3, R4, R5}};
char* rv_get_name()
{
return REVERSI_NAME;
}
/*this function makes and returns a satrting board(logic, not gui) for reversi game
on failure return null*/
int* rv_get_initial_state()
{
int i=0,j=0;
int* initial_board = (int*)calloc(REVERSI_ROWS*REVERSI_COLS,sizeof(int));
if(initial_board==NULL){
return NULL;
}
for (i=0; i< REVERSI_ROWS; i++){
for(j=0; j <REVERSI_COLS; j++){
initial_board[i*REVERSI_ROWS + j] = REVERSI_NO_PLAYER;
}
}
/* initial piece layout */
initial_board[PLAYER_1_START_1] = REVERSI_PLAYER_1;
initial_board[PLAYER_2_START_1] = REVERSI_PLAYER_2;
initial_board[PLAYER_1_START_2] = REVERSI_PLAYER_1;
initial_board[PLAYER_2_START_2] = REVERSI_PLAYER_2;
return initial_board;
}
/* creates a node and element for each child-state, and adds to list*/
int rv_make_node(int* game_state, int row, int col, int player)
{
vertex node;
node = make_node(row*REVERSI_ROWS + col,game_state,rv_get_state_score(game_state));
if (node == NULL)
{
free(game_state);
return -1;
}
return 1;
}
/* creates a minimax node and element for each child-state(game_state+move), and adds to list
on failure return null*/
int rv_add_to_children_list(linked_list list, int* game_state, int row, int col, int player)
{
int* moved_state=NULL;
vertex node=NULL;
element new_elem=NULL,prev_tail=NULL;
moved_state = rv_copy_and_make_move(game_state,row,col,player);
if(moved_state==NULL){
return -1;
}
node = make_node(row*REVERSI_ROWS + col,moved_state,rv_get_state_score(moved_state));
if (node == NULL)
{
free(moved_state);
return -1;
}
//create new element
new_elem = new_element();
if (new_elem == NULL)
{
free(moved_state);
free(node);
return -1;
}
//assign node to element
new_elem->node = node;
// add to list
if (list->head == NULL)
{
list->head = new_elem;
list->tail = new_elem;
}
else
{
prev_tail = list->tail;
prev_tail->next = new_elem;
new_elem->prev = prev_tail;
list->tail = new_elem;
}
return 1;
}
/*this functions returns a list of minimax elements.
* each of these elements represents a possible move that a player can do in his turn
* if game_state is the current board.
* on success the function returns with *error=0, on failure returns with *error=-1*/
linked_list rv_get_state_children(int* game_state, int player,int *error)
{
int i=0,j=0,return_value=0;
element run_elem=NULL;
linked_list list = new_list();
for (i=0; i < REVERSI_ROWS; i++)
{
for(j=0; j < REVERSI_COLS; j++)
{
//if we may place at the slot i*TIC_TAC_TOE_ROWS + j on the board-add this move as a child
if (rv_is_valid_move(game_state, player,i,j))
{
return_value = rv_add_to_children_list(list,game_state,i,j,player);
if (return_value==-1){
break;
}
}
}
if (return_value==-1){
break;
}
}
if (return_value==-1){//if ttc_add_to_children_list failed at any time within the for- clear list and return with failure
for (run_elem=list->head;run_elem!=NULL && run_elem->next!=NULL;run_elem=run_elem->next){
free(run_elem->node->game_state);
free(run_elem->node);
if (run_elem->prev!=NULL){
free(run_elem->prev);
}
}
//free last one
if (run_elem!=NULL){
free(run_elem->node->game_state);
free(run_elem->node);
free(run_elem);
}
*error=-1;
return NULL;
}
else if (list->head==NULL){//if no childern where created- this means the enemy has no moves
if (rv_player_has_moves(game_state,(-1)*player)==1){//pass move
rv_add_to_children_list(list,game_state,-1,-1,player);
}
}
*error=0;
return list;
}
/*this functions returns a list of minimax elements.
* each of these elements represents a possible move that a player can do in his turn
* if game_state is the current board.
* on success the function returns with *error=0, on failure returns with *error=-1*/
int* rv_copy_and_make_move(int* game_state, int move_row, int move_col, int player)
{
int i=0,j=0;
//assign new board
int* copied_state = (int*)calloc(REVERSI_ROWS * REVERSI_COLS, sizeof(int));
if (copied_state==NULL){
return NULL;
}
//copy current state
for (i=0; i < REVERSI_ROWS; i++)
{
for(j=0; j < REVERSI_COLS; j++)
{
copied_state[i*REVERSI_ROWS + j] = game_state[i*REVERSI_ROWS + j];
}
}
if (move_row==-1 && move_col==-1){//pass move(return the same board-the enemy can't make any move)
return copied_state;
}
//make move
rv_make_move(copied_state,move_row,move_col,player);
return copied_state;
}
/*return the number of pieces a player have on the board*/
int get_player_pieces(int* game_state, int player)
{
int i,j,count=0;
for (i=0; i < REVERSI_ROWS; i++)
{
for(j=0; j < REVERSI_COLS; j++)
{
if (game_state[i*REVERSI_ROWS +j] == player)
{
count++;
}
}
}
return count;
}
/*this is a function which evaluate each board.
* we use this function for the minimax algorithm */
int rv_get_state_score(int* game_state)
{
int i=0,j = 0;
int score=0;
int player_pieces =0, other_pieces = 0;
if (rv_is_game_over(game_state))
{
player_pieces = get_player_pieces(game_state,ENUM_PLAYER_1);
other_pieces = get_player_pieces(game_state,ENUM_PLAYER_2);
if (player_pieces > other_pieces)
{
return INT_MAX;
}
else if (player_pieces < other_pieces)
{
return INT_MIN;
}
else // tie
{
return 0 ;
}
}
for (i=0; i < REVERSI_ROWS; i++)
{
for(j=0; j < REVERSI_COLS; j++)
{
/* if position is empty: multiply by zero, else, (+1)/(-1) according to piece*/
/*score += game_state[i*REVERSI_ROWS + j] * region_scores[i][j]; */
if (game_state[i*REVERSI_ROWS + j] == ENUM_PLAYER_1)
{
score += region_scores[i][j];
}
else if (game_state[i*REVERSI_ROWS + j] == ENUM_PLAYER_2)
{
score -= region_scores[i][j];
}
}
}
return score;
}
/* get difficult level for game*/
int* rv_get_difficulty_levels()
{
return reversi_diffficulties;
}
/*make a given move on the board, flips other player's pieces accordingly*/
int rv_make_move(int* game_state, int rows, int cols, int player)
{
int i=0,t_rows=-1,t_cols=-1, length=0;
int other = player*(-1);
if (!rv_is_valid_move(game_state,player,rows,cols))
{
return 0;
}
// for every possible direction check if can flip enemys pieces
for (i=0;i<REVERSI_ROWS; i++)
{
length = 0;
t_rows = rows + move_directions[i][0];
t_cols = cols + move_directions[i][1];
//if the derection lead as out of the board, try another
if (t_cols>=REVERSI_COLS || t_rows>=REVERSI_ROWS|| t_cols<0 || t_rows<0){
continue;
}
while (game_state[t_rows*REVERSI_ROWS + t_cols] == other)
{
length++;
t_rows += move_directions[i][0];
t_cols += move_directions[i][1];
if (t_cols>=REVERSI_COLS || t_rows>=REVERSI_ROWS|| t_cols<0 || t_rows<0){
break;
}
// if encoutered enemy piece at that derection
if (game_state[t_rows*REVERSI_ROWS + t_cols] == player )
{
// retract moves and change colors
while (t_rows != rows || t_cols != cols)
{
t_rows -= move_directions[i][0];
t_cols -= move_directions[i][1];
game_state[t_rows*REVERSI_ROWS + t_cols] = player;
}
// finished fliping in this direction, break
break;
}
}
}
return 1;
}
/* a move is valid iff another piece of the player is encoutered
in one or more- this fuction checks if the move is valid*/
int rv_is_valid_move (int *game_state, int player, int rows, int cols)
{
int i=0,t_rows=-1,t_cols=-1, length=0;
int other = player*(-1);
//if there is another piece at the same place,can't make this move
if (game_state[rows*REVERSI_ROWS+cols]!=0)
{
return 0;
}
// for every possible direction flip enemys pieces
for (i=0;i<REVERSI_ROWS; i++)
{
length = 0;
t_rows = rows + move_directions[i][0];
t_cols = cols + move_directions[i][1];
//if the derection lead as out of the board, try another
if (t_cols>=REVERSI_COLS || t_rows>=REVERSI_ROWS || t_cols<0 || t_rows<0){
continue;
}
// if encoutered enemy piece at that derection
while (game_state[t_rows*REVERSI_ROWS + t_cols] == other)
{
length++;
t_rows += move_directions[i][0];
t_cols += move_directions[i][1];
if (t_cols>=REVERSI_COLS || t_rows>=REVERSI_ROWS|| t_cols<0 || t_rows<0){
break;
}
// if encoutered another piece
if (game_state[t_rows*REVERSI_ROWS + t_cols] == player )
{
// retract moves and change colors
while (t_rows != rows || t_cols != cols)
{
t_rows -= move_directions[i][0];
t_cols -= move_directions[i][1];
//game_state[t_rows*REVERSI_ROWS + t_cols] = player;
}
// finished fliping in this direction, break
return 1;
}
}
}
return 0;
}
/* game is over if : (1) board is full or (2) no-one can place pieces legally-this function checks if one of the condition exist*/
int rv_is_game_over(int* game_state)
{
int i,j;
for (i=0; i < REVERSI_ROWS; i++)
{
for(j=0; j < REVERSI_COLS; j++)
{
if (rv_is_valid_move(game_state,REVERSI_PLAYER_1,i,j)==1)
{
return 0;
}
if (rv_is_valid_move(game_state,REVERSI_PLAYER_2,i,j)==1)
{
return 0;
}
}
}
return 1;
}
/* checks if there is a victory in passed: return 0 for tie, 1/-1 for victory . for error return -2*/
int rv_is_victory(int* game_state)
{
int player_pieces,other_pieces;
if (rv_is_game_over(game_state))
{
player_pieces = get_player_pieces(game_state,REVERSI_PLAYER_1);
other_pieces = get_player_pieces(game_state,REVERSI_PLAYER_2);
if (player_pieces > other_pieces)
{
return REVERSI_PLAYER_1;
}
else if (player_pieces < other_pieces)
{
return REVERSI_PLAYER_2;
}
else // tie
{
return 0 ;
}
}
return -2;
}
/*this unction interpets a mouse cliking on the GUI game board into a move on the board*/
int rv_handle_mouse_button_down (SDL_Event *event, int* game_state,int player)
{
int x=0,y=0;
int succes;
x=event->button.x;
y=event->button.y;
if(rv_is_game_over(game_state))
{
return 0;
}
succes=rv_make_move(game_state,(y-RVR_YOFFSET)/RVR_WBTN,(x-RVR_XOFFSET)/RVR_HBTN,player);
if(succes==0)
{
if (rv_player_has_moves(game_state,player)==0){
return 2;//pass move to next player
}
return 0;
}
return 1;
}
/*this function act as a wraper for minimax while playing reversi*/
int rv_handle_computer_turn(int* game_state, int depth,int player)
{
int comp_move;
if (rv_player_has_moves(game_state,player)==0){
return 0;
}
do{
if (player==-1){
comp_move = get_computer_move(game_state, depth, rv_get_state_children);
}
else {
comp_move=get_suggested_move(game_state,depth, rv_get_state_children);
}
if (comp_move<0){
return -1;
}
if (!rv_is_valid_move(game_state,player,comp_move/REVERSI_ROWS,comp_move%REVERSI_COLS)){
return -2;//if the minimax algorithm return unvalid move,an error accord
}
rv_make_move(game_state,comp_move/REVERSI_ROWS,comp_move%REVERSI_COLS,player);
} while (rv_player_has_moves(game_state,-1*player)==0 && rv_player_has_moves(game_state,player)==1);
return 0;
}
/*check if given player can make a move. if cam returns 1,else 0.*/
int rv_player_has_moves(int* game_state, int player)
{
int i,j;
for (i=0; i < REVERSI_ROWS; i++)
{
for(j=0; j < REVERSI_COLS; j++)
{
if (rv_is_valid_move(game_state, player,i,j))
{
return 1;
}
}
}
return 0;
}