-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathawari.c
More file actions
531 lines (450 loc) · 12.9 KB
/
Copy pathawari.c
File metadata and controls
531 lines (450 loc) · 12.9 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
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
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
/*
Awari.
A simple two players game implemented with the minimax algorihm.
Set the macros ALPHA and BETA to enable the alpha and/or beta pruning of search tree.
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
/* Number of houses per side. */
#define N_HOUSES 6
/* Initial number of seeds per house. */
#define N_SEEDS 3
/* Maximum search depth. */
#define N_MAX_DEPTH 25
/* Total number of houses including the 'score' house. */
#define N_TOT_HOUSES (N_HOUSES + 1)
/* Players: computer or human. */
#define CPU 0
#define PLAYER 1
/* Game end flags. */
#define END_TIE 2
#define END_NONE 3
/* Global variables. */
/* Board game descriptor */
int g_boards[N_MAX_DEPTH+1][2][N_TOT_HOUSES];
int g_current_depth;
int g_max_depth;
/* Store the possible moves. */
int g_moves[N_MAX_DEPTH+1][N_HOUSES];
/* Store the player and evaluation of the previous moves, used for Aplha/Beta pruning. */
int g_previous[N_MAX_DEPTH+1][2];
/* Counter of visited nodes during minimax search. */
int g_visited_nodes;
/*
* Print program command line parameters.
*/
void print_usage()
{
printf("Usage: awari <level> <initial player>\n");
printf(" | |\n");
printf(" 1-%d 0-cpu, 1-player\n", N_MAX_DEPTH);
}
/*
* Parse command line parameters, returns the game difficult level (the search depth)
* and the initial player..
*/
int parse_options(int num, char **arg, int *level, int *ini_player)
{
if(num != 3)
{
print_usage();
return -1;
}
*level = atoi(arg[1]);
*ini_player = atoi(arg[2]);
if( (*level < 1) || (*level >= N_MAX_DEPTH) )
{
printf("Invalid game level, select a number between 1 and %d\n", N_MAX_DEPTH);
return -1;
}
if((*ini_player != CPU) && (*ini_player != PLAYER))
{
printf("Invalid initial player, select 0=CPU or 1=PLAYER\n");
return -1;
}
return 0;
}
/*
* Initialize the data structures for a new game.
*/
void init_board()
{
g_current_depth = 0;
g_visited_nodes = 0;
/* Reset the score house. */
g_boards[g_current_depth][CPU][0] = 0;
g_boards[g_current_depth][PLAYER][0] = 0;
/* Fill the houses with initial seeds. */
for(int i=1 ; i<N_TOT_HOUSES ; i++)
{
g_boards[g_current_depth][CPU][i] = N_SEEDS;
g_boards[g_current_depth][PLAYER][i] = N_SEEDS;
}
}
/*
* Print an horizontal line to separate game moves.
*/
void print_line()
{
for(int i=0 ; i<5*N_HOUSES+18 ; i++)
{
putchar('_');
}
printf("\n\n ");
}
/*
* Print the current board.
*/
void print_board()
{
print_line();
/* CPU (top side). */
for(int i=N_HOUSES ; i>0 ; i--)
{
printf("| %2d ", g_boards[g_current_depth][CPU][i]);
}
printf("|\n");
/* CPU score house. */
printf(" %2d ", g_boards[g_current_depth][CPU][0]);
/* Middle space. */
for(int i=0; i<N_HOUSES ; i++)
{
printf("|====");
}
printf("|");
/* PLAYER score house */
printf(" %2d\n ",g_boards[g_current_depth][PLAYER][0]);
/* PLAYER (bottom side). */
for(int i=0 ; i<N_HOUSES ; i++)
{
printf("| %2d ", g_boards[g_current_depth][PLAYER][i+1]);
}
printf("|\n\n ");
/* House indexes. */
for(int i=0 ; i<N_HOUSES ; i++)
{
printf(" (%1d) ",i+1);
}
printf("\n\n");
}
/*
* Checks if the current board is in a final state or not, returns:
* CPU/PLAYER if there is a winner, END_TIE if the game ended with a tie
* or END_NONE if the game is not ended.
*/
int is_ended()
{
int tot_player = 0;
int tot_cpu = 0;
/* Count the total seeds in the houses. */
for(int i=0; i<N_HOUSES ; i++)
{
tot_cpu += g_boards[g_current_depth][CPU][i+1];
tot_player += g_boards[g_current_depth][PLAYER][i+1];
}
/* The game ends when one side has no seeds and the winner is the player with more seeds in the score house. */
if( !tot_cpu || !tot_player)
{
if( g_boards[g_current_depth][CPU][0] < g_boards[g_current_depth][PLAYER][0] )
{
return PLAYER;
}
if( g_boards[g_current_depth][CPU][0] > g_boards[g_current_depth][PLAYER][0] )
{
return CPU;
}
return END_TIE;
}
return END_NONE;
}
/*
* Checks if the game is over and prints the result. Return 0 if
* the game is not terminated.
*/
int check_winner()
{
/* Check if the game is terminated and print the winner. */
switch( is_ended() )
{
case CPU:
print_line();
printf("* CPU WINS! *\n\n");
return 1;
case PLAYER:
print_line();
printf("* CPU WINS! *\n\n");
return 1;
case END_TIE:
print_line();
printf("* IT'S A TIE! *\n\n");
return 1;
default:
break;
}
return 0;
}
/*
* Get the move of human player.
*/
int prompt_player_move()
{
int move;
const int MAX_LEN = 16;
char str[MAX_LEN];
while(1)
{
printf("PLAYER> ");
fgets(str, MAX_LEN, stdin);
move = atoi(str);
/* Accept the selection if is in the valid range and if the house is not empty. */
if(move>0 && move <=N_HOUSES && g_boards[g_current_depth][PLAYER][move])
{
return move;
}
}
return 0;
}
/*
* Increment the seeds in the houses from 'ini' to 'end' in the 'player' side.
*/
void increment_seed(int ini, int end, int player)
{
for( ; ini<=end ; ini++)
{
g_boards[g_current_depth][player][ini]++;
}
}
/*
* Update the game board according to the 'move' for 'player'.
* Return the next player.
*/
int do_move(int move, int player)
{
int pos, n_seeds;
int other_player = (player == CPU ? PLAYER : CPU);
/* Get the seeds in selected house. */
n_seeds = g_boards[g_current_depth][player][move];
/* Empty the house. */
g_boards[g_current_depth][player][move] = 0;
pos = N_TOT_HOUSES - (move + n_seeds);
if( pos > 0 ) /* Here the last seed lands in the current player's side. */
{
/* Distribute the seeds. */
increment_seed(move+1, N_TOT_HOUSES-pos, player);
/* If the last seed lands in an empty house on the player side, get all the seeds of the other player in the opposite house. */
if( (g_boards[g_current_depth][player][N_TOT_HOUSES-pos] == 1) && (g_boards[g_current_depth][other_player][pos] != 0) )
{
g_boards[g_current_depth][player][0] += g_boards[g_current_depth][other_player][pos] + 1;
g_boards[g_current_depth][player][N_TOT_HOUSES-pos] = 0;
g_boards[g_current_depth][other_player][pos] = 0;
}
return other_player;
}
else if( pos == 0 ) /* Here the last seed lands in the current player's score house. */
{
/* Distribute the seeds. */
increment_seed(move+1, N_HOUSES-pos, player);
g_boards[g_current_depth][player][0]++;
/* Continue with current player. */
return player;
}
else /* Here the last seed passes the current player's score house */
{
int turns, r_seeds, ini_house, ini_player;
ini_player = player;
/* Number of side changes. */
turns = -pos/N_TOT_HOUSES+1;
/* Number of remaining seeds. after side changes. */
r_seeds = -pos%N_TOT_HOUSES;
ini_house = move+1;
/* Distribute seeds on both sides for the number of turns. */
for(int i=0; i<turns ; i++)
{
increment_seed(ini_house, N_HOUSES, player);
player = (player == CPU ? PLAYER:CPU);
ini_house = 1;
}
/* DIstribute the remaining seeds. */
increment_seed(1, r_seeds, player);
/* Increment the score houses. */
g_boards[g_current_depth][ini_player][0] += turns/2+turns%2;
g_boards[g_current_depth][other_player][0] += turns/2;
/* If the last seed lands on the initial player's score house, keep playing. */
if( (turns%2) && !r_seeds )
{
return ini_player;
}
return other_player;
}
}
/*
* Evaluation function: return a score for the current node of the search tree.
* It is the difference between CPU and PLAYER seeds in the score houses.
*/
int evaluate_move()
{
/* The board configuration is evaluated as the differences of seeds in score houses. */
int val = g_boards[g_current_depth][CPU][0] - g_boards[g_current_depth][PLAYER][0];
return val;
}
/*
* Get the possible moves for the 'player' for the current board configuration.
* It stores the moves in the global stack "g_moves" and return the number of elements.
*/
int get_player_moves(int player)
{
int j = 0;
for(int i=1 ; i<N_TOT_HOUSES ; i++)
{
/* A move is possible if the house is not empty. */
if(g_boards[g_current_depth][player][i])
{
g_moves[g_current_depth][j++] = i;
}
}
return j;
}
/*
* Minimax algorithm function.
*/
int minimax_search(int depth, int player, int *best_move)
{
int n_moves, next_player, tmp_val, tmp_move;
int val = (player == PLAYER ? 999 : -999);
g_visited_nodes ++;
/* If we reached the max search depth or there is a winner stop the recursion and return the evaluation. */
if( depth == 0 || is_ended() != END_NONE )
{
int score = evaluate_move();
g_current_depth--;
return score;
}
/* Get the possible moves for the player in the current board configuration, it updates the g_moves array. */
n_moves = get_player_moves(player);
/* Store the player (for alpha/beta pruning). */
g_previous[g_current_depth][0] = player; /* Registra il giocatore per il taglio alfa-beta */
for( int i=n_moves-1; i>=0 ; i-- )
{
/* Store the evaluation (for alpha/beta pruning). */
g_previous[g_current_depth][1] = val;
/* Prepare for recursion. */
g_current_depth++;
/* Duplicate the current board configuration. */
memcpy(g_boards[g_current_depth], g_boards[g_current_depth-1], sizeof(g_boards[0]));
/* Do the move on the new board and get the next player. */
next_player = do_move(g_moves[g_current_depth-1][i], player);
/* Explore the search tree from the new node. */
tmp_val = minimax_search(depth-1, next_player, &tmp_move);
/* CPU player generates OR nodes, select the max value. */
if(player == CPU)
{
if(tmp_val > val)
{
val = tmp_val;
*best_move = g_moves[g_current_depth][i];
}
#ifdef BETA
/* Stop searching under AND nodes (nodes where one of the previous player was PLAYER) and if */
/* val is greater than the previous evaluation. */
{
int cut = 0;
for(int k=1 ; k<=g_current_depth-1 && !cut ; k++)
{
if( g_previous[k][0] == PLAYER && val >= g_previous[k][1] )
{
cut = 1;
break;
}
}
if(cut)
{
break;
}
}
#endif
}
else /* PLAYER generates AND nodes, select the min value. */
{
if(tmp_val < val)
{
val = tmp_val;
}
#ifdef ALPHA
/* Stop searching under OR nodes (nodes where one of the previous player was CPU) and if */
/* val is less than the previous evaluation. */
{
int cut = 0;
for(int k=0 ; k<=g_current_depth-1 && !cut ; k++)
{
if( g_previous[k][0] == CPU && val <= g_previous[k][1] )
{
cut = 1;
break;
}
}
if(cut)
{
break;
}
}
#endif
}
}
/* Return to previous level. */
g_current_depth--;
return val;
}
/*
* Call the minimax recursive search and returns the best move.
* Prints the move and the number of evaluated nodes.
*/
int get_cpu_move()
{
int move;
minimax_search(g_max_depth, CPU, &move);
printf("CPU[%d]> %d\n", g_visited_nodes, N_TOT_HOUSES-move);
g_visited_nodes = 0;
g_current_depth ++;
return move;
}
/*
* main
*/
int main(int argc, char *argv[])
{
int move;
int player;
/* Read command line. */
if( parse_options(argc, argv, &g_max_depth, &player) < 0 )
{
return -1;
}
/* Initialize the game. */
init_board();
/* Game loop. */
while(1)
{
/* Print the current board. */
print_board();
/* Stop the game if there is a winner. */
if( check_winner() )
{
return 0;
}
/* Select the move. */
switch( player )
{
case CPU:
move = get_cpu_move();
break;
case PLAYER:
move = prompt_player_move();
break;
default:
return -1;
}
/* Execute the move and update the current board. */
player = do_move(move, player);
}
}