-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.c
681 lines (606 loc) · 17.1 KB
/
main.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
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
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
/**
* Kalah Agent, by John V. Flickinger
* 4/12/2016
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <limits.h>
#include <stdbool.h>
#include <time.h>
typedef struct {
int v;
int move;
} Node;
void input_loop();
int calculate_agent_turn();
int eval(int state[]);
int get_free_turns_for_playerX(int state [], int pNum);
int count_marbles(int state[], int pNum);
Node alpha_beta_search(int state[], int depth, int alpha, int beta, bool maximizingPlayer);
void get_all_avail_actions(int state[], int ret[], int pNum);
int move(int state[], int bowl, int pNum);
void special_condition_in_empty_bowl(int state[], int lastIndex);
int no_more_moves(int state[], int pNum);
int terminal_test(int children[]);
int max(int val1, int val2);
int min(int val1, int val2);
void init_board();
void print_out_board(int state[]);
int getLine(char* prmpt, char* buff, size_t sz);
/* Variables */
int board[14]; // game board
int playerNum; // either 1 or 2
int debug = 0;
int SEARCH_DEPTH = 10; // start depth
int s_depth = 0;
int PLAYER1_KALAH = 6;
int PLAYER2_KALAH = 13;
bool search_broken;
clock_t start, diff;
int main(int argc, char** argv)
{
if(argc > 1) {
int i;
char* arg1 = argv[1];
if(strcmp(arg1, "1") == 0) { // player 1
playerNum = 1;
} else if(strcmp(arg1, "2") == 0) { // player 2
playerNum = 2;
} else {
printf("Not a valid command\n");
exit(0);
}
if(argc > 2) {
for(i = 0; i < argc; i++) {
char * arg = argv[i];
if(strcmp(arg, "-d") == 0) { // debug option
debug = 1;
printf("In debugging mode\n");
} else if(strcmp(arg, "-h") == 0) {
i++;
SEARCH_DEPTH = atoi(argv[i]);
if(debug) {
printf("\nDepth is %d\n", SEARCH_DEPTH);
}
}
}
}
} else {
printf("Please specify agent number\n");
exit(0);
}
init_board();
if(debug) print_out_board(board);
input_loop(); // start taking in commands
return 0;
}
/**
* @brief The main loop to recieve game commands
*/
void input_loop()
{
int readResult;
char buff[15]; // buffer to read user data
while(1) { // TODO while not end of the game
// get one line of input from the user
readResult = getLine("", buff, sizeof(buff));
if(readResult == 1) {
printf("\nNo input\n");
exit(0);
} else if(readResult == 2) {
printf("Input too long [%s]\n", buff);
exit(0);
} // end user-input
// handle quit command
if(strcmp(buff, "quit") == 0) {
exit(0);
}
// handle move command
else if(strcmp(buff, "move") == 0) {
int suggestedMove = calculate_agent_turn();
move(board, suggestedMove, (playerNum == 1) ? 1 : 2);
if(debug) {
printf("Move made: %d", suggestedMove);
print_out_board(board);
} else {
printf("%d", suggestedMove);
}
fflush(stdout);
}
// handle opponent command
else if(strstr(buff, "opponent") != '\0') {
char* b;
b = strtok(buff, " ");
b = strtok(NULL, " "); // get the second argument
int bowl = atoi(b);
move(board, bowl, (playerNum == 1) ? 2 : 1);
if(debug)
print_out_board(board);
}
// printf("Eval of state: %d", eval(board));
int i;
for(i = 0; i < 15; i++) {
buff[i] = 0;
}
}
}
int calculate_agent_turn()
{
int fake_state[14];
// make a copy of the board
int i;
for(i = 0; i < 14; i++) {
fake_state[i] = board[i];
}
int best_move;
start = clock();
search_broken = false;
s_depth = 0;
do {
if(debug) printf("At depth: %d:\n ----------------------------\n", SEARCH_DEPTH + s_depth);
int move = alpha_beta_search(fake_state, SEARCH_DEPTH + s_depth, INT_MIN, INT_MAX, true).move;
diff = clock()-start;
if(!search_broken) {
best_move = move;
}
if(debug) printf("\ntime taken: %d secs\n---------------------\n\n", (diff/CLOCKS_PER_SEC));
s_depth++;
}while(!search_broken);
return best_move;
}
int eval(int state[])
{
int score = 0;
int p1Stones = count_marbles(state, 1);
int p2Stones = count_marbles(state, 2);
score = ((state[6] - state[13]) + (p1Stones - p2Stones));
//check if it's for sure win.
if(state[6] > 36) {
score+=1000;
} else if(state[13] > 36) {
score-=1000;
}
int i, countEmpty=0, mostOpponent = 0, aiMin = INT_MAX, aiMost = INT_MIN;
for(i = 0; i < 6; i++) {
int oppositeIndex = (12 - 2 * i) + i;
if(state[i] < aiMin) {
aiMin = state[i];
}
if(state[i] > aiMost) {
aiMost = state[i];
}
if(state[i] == 0) {
if(state[oppositeIndex] > 0) {
//do small cost checks
int canSteal = 0;
int j;
for(j = 0; j < 6; j++) {
if((state[j] - (i-j)) % 13 == 0) {
canSteal = 1;
}
}
if(canSteal) {
score += state[oppositeIndex];
}
}
countEmpty++;
}
if(state[oppositeIndex] > mostOpponent)
mostOpponent = state[oppositeIndex];
if(state[oppositeIndex] == 0) {
int j, canOpponentSteal = 0;
if(state[oppositeIndex] > 5) {
for(j = 7; j < 13; j++) {
if((state[j] - (i-j)) % 13 == 0) {
canOpponentSteal = 1;
}
}
if(canOpponentSteal) {
score -= state[i];
}
}
}
}
if((aiMost - aiMin) > 10) {
score--;
}
// if(countEmpty== 3 && (p1Stones + p2Stones) < 20) {
// score--;
// } else if(countEmpty == 4 && (p1Stones + p2Stones) < 20) {
// score-=3;
// }
// else if(countEmpty == 3 && (p1Stones + p2Stones) < 10) {
// score-=2;
// } else if(countEmpty == 4 && (p1Stones + p2Stones) < 10) {
// score-=5;
// }
//
// if(mostOpponent > 10 && (p1Stones + p2Stones) < 30) {
// score--;
// }
// else if(mostOpponent > 14 && (p1Stones + p2Stones) < 40) {
// score-=4;
// } else if(mostOpponent > 17) {
// score-=10;
// }
//
// if((p1Stones + p2Stones) > 60 && (state[5] < 4)) {
// score += 1;
// }
float ratio = (float)p1Stones/p2Stones;
if(ratio > .72) {
score-=mostOpponent*2;
}
float inverseRatio = (float)p2Stones/p1Stones;
if(inverseRatio > .85) {
score-=mostOpponent/2;
}
score-=get_free_turns_for_playerX(state, 2);
score+=get_free_turns_for_playerX(state, 1);
if(playerNum == 2) score *= -1;
return score;
}
int get_free_turns_for_playerX(int state [], int pNum) {
int count = 0;
if(pNum == 1) {
int i;
for(i = 0; i < 6; i++) {
if(state[i] == (6-i)) {
count++;
}
}
} else if(pNum == 2) {
int i;
for(i = 7; i < 13; i++) {
if(state[i] == (13-i)) {
count++;
}
}
}
return count;
}
int count_marbles(int state[], int pNum)
{
int sum = 0;
if(pNum == 1) {
int i;
for(i = 0; i < 6; i++) {
sum += state[i];
}
} else {
int i;
for(i = 7; i < 13; i++) {
sum += state[i];
}
}
}
Node alpha_beta_search(int state[], int depth, int alpha, int beta, bool maximizingPlayer)
{
Node node;
if(depth == 0) {
node.v = eval(state);
return node;
}
int children[14];
// make copy of state
int n_state[14];
int i;
int k;
if(maximizingPlayer) {
get_all_avail_actions(state, children, playerNum);
if(terminal_test(children)) {
node.v = eval(state);
return node;
}
// print_out_board(children);
node.v = INT_MIN;
for(k = 13; k >= 0; k--) { // for every action in action
if(children[k] == 1) {
diff = clock()-start;
if(((diff) / CLOCKS_PER_SEC) > 14 || search_broken) {
if(debug) printf("Broke off search.\n");
search_broken = true;
break;
}
// make a copy of the game state
for(i = 0; i < 14; i++) {
n_state[i] = state[i];
}
int goAgain = move(n_state, k, playerNum);
int val = alpha_beta_search(n_state, depth - 1, alpha, beta, (goAgain == 1) ? true : false).v;
if(debug) {
if(depth == (SEARCH_DEPTH + s_depth)) {
printf("move %d with eval: %d\n", k, val);
}
}
if(node.v < val) {
node.v = val;
node.move = k;
}
alpha = max(alpha, node.v);
if(beta <= alpha) {
break;
}
}
}
return node;
} else {
if(playerNum == 1) {
get_all_avail_actions(state, children, 2);
} else {
get_all_avail_actions(state, children, 1);
}
if(terminal_test(children)) {
node.v = eval(state);
return node;
}
node.v = INT_MAX;
for(k = 13; k >=0; k--) { // for every action in action
if(children[k] == 1) {
diff = clock()-start;
if(((diff) / CLOCKS_PER_SEC) > 14 || search_broken) {
if(debug) printf("Broke off search.\n");
search_broken = true;
break;
}
// make a copy of the game state
for(i = 0; i < 14; i++) {
n_state[i] = state[i];
}
int goAgain = move(n_state, k, (playerNum == 1) ? 2 : 1);
int val = alpha_beta_search(n_state, depth - 1, alpha, beta, (goAgain == 1) ? false : true).v;
if(node.v >= val) {
node.v = val;
node.move = k;
}
beta = min(beta, node.v);
if(beta <= alpha) {
break;
}
diff = clock()-start;
}
}
return node;
}
}
/**
* @brief
* @param state, the board state to look at.
* @param ret, an array of all the actions containing either a 1 or a 0.
* 1 for available action, 0 for unavailable.
*/
void get_all_avail_actions(int state[], int ret[], int pNum)
{
if(pNum == 1) {
int i;
for(i = 0; i < 6; i++) {
if(state[i] > 0) {
ret[i] = 1;
} else {
ret[i] = 0;
}
}
for(i = 7; i < 13; i++) {
ret[i] = 0;
}
} else {
int i;
for(i = 7; i < 13; i++) {
if(state[i] > 0) {
ret[i] = 1;
} else {
ret[i] = 0;
}
}
for(i = 0; i < 6; i++) {
ret[i] = 0;
}
}
ret[6] = 0;
ret[13] = 0;
}
/**
* @brief Move a stone.
* @param state, the state of the board
* @param bowl, which bowl number to move
* @param pNum, who's the player moving?
* @return 1 = Go again, 0 = next player's turn
*/
int move(int state[], int bowl, int pNum)
{
// pick up stones from bowl
int numStones = state[bowl];
state[bowl] = 0;
int index = bowl + 1;
do {
// Don't add stones to opponent's Kalah
if(pNum == 1) {
if(index == 13) {
index = 0;
}
}
if(pNum == 2) {
if(index == 6) {
index++;
}
}
state[index]++;
numStones--;
index++;
if(index > 13) {
index = 0; // go in circle
}
} while(numStones > 0);
// Reset index to last bowl where a stone was dropped
if(index == 0) {
index = 13;
} else {
index--;
}
// The bowl was previously empty
if(state[index] == 1) {
if(pNum == 1 && index < 6) {
special_condition_in_empty_bowl(state, index); // empty bowl last stone in player's own side
}
if(pNum == 2 && index > 6 && index != 13) {
special_condition_in_empty_bowl(state, index);
}
}
//check to see if that player is out of stones
if(no_more_moves(state, pNum)) {
if(pNum == 1) {
int i;
for(i = 7; i < 13; i++) {
int nStones = state[i];
state[i] = 0;
state[PLAYER2_KALAH] += nStones;
}
} else {
int i;
for(i = 0; i < 6; i++) {
int nStones = state[i];
state[i] = 0;
state[PLAYER1_KALAH] += nStones;
}
}
return 0;
}
// Special condition last stone in own's kalah
if(pNum == 1 && index == 6) {
return 1;
}
if(pNum == 2 && index == 13) {
return 1;
}
return 0; // next player's turn
}
int no_more_moves(int state[], int pNum) {
if(pNum == 1) {
int i;
for(i = 0; i < 6; i++) {
if(state[i] > 0) {
return false;
}
}
} else {
int i;
for(i = 7; i < 13; i++) {
if(state[i] > 0) {
return false;
}
}
}
return true;
}
int terminal_test(int children[])
{
int i;
for(i = 0; i < 6; i++) {
if(children[i] == 1) {
return false;
}
}
for(i = 7; i < 13; i++) {
if(children[i] == 1) {
return false;
}
}
return true;
}
/**
* @brief Handle the special condition when player drops last stone into one of his/her bowls.
* @param state, the board to modify
* @param lastIndex, the place where the last stone went into
*/
void special_condition_in_empty_bowl(int state[], int lastIndex)
{
if(lastIndex < 6) { // move to P1's Kalah
int oppositeIndex = (12 - 2 * lastIndex) + lastIndex;
int totStones = state[oppositeIndex] + 1;
state[6] += totStones; // deposit to Kalah
state[oppositeIndex] = 0;
state[lastIndex] = 0;
} else if(lastIndex > 6) { // move to P2's kalah
int oppositeIndex = 12 - lastIndex;
int totStones = state[oppositeIndex] + 1;
state[13] += totStones; // deposit to Kalah
state[oppositeIndex] = 0;
state[lastIndex] = 0;
}
}
int max(int val1, int val2)
{
if(val1 > val2) {
return val1;
} else {
return val2;
}
}
int min(int val1, int val2)
{
if(val1 < val2) {
return val1;
} else {
return val2;
}
}
/**
* @brief Used to initialize the game state
*/
void init_board()
{
int i;
for(i = 0; i < 14; i++) {
if(i == 13 || i == 6) {
continue;
}
board[i] = 6; // beans per bowl
}
}
/**
* @brief Print out the representation of a game board.
* @param state, the state of the game board.
*/
void print_out_board(int state[])
{
// Gap size
char* gap = "\t";
// top row
printf("\n%s%d%s%d%s%d", gap, state[12], gap, state[11], gap, state[10]);
printf("%s%d%s%d%s%d\n", gap, state[9], gap, state[8], gap, state[7]);
// middle
printf("%d%s%s%s%s%s%s%s%d", state[13], gap, gap, gap, gap, gap, gap, gap, state[6]);
// bottom row
printf("\n%s%d%s%d%s%d", gap, state[0], gap, state[1], gap, state[2]);
printf("%s%d%s%d%s%d\n", gap, state[3], gap, state[4], gap, state[5]);
}
/**
* @brief Get a line of input from the user
* @param prmpt, what to ask the user
* @param buff, the buffer for temporary reading
* @param sz, the size of the buffer
* @return OK = 0; NO input = 1, too long = 2
*/
int getLine(char* prmpt, char* buff, size_t sz)
{
int ch, extra;
// Get line with buffer overrun protection.
if(prmpt != NULL) {
printf("%s", prmpt);
fflush(stdout);
}
if(fgets(buff, sz, stdin) == NULL)
return 1;
// If it was too long, there'll be no newline. In that case, we flush
// to end of line so that excess doesn't affect the next call.
if(buff[strlen(buff) - 1] != '\n') {
extra = 0;
while(((ch = getchar()) != '\n') && (ch != EOF))
extra = 1;
return (extra == 1) ? 2 : 0;
}
// Otherwise remove newline and give string back to caller.
buff[strlen(buff) - 1] = '\0';
return 0;
}