-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathaialpabeta.c
581 lines (356 loc) · 11.8 KB
/
aialpabeta.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
/*
* aialpabeta.c
* hello
*
* Created by Aron Allen on 11/11/09.
* Copyright 2009 __MyCompanyName__. All rights reserved.
*
*/
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <time.h>
#include "main.h"
#include "mover.h"
#include "jumper.h"
#include "commitmove.h"
#include "commitjump.h"
#include "bitops.h"
#include "aialpabeta.h"
int bestMJab(PGAME orgGame, int secondsToSearch, int plyNodeLimit){
int i;
int j;
int h;
int l;
int nodes = 0;
int endOfGameCounter = 0;
int deepnesSearched = 0;
int startTime = time(0);
int abAlloc = 4;
double score = 0.0;
int moves;
float piecesJumpedInTurn;
PLIGHTGAME tempMoves;
LIGHTGAME lightGame;
GAME heavyGame;
GAME heavyMoveGame;
GAME previousGame;
GAME theGame = (*orgGame);
//Allocate array containing game tree
PALPHABETA alphaBetaStack = calloc( abAlloc, sizeof(ALPHABETA));
//Initieate the game, and set parrent move to -1
lightGame = lightGameFromHeavyGame(theGame);
lightGame.parrentGame = -1;
//Allocate game trees children
for (i=0; i<abAlloc; i++) {
alphaBetaStack[i].moves = malloc(pow(2,i) * sizeof(LIGHTGAME));
alphaBetaStack[i].allocMove = pow(2,i);
}
//Initize game tree
alphaBetaStack[0].moves[0] = lightGame;
alphaBetaStack[0].moveCount = 1;
alphaBetaStack[0].moves[0].score = 0.0;
//Build the stack add scores to the last element of the stack
//Loop stops when time runs out, or the end of the game is found
for (i = 0; time(0) < startTime + secondsToSearch && endOfGameCounter < alphaBetaStack[i].moveCount; i++) {
moves = 0;
h = 0;
//Reallocate space for game tree
if (i+1 >=abAlloc) {
//Increment allocated space
abAlloc++;
alphaBetaStack = realloc(alphaBetaStack, abAlloc * sizeof(ALPHABETA));
//Alocate space for children.
alphaBetaStack[i+1].moves = malloc(plyNodeLimit * 10 * sizeof(LIGHTGAME));
alphaBetaStack[i+1].allocMove = plyNodeLimit * 10;
alphaBetaStack[i+1].moveCount = 0;
}
//Set movecount to zero
alphaBetaStack[i+1].moveCount = 0;
//Set the end of game counter to zero
endOfGameCounter = 0;
for (j = 0; j < alphaBetaStack[i].moveCount && endOfGameCounter < alphaBetaStack[i].moveCount && time(0) < startTime + secondsToSearch; j++){
heavyGame = heavyGameFromLightGame(alphaBetaStack[i].moves[j]);
if (i == 0) {
previousGame = heavyGameFromLightGame(alphaBetaStack[i].moves[0]);
}else {
previousGame = heavyGameFromLightGame(alphaBetaStack[i-1].moves[alphaBetaStack[i].moves[j].parrentGame]);
}
if (goDeeper(&heavyGame, i)) {
//Prepare a heavy game, from the current lightgame
//Find all the jumpers in the game
findJumpersForGame(&heavyGame);
//If no jumps found find moves
if (!heavyGame.canJ) {
findMoversForGame(&heavyGame);
}
//Increment MoveCount
alphaBetaStack[i+1].moveCount += heavyGame.mjCount;
if (alphaBetaStack[i+1].moveCount >= alphaBetaStack[i+1].allocMove) {
//Reallocate the moves stack for the current AB stack.
tempMoves = realloc(alphaBetaStack[i+1].moves, sizeof(LIGHTGAME) * (alphaBetaStack[i+1].moveCount * (i+1)));
if (!tempMoves) {
printf("Temp Moves PANIC\n");
}else {
alphaBetaStack[i+1].moves = tempMoves;
alphaBetaStack[i+1].allocMove = alphaBetaStack[i+1].moveCount * (i+1);
}
}
if (heavyGame.mjCount) {
l = 0;
//Commit all moves and add results to stack
for (h; h < alphaBetaStack[i+1].moveCount; h++) {
heavyMoveGame = heavyGame;
if (heavyMoveGame.canJ) {
piecesJumpedInTurn = bitsInBitboard(heavyGame.mjs[l].removePieces);
makeJump(l, &heavyMoveGame);
}else {
piecesJumpedInTurn = 0;
makeMove(l, &heavyMoveGame);
}
if (l >= heavyMoveGame.mjCount) {
printf("L is to high\n");
}
//score game
heavyMoveGame.score = scoreGame(&heavyMoveGame, &theGame, piecesJumpedInTurn);
nodes++;
heavyMoveGame.parrentGame = j;
alphaBetaStack[i].moves[j].score = 0;
changeTurn(&heavyMoveGame);
cleanUp(&heavyMoveGame);
alphaBetaStack[i+1].moves[h] = lightGameFromHeavyGame(heavyMoveGame);
l++;
}
}else{
score = 0.0;
if (theGame.turn == 'w') {
if (heavyGame.black == 0) {
score = 1000.0;
}else if(heavyGame.white == 0){
score = -1000.0;
}
}else{
if (heavyGame.white == 0){
score = 1000.0;
}else if (heavyGame.black == 0){
score = -1000.0;
}
}
if (heavyGame.mjCount == 0 && !score) {
if (theGame.turn == heavyGame.turn) {
score = -1000.0;
}else {
score = 1000.0;
}
}
alphaBetaStack[i].moves[j].score = score;
endOfGameCounter++;
}
}
}
//Sort current ABstack (decending for current turn, acssending for opponent turn)
if (alphaBetaStack[i].moves[0].turn != (*orgGame).turn) {
qsort(alphaBetaStack[i+1].moves, alphaBetaStack[i+1].moveCount, sizeof(LIGHTGAME), stackCompare);
}else{
qsort(alphaBetaStack[i+1].moves, alphaBetaStack[i+1].moveCount, sizeof(LIGHTGAME), stackCompareInverted);
}
//If we are over the plyNodeLimit, cutoff the rest of the tree.
if (alphaBetaStack[i+1].moveCount > plyNodeLimit) {
tempMoves = calloc(plyNodeLimit, sizeof(LIGHTGAME));
//Relese every result after onehundred
for (l=0; l<plyNodeLimit; l++) {
tempMoves[l] = alphaBetaStack[i+1].moves[l];
}
free (alphaBetaStack[i+1].moves);
alphaBetaStack[i+1].moves = tempMoves;
//Set the movecount and alloc count.
alphaBetaStack[i+1].moveCount = plyNodeLimit;
alphaBetaStack[i+1].allocMove = plyNodeLimit;
/* DEBUGGING CHECKING IF QSORT WORKS RIGHT
if(alphaBetaStack[i].moves[0].turn != (*orgGame).turn)
{
printf("My opponents best move has the score of %f\n", alphaBetaStack[i+1].moves[0].score );
printf("My opponents worst move has the score of %f\n\n", alphaBetaStack[i+1].moves[plyNodeLimit].score );
}
else
{
printf("My own best move has the score of %f\n", alphaBetaStack[i+1].moves[0].score );
printf("My own worst move has the score of %f\n\n", alphaBetaStack[i+1].moves[plyNodeLimit].score );
}
*/
}
}
deepnesSearched = i;
//Iterate through tree, buttom up
for (i; i > 0; i--) {
h = 0;
while (h < alphaBetaStack[i].moveCount) {
if (!alphaBetaStack[i-1].moves[alphaBetaStack[i].moves[h].parrentGame].score) {
alphaBetaStack[i-1].moves[alphaBetaStack[i].moves[h].parrentGame].score = alphaBetaStack[i].moves[h].score;
}
//If the score of the child move is, greater than the current score, then replace it.
if (alphaBetaStack[i].moves[0].turn == (*orgGame).turn) {
if (alphaBetaStack[i-1].moves[alphaBetaStack[i].moves[h].parrentGame].score > alphaBetaStack[i].moves[h].score)
alphaBetaStack[i-1].moves[alphaBetaStack[i].moves[h].parrentGame].score = alphaBetaStack[i].moves[h].score;
}else {
if (alphaBetaStack[i-1].moves[alphaBetaStack[i].moves[h].parrentGame].score < alphaBetaStack[i].moves[h].score)
alphaBetaStack[i-1].moves[alphaBetaStack[i].moves[h].parrentGame].score = alphaBetaStack[i].moves[h].score;
}
h++;
}
}
for (i=0; i < (*orgGame).mjCount; i++) {
theGame.mjs[i].score = alphaBetaStack[1].moves[i].score;
}
qsort(theGame.mjs, theGame.mjCount, sizeof(MJ), scoreCompare);
for (i=0; i < (*orgGame).mjCount; i++) {
(*orgGame).mjs[i].score = theGame.mjs[i].score;
}
//FREE everything from the memory
for (i=0; i<abAlloc; i++) {
free(alphaBetaStack[i].moves);
}
free(alphaBetaStack);
printf("Searched %d ply, containing %d nodes\n", deepnesSearched, nodes);
return 0;
}
int stackCompare(const void *a, const void *b){
LIGHTGAME *mA = (LIGHTGAME *) a;
LIGHTGAME *mB = (LIGHTGAME *) b;
if (mA->score > mB->score) {
return 1;
}else if(mA->score == mB->score)
{
return 0;
}else if (mA->score < mB->score) {
return -1;
}
}
int stackCompareInverted(const void *a, const void *b){
LIGHTGAME *mA = (LIGHTGAME *) a;
LIGHTGAME *mB = (LIGHTGAME *) b;
if (mA->score > mB->score) {
return -1;
}else if(mA->score == mB->score)
{
return 0;
}else if (mA->score < mB->score) {
return 1;
}
}
int scoreCompareInverted(const void *a, const void *b){
MJ *mja = (MJ *) a;
MJ *mjb = (MJ *) b;
if (mjb->score > mja->score) {
return -1;
}else if(mjb->score == mja->score)
{
return 0;
}else if (mjb->score < mja->score) {
return 1;
}
}
int scoreCompare(const void *a, const void *b){
MJ *mja = (MJ *) a;
MJ *mjb = (MJ *) b;
if (mjb->score > mja->score) {
return 1;
}else if(mjb->score == mja->score)
{
return 0;
}else if (mjb->score < mja->score) {
return -1;
}
}
int goDeeper(PGAME hg, int ply){
return 1;
if (ply < 2) {
return 1;
}
ply = ply/1.5;
if ((*hg).score > -ply && (*hg).score < ply) {
return 1;
}
return 0;
}
double scoreGame(PGAME hg, PGAME org, float piecesJumpedInTurn){
int blackMultiplier;
int whiteMultiplier;
BITBOARD sideSafe = !(LEGAL_EAST_MOVE&LEGAL_WEST_MOVE);
BITBOARD homeBase;
if ((*hg).turn == 'w') {
blackMultiplier = -1;
whiteMultiplier = 1;
homeBase = !LEGAL_SOUTH_MOVE;
}else{
blackMultiplier = 1;
whiteMultiplier = -1;
homeBase = !LEGAL_NORTH_MOVE;
}
float score = 0.0;
score += bitsInBitboard((*hg).black)*blackMultiplier*10;
score += bitsInBitboard((*hg).white)*whiteMultiplier*10;
score += bitsInBitboard((*hg).kings&(*hg).black)*blackMultiplier*40;
score += bitsInBitboard((*hg).kings&(*hg).white)*whiteMultiplier*40;
score += bitsInBitboard(((*hg).black&!(*hg).kings)&homeBase)*blackMultiplier*3;
score += bitsInBitboard(((*hg).white&!(*hg).kings)&homeBase)*whiteMultiplier*3;
score += bitsInBitboard((*hg).white&sideSafe)*whiteMultiplier*1;
score += bitsInBitboard((*hg).white&sideSafe)*blackMultiplier*1;
return score;
if (piecesJumpedInTurn > 1) {
piecesJumpedInTurn = 1 + (0.2*piecesJumpedInTurn);
}else{
piecesJumpedInTurn = 1;
}
BITBOARD orgFriends;
BITBOARD orgEnemies;
BITBOARD orgKings = (*org).kings;
BITBOARD friends;
BITBOARD enemies;
BITBOARD kings = (*hg).kings;
if ((*org).turn == 'w') {
orgFriends = (*org).white;
friends = (*hg).white;
orgEnemies = (*org).black;
enemies = (*hg).black;
}else{
orgFriends = (*org).black;
friends = (*hg).black;
orgEnemies = (*org).white;
enemies = (*hg).white;
}
if ((*org).turn == 'w') {
homeBase = !LEGAL_SOUTH_MOVE;
}else {
homeBase = !LEGAL_NORTH_MOVE;
}
int enemiesLost = 0;
int friendsLost = 0;
int enemyKingsLost = 0;
int friendlyKingsLost = 0;
int balance = 0;
int positionScore = 0;
int enemyCount = 0;
int friendCount = 0;
int enemyKingCount = 0;
int friendlyKingCount = 0;
//Find out how many frieldly pieces were lost
friendsLost = bitsInBitboard(orgFriends) - bitsInBitboard(friends); //
friendlyKingsLost = bitsInBitboard(orgFriends&orgKings) - bitsInBitboard(friends&kings); //
//Find out how many enemy pieces were taken
enemiesLost = bitsInBitboard(orgEnemies) - bitsInBitboard(enemies); //
enemyKingsLost = bitsInBitboard(orgEnemies&orgKings) - bitsInBitboard(enemies&kings); //
enemyCount = bitsInBitboard(enemies);
friendCount = bitsInBitboard(friends);
enemyKingCount = bitsInBitboard(enemies&kings);
friendlyKingCount = bitsInBitboard(friends&kings);
positionScore = bitsInBitboard((friends&!kings)&homeBase)*5;
positionScore += bitsInBitboard(friends&sideSafe)*2;
balance = friendsLost * 10;
balance += enemiesLost * 10;
balance += friendlyKingsLost * 30;
balance += enemyKingsLost * 30;
balance += positionScore;
score = balance * piecesJumpedInTurn;
return score;
}