-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
921 lines (824 loc) · 30 KB
/
Copy pathmain.cpp
File metadata and controls
921 lines (824 loc) · 30 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
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
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
//
// main.cpp
// AI Checkers Project
//
// Created by Andrew Lorber on 10/7/19.
// Copyright © 2019 Andrew Lorber. All rights reserved.
//
#include <iostream>
#include <array>
#include <string>
#include <vector>
#include <fstream>
#include <math.h>
#include <algorithm>
#include <time.h>
using namespace std;
// Board Key:
// 0 = empty space, 1 = player 1, 2 = player 2, 3 = player 1 king, 4 = player 2 king
int currentBoard[8][8] = {0};
// Global variables for heuristic
int currentTotalPcs = 0;
int currentP1Pcs = 0;
int currentP2Pcs = 0;
int currentP1Kings = 0;
int currentP2Kings = 0;
// Keeps track of turns without jumps. After 80 (40 each player), a draw is called.
int turnCount = 0;
// Initializes new game board
void initStartBoard(){
int player = 2;
for(int i = 0, j = 0; j < 8; i++){
if ((i+j+1)%2 == 0){
currentBoard[j][i] = player;
}
if (i == 7){
i = -1;
if(j == 2){
j = 5;
player = 1;
} else {
j++;
}
}
}
currentTotalPcs = 24;
currentP1Pcs = 12;
currentP2Pcs = 12;
}
// Initializes user-set game board
void initUserBoard(){
// Reads in file with game board. See board key above.
string file;
cout << "Please enter the name of your board file.\nEach space should be either:\n 0 - empty space\n";
cout << " 1 - player 1 piece\n 2 - player 2 piece\n 3 - player 1 king\n 4 - player 2 king\n";
cout << "The squares of the board that are not used in checkers should be replaced with spaces.\n";
cout << "Note: Player 1 starts on the bottom of the board and player 2 starts on the top of the board.\n";
cin >> file;
ifstream fin;
fin.open(file);
// Checks for incorrect input
while(!fin.is_open()){
cout << "The file you entered cannot be opened.\nPlease enter a new file.\n";
cin >> file;
fin.open(file);
}
int square;
int i = 1; int j = 0;
while(fin >> square){
if ((i+j+1)%2 == 0){
// If you input a board with a player in the opposite player's home row, it will become a king
if(j == 0 && square == 1){
square = 3;
} else if(j == 7 && square == 2){
square = 4;
}
currentBoard[j][i] = square;
// Keeps track of # pcs
if(square == 1 || square == 3){
currentP1Pcs++;
currentTotalPcs++;
if(square == 3){
currentP1Kings++;
}
} else if(square == 2 || square == 4){
currentP2Pcs++;
currentTotalPcs++;
if(square == 4){
currentP2Kings++;
}
}
}
do{
i++;
if(i == 8){
i = 0;
j++;
}
if(j == 8){
break;
}
} while ((i+j+1)%2 != 0);
}
}
// Coordinate class to simplify things
class Coordinate {
public:
int x, y;
Coordinate(int i, int j) : x{i}, y{j} {}
};
// Vector of Legal Moves
vector<vector<Coordinate>> movesList;
// Vector of moves that include a jump
vector<vector<Coordinate>*> jumpsList;
// Prevents jumping same spot twice
bool legalJump(int jumpedI, int jumpedJ, const vector<Coordinate> & move){
for (int i = 0; i < move.size()-1; i++) {
if(((abs(move.at(i).x + move.at(i+1).x)) / 2) == jumpedI
&& ((abs(move.at(i).y + move.at(i+1).y)) / 2) == jumpedJ){
return false;
}
}
return true;
}
// Handles jumping
void jump(vector<Coordinate>& move, int i, int j, int player, int otherPlayer, bool king, int board[8][8],
vector<vector<Coordinate>>& moves = movesList, vector<vector<Coordinate>*>& jumps = jumpsList){
// Keeps track of multiple possible double jumps (i.e need to add another move to the list)
bool multiOptions = false;
vector<Coordinate> moveCopy = move;
// If you end a jump in a space to become a king, your jump ends
if (((j == 0 && player == 1) || (j == 7 && player == 2)) && !king) {
return;
}
// NW Corner
if(i > 1 && j > 1 && (player == 1 || king)){
if((board[j-1][i-1] == otherPlayer || board[j-1][i-1] == otherPlayer+2)
&& (board[j-2][i-2] == 0 || (i-2 == move.at(0).x && j-2 == move.at(0).y))
&& legalJump(i-1, j-1, moveCopy)){
move.push_back(Coordinate(i-2, j-2));
multiOptions = true;
jump(move, i-2, j-2, player, otherPlayer, king, board, moves, jumps);
}
}
// NE Corner
if(i < 6 && j > 1 && (player == 1 || king)){
if((board[j-1][i+1] == otherPlayer || board[j-1][i+1] == otherPlayer+2)
&& (board[j-2][i+2] == 0 || (i+2 == move.at(0).x && j-2 == move.at(0).y))
&& legalJump(i+1, j-1, moveCopy)){
if (multiOptions) {
moves.push_back(moveCopy);
jumps.push_back(&moves.at(moves.size()-1));
(moves.at(moves.size()-1)).push_back(Coordinate(i+2, j-2));
jump(moves.at(moves.size()-1), i+2, j-2, player, otherPlayer, king, board, moves, jumps);
} else {
move.push_back(Coordinate(i+2, j-2));
multiOptions = true;
jump(move, i+2, j-2, player, otherPlayer, king, board, moves, jumps);
}
}
}
// SW Corner
if(i > 1 && j < 6 && (player == 2 || king)){
if((board[j+1][i-1] == otherPlayer || board[j+1][i-1] == otherPlayer+2)
&& (board[j+2][i-2] == 0 || (i-2 == move.at(0).x && j+2 == move.at(0).y))
&& legalJump(i-1, j+1, moveCopy)){
if (multiOptions) {
moves.push_back(moveCopy);
jumps.push_back(&moves.at(moves.size()-1));
(moves.at(moves.size()-1)).push_back(Coordinate(i-2, j+2));
jump(moves.at(moves.size()-1), i-2, j+2, player, otherPlayer, king, board, moves, jumps);
} else {
move.push_back(Coordinate(i-2, j+2));
multiOptions = true;
jump(move, i-2, j+2, player, otherPlayer, king, board, moves, jumps);
}
}
}
// SE Corner
if(i < 6 && j < 6 && (player == 2 || king)){
if((board[j+1][i+1] == otherPlayer || board[j+1][i+1] == otherPlayer+2)
&& (board[j+2][i+2] == 0 || (i+2 == move.at(0).x && j+2 == move.at(0).y))
&& legalJump(i+1, j+1, moveCopy)){
if (multiOptions) {
moves.push_back(moveCopy);
jumps.push_back(&moves.at(moves.size()-1));
(moves.at(moves.size()-1)).push_back(Coordinate(i+2, j+2));
jump(moves.at(moves.size()-1), i+2, j+2, player, otherPlayer, king, board, moves, jumps);
} else {
move.push_back(Coordinate(i+2, j+2));
multiOptions = true;
jump(move, i+2, j+2, player, otherPlayer, king, board, moves, jumps);
}
}
}
}
//Finds all legal moves for player
void getLegalMoves(int player, int board[8][8] = currentBoard, vector<vector<Coordinate>>& moves = movesList,
vector<vector<Coordinate>*>& jumps = jumpsList){
moves.clear();
jumps.clear();
int otherPlayer;
bool king;
bool jumped = false; // If there is a jump that could be made,
// then no need to check for non-jump moves anymore
if(player == 1){
otherPlayer = 2;
} else {
otherPlayer = 1;
}
for(int i = 0, j = 0; j < 8; i++){
if(board[j][i] == player || board[j][i] == player +2){
king = board[j][i] > 2;
// Checks North corners
if((player == 1 || king) && j != 0){ // If on top row, then no North corners
// check NW
if(i != 0){
if(!jumped && board[j-1][i-1] == 0){
// Adds move
moves.push_back({Coordinate(i, j), Coordinate(i-1, j-1)});
} else if((board[j-1][i-1] == otherPlayer || board[j-1][i-1] == otherPlayer + 2)
&& i != 1 && j != 1){
if(board[j-2][i-2] == 0){
// Handle jump
jumped = true;
moves.push_back({Coordinate(i, j), Coordinate(i-2, j-2)});
jumps.push_back(&moves.at(moves.size()-1));
jump(moves.at(moves.size()-1), i-2, j-2, player, otherPlayer, king, board, moves, jumps);
}
}
}
// check NE
if(i != 7){
if(!jumped && board[j-1][i+1] == 0){
// Adds move
moves.push_back({Coordinate(i, j), Coordinate(i+1, j-1)});
} else if((board[j-1][i+1] == otherPlayer ||board[j-1][i+1] == otherPlayer + 2)
&& i != 6 && j != 1){
if(board[j-2][i+2] == 0){
// Handle jump
jumped = true;
moves.push_back({Coordinate(i, j), Coordinate(i+2, j-2)});
jumps.push_back(&(moves.at(moves.size()-1)));
jump(moves.at(moves.size()-1), i+2, j-2, player, otherPlayer, king, board, moves, jumps);
}
}
}
}
// Checks South Corners
if((player == 2 || king) && j != 7){
// check SW
if(i != 0){
if(!jumped && board[j+1][i-1] == 0){
// Adds move
moves.push_back({Coordinate(i, j), Coordinate(i-1, j+1)});
} else if((board[j+1][i-1] == otherPlayer ||board[j+1][i-1] == otherPlayer + 2)
&& i != 1 && j != 6){
if(board[j+2][i-2] == 0){
// Handle jump
jumped = true;
moves.push_back({Coordinate(i, j), Coordinate(i-2, j+2)});
jumps.push_back(&moves.at(moves.size()-1));
jump(moves.at(moves.size()-1), i-2, j+2, player, otherPlayer, king, board, moves, jumps);
}
}
}
// check SE
if(i != 7){
if(!jumped && board[j+1][i+1] == 0){
// Adds move
moves.push_back({Coordinate(i, j), Coordinate(i+1, j+1)});
} else if((board[j+1][i+1] == otherPlayer ||board[j+1][i+1] == otherPlayer + 2)
&& i != 6 && j != 6){
if(board[j+2][i+2] == 0){
// Handle jump
jumped = true;
moves.push_back({Coordinate(i, j), Coordinate(i+2, j+2)});
jumps.push_back(&moves.at(moves.size()-1));
jump(moves.at(moves.size()-1), i+2, j+2, player, otherPlayer, king, board, moves, jumps);
}
}
}
}
}
if(i == 7){
i = -1;
j++;
}
}
}
// Displays all legal moves (If jumps are possible, will only display jumps)
void printMoves(){
if(jumpsList.size() > 0){
for(int i = 0; i < jumpsList.size(); i++){
cout << i+1 << ".";
for(int j = 0; j < jumpsList.at(i)->size(); j++){
cout << " (" << jumpsList.at(i)->at(j).x << ", " << jumpsList.at(i)->at(j).y << ")";
}
cout << '\n';
}
} else {
for(int i = 0; i < movesList.size(); i++){
cout << i+1 << ".";
for(int j = 0; j < movesList.at(i).size(); j++){
cout << " (" << movesList.at(i).at(j).x << ", " << movesList.at(i).at(j).y << ")";
}
cout << '\n';
}
}
}
void printBoard(){
cout << " \nKEY:\n P1 Pawn: " << "\033[1;31m" << "\u25EF" << "\033[0m\n";
cout << " P1 King: " << "\033[1;31m" << "\u25C6" << "\033[0m\n";
cout << " P2 Pawn: " << "\033[1;34m" << "\u25EF" << "\033[0m\n";
cout << " P2 King: " << "\033[1;34m" << "\u25C6" << "\033[0m\n";
cout << " X - axis" << '\n';
cout << " |-0-|-1-|-2-|-3-|-4-|-5-|-6-|-7-|" << '\n';
cout << " Y";
cout << '\n' << "|---| |---|---|---|---|---|---|---|---|" << '\n';
for(int i = 0, j = 0; j < 8; i++){
if(i == 0){
cout << "|-" << j << "-| ";
}
if(((i+j+1)%2 == 0)){
if(currentBoard[j][i] == 1){ // Player 1 pawn = red circle
cout << " | " << "\033[1;31m" << "\u25EF" << "\033[0m";
} else if(currentBoard[j][i] == 3){ // Player 1 king = red diamond
cout << " | " << "\033[1;31m" << "\u25C6" << "\033[0m";
} else if(currentBoard[j][i] == 2){ // Player 2 pawn = blue circle
cout << " | " << "\033[1;34m" << "\u25EF" << "\033[0m";
} else if(currentBoard[j][i] == 4){ // Player 2 king = blue diamond
cout << " | " << "\033[1;34m" << "\u25C6" << "\033[0m";
} else {
cout << " | " << currentBoard[j][i];
}
} else {
cout << " | ";
}
if(i == 7){
cout << " | " << '\n' << "|---| |---|---|---|---|---|---|---|---|" << '\n';
i = -1;
j++;
}
}
cout << '\n';
}
// Takes the choice of move from legal move list and implements move
void ImplementMove(int moveNum, int board[8][8] = currentBoard, vector<vector<Coordinate>>& moves = movesList,
vector<vector<Coordinate>*>& jumps = jumpsList){
moveNum--;
bool jump = jumps.size() > 0;
int piece;
if(!jump){
piece = board[moves.at(moveNum).at(0).y][moves.at(moveNum).at(0).x];
// Makes piece king when needed
if ((moves.at(moveNum).at(1).y == 0 && piece == 1) || (moves.at(moveNum).at(1).y == 7 && piece == 2)) {
board[moves.at(moveNum).at(1).y][moves.at(moveNum).at(1).x] = piece + 2;
// Keeps track of # pcs
if(piece == 1){
currentP1Kings++;
} else{
currentP2Kings++;
}
} else {
board[moves.at(moveNum).at(1).y][moves.at(moveNum).at(1).x] = piece;
}
board[moves.at(moveNum).at(0).y][moves.at(moveNum).at(0).x] = 0;
} else {
piece = board[jumps.at(moveNum)->at(0).y][jumps.at(moveNum)->at(0).x];
int jumpedX;
int jumpedY;
// Clears starting square
board[jumps.at(moveNum)->at(0).y][jumps.at(moveNum)->at(0).x] = 0;
for (int i = 0; i < jumps.at(moveNum)->size(); i++) {
if(i == jumps.at(moveNum)->size() - 1){
// Makes piece king when needed
if ((jumps.at(moveNum)->at(i).y == 0 && piece == 1) || (jumps.at(moveNum)->at(i).y == 7 && piece == 2)){
board[jumps.at(moveNum)->at(i).y][jumps.at(moveNum)->at(i).x] = piece + 2;
} else {
board[jumps.at(moveNum)->at(i).y][jumps.at(moveNum)->at(i).x] = piece;
}
} else {
// Gets coordinate of jumped square and clears it
jumpedX = (abs(jumps.at(moveNum)->at(i).x + jumps.at(moveNum)->at(i+1).x)) / 2;
jumpedY = (abs(jumps.at(moveNum)->at(i).y + jumps.at(moveNum)->at(i+1).y)) / 2;
// Keeps track of # pcs
int jumpedPc = board[jumpedY][jumpedX];
if(board == currentBoard){
currentTotalPcs--;
if(jumpedPc == 1 || jumpedPc == 3){
currentP1Pcs--;
if(jumpedPc == 3){
currentP1Kings--;
}
} else {
currentP2Pcs--;
if(jumpedPc == 4){
currentP2Kings--;
}
}
}
board[jumpedY][jumpedX] = 0;
}
}
}
}
// Asks user for starting player
bool player1Starts(bool userPlaying){
string resp;
if(userPlaying){
cout << "Would you like to go first? (Y/N) \n";
} else {
cout << "Would you like player 1 to go first? (Y/N) \n";
}
cin >> resp;
while(resp.compare("Y") != 0 && resp.compare("N") != 0){
cout << "That was an invalid response. Please respond with 'Y' or 'N'." << '\n';
cin >> resp;
}
if(resp.compare("Y") == 0){
return true;
} else {
return false;
}
}
// Tests if a player has won
int gameOver(int board[8][8] = currentBoard){
bool player1Wins = true;
bool player2Wins = true;
for(int i = 0, j = 0; j < 8; i++){
if(player2Wins && (board[j][i] == 1 || board[j][i] == 3)){
player2Wins = false;
} else if(player1Wins && (board[j][i] == 2 || board[j][i] == 4)){
player1Wins = false;
}
if (!player1Wins && !player2Wins) {
return 0;
}
if(i == 7){
i = -1;
j++;
}
}
if (player1Wins) {
return 1;
} else {
return 2;
}
}
void copyBoard(int from[8][8], int to[8][8]){
for(int i = 0, j = 0; j < 8; i++){
to[j][i] = from[j][i];
if(i == 7){
i = -1;
j++;
}
}
}
// Heuristic function
int evalFunc(int board[8][8], int player, int depth, bool noMoves, int numMoves){
int val = 0;
int p1Pawns = 0;
int p2Pawns = 0;
int p1Kings = 0;
int p2Kings = 0;
int p1Bottom = 0;
int p2Top = 0;
int p1MidBox = 0;
int p2MidBox = 0;
int p1MidRows = 0;
int p2MidRows = 0;
int p1Pcs = 0;
int p2Pcs = 0;
int totalpcs = 0;
for(int i = 0, j = 0; j < 8; i++){
// Home Rows (+- 50)
if(j == 0 && (board[j][i] == 2 || board[j][i] == 4)){
p2Top++;
} else if(j == 7 && (board[j][i] == 1 || board[j][i] == 3)){
p1Bottom++;
}
// Pawns (+- 100)
if(board[j][i] == 1){
val-=100;
p1Pawns++;
p1Pcs++;
totalpcs++;
} else if(board[j][i] == 2){
val+=100;
p2Pawns++;
p2Pcs++;
totalpcs++;
} else if(board[j][i] == 3){ // Kings (+- 155)
val-=155;
p1Kings++;
p1Pcs++;
totalpcs++;
} else if(board[j][i] == 4){
val += 155;
p2Kings++;
p2Pcs++;
totalpcs++;
}
// Middle box (+- 50)
if((i >= 2 && i <= 5) && (j >= 2 && j <= 5)){
if (board[j][i] == 2 || board[j][i] == 4) {
p2MidBox++;
} else if(board[j][i] == 2 || board[j][i] == 4){
p1MidBox++;
}
}
// Mid-board, but not in middle box (+- 10)
if((i < 2 || i > 5) && (j >= 2 && j <= 5)){
if (board[j][i] == 2 || board[j][i] == 4) {
p2MidRows++;
} else if(board[j][i] == 2 || board[j][i] == 4){
p1MidRows++;
}
}
if(i == 7){
i = -1;
j++;
}
}
// Uses different heuristic for end-game. Players care more about kings and total pcs left.
// End-game is "triggered" when one player has less than 2/3 of the other player's pcs, when there are
// 10 or less pcs on the board, or when there are less than 15 pcs and one player has more kings.
if(currentTotalPcs <= 10 || (currentP2Pcs*(2/3) >= currentP1Pcs) || (currentP1Pcs*(2/3) >= currentP2Pcs)
|| (currentTotalPcs <= 14 && abs(currentP1Kings - currentP2Kings) > 0)){
// Different depending on who has more pcs. Winner will want to trade more.
if(currentP2Pcs > currentP1Pcs && p2Pcs > p1Pcs){
val += ((currentTotalPcs - totalpcs)*30);
} else if(currentP1Pcs > currentP2Pcs && p1Pcs > p2Pcs) {
val -= ((currentTotalPcs - totalpcs)*30);
}
// Kings are more valuable (+- 20)
val += p2Kings*20;
val -= p1Kings*20;
// If early or mid game, positioning and pcs count matters more
} else {
val += ((p2Top*50) + (p2MidBox*50) + (p2MidRows*10));
val -= ((p1Bottom*50) + (p1MidBox*50) + (p1MidRows*10));
}
// Values home row more if other player still has pawns to king (+- 40)
if(p1Pawns > 0){
val+=40*p2Top;
} else if(p2Pawns > 0){
val-=40*p1Bottom;
}
// Makes AI take the closer win and push off loss
// Will also make AI try to block their opponent if it means winning fastest
if(player == 2 && (noMoves || gameOver(board) == 1)){
val -= (500 + (700 - (depth*10)));
} else if (player == 1 && (noMoves || gameOver(board) == 2)){
val += (500 + (700 - (depth*10)));
}
// Random number added to make it randomly choose between multiple "equal" moves
val += rand() % 10;
return val;
}
// Global variable to keep track of time limit
bool timeLimitPassed = false;
// Alpha Beta Search
int alphaBeta(int board[8][8], int depthLeft, int alpha, int beta, bool maxPlayer, time_t endTime,
int currentDepth, bool root = false){
// Checks if time limit has run out
if(timeLimitPassed || time(nullptr) >= endTime){
timeLimitPassed = true;
return 0;
}
int boardCopy[8][8] = {0};
vector<vector<Coordinate>> nodeMoves;
vector<vector<Coordinate>*> nodeJumps;
nodeMoves.reserve(200);
int value;
int tmpValue;
int bestMove = 1;
int player = maxPlayer ? 2 : 1;
getLegalMoves(player, board, nodeMoves, nodeJumps);
int moveAmt = nodeJumps.size() > 0 ? nodeJumps.size() : nodeMoves.size();
// Runs evaluation function at max depth or end-game board, but will continue down a branch if there is a forced jump
if((depthLeft <= 0 && (nodeJumps.size() == 0)) || moveAmt == 0 || gameOver(board) > 0){
return evalFunc(board, player, currentDepth, (moveAmt == 0), moveAmt);
}
if(maxPlayer){
value = -90000;
for(int i = 1; i <= moveAmt; i++){
copyBoard(board, boardCopy);
ImplementMove(i, boardCopy, nodeMoves, nodeJumps);
tmpValue = alphaBeta(boardCopy, depthLeft-1, alpha, beta, false, endTime, currentDepth+1);
if(tmpValue > value){
value = tmpValue;
if(root){
bestMove = i;
}
}
alpha = max(alpha, value);
if(alpha >= beta){
break;
}
}
if(root){
return bestMove;
} else {
return value;
}
} else {
value = 90000;
for(int i = 1; i <= moveAmt; i++){
copyBoard(board, boardCopy);
ImplementMove(i, boardCopy, nodeMoves, nodeJumps);
tmpValue = alphaBeta(boardCopy, depthLeft-1, alpha, beta, true, endTime, currentDepth+1);
if(tmpValue < value){
value = tmpValue;
if(root){
bestMove = i;
}
}
beta = min(beta, value);
if(alpha >= beta){
break;
}
}
if(root){
return bestMove;
} else {
return value;
}
}
}
// Implements Iterative Deepening for the Alpha Beta Search
int iterativeDeepening(int seconds, bool player2 = true){
timeLimitPassed = false;
int bestMove = 1;
int move;
time_t startTime = time(nullptr);
time_t endTime = startTime + seconds;
int depth = 1;
// If there is only one move, do it
if(movesList.size() == 1 || jumpsList.size() == 1){
cout << "Depth: 0\nTime Searching: " << time(nullptr) - startTime << " Seconds\n";
return 1;
}
// If there is less than half the time left, then it won't finish the next iteration, so stop
while((time(nullptr) + (seconds/2)) <= endTime){
move = alphaBeta(currentBoard, depth, -90000, 90000, player2, endTime, 0, true);
if(!timeLimitPassed){
bestMove = move;
depth++;
}
}
cout << "Depth: " << depth << "\nTime Searching: " << time(nullptr) - startTime << " Seconds\n";
return bestMove;
}
// Keeps track of turns without jumps. After 80 turns (40 each player) calls a draw.
bool callDraw(){
if(jumpsList.size() == 0){
turnCount++;
} else {
turnCount = 0;
}
if(turnCount >= 80){
cout << "It has been 80 turns since a piece has been jumped. The game is a draw.\n";
return true;
} else {
return false;
}
}
// Plays AI vs AI
void playAIvsAI(int startingPlayer, int seconds){
if(startingPlayer == 1){
getLegalMoves(1);
// If no moves then game ends
if (movesList.size() == 0) {
cout << "Player 1 has no possible moves. Player 2 wins.\n";
return;
}
cout << "Player 1 is thinking...\n";
ImplementMove(iterativeDeepening(floor(seconds), false));
printBoard();
}
while(true){
// Player 2 turn
getLegalMoves(2);
// If no moves then game ends
if(movesList.size() == 0){
cout << "Player 2 has no possible moves. Player 1 wins.\n";
return;
}
cout << "Player 2 is thinking...\n";
ImplementMove(iterativeDeepening(floor(seconds)));
printBoard();
// Checks for draw
if(callDraw()){
return;
}
// Player 1 turn
getLegalMoves(1);
// If no moves then game ends
if(movesList.size() == 0) {
cout << "Player 1 has no possible moves. Player 2 wins.\n";
return;
}
cout << "Player 1 is thinking...\n";
ImplementMove(iterativeDeepening(floor(seconds), false));
printBoard();
// Checks for draw
if(callDraw()){
return;
}
}
}
// Gets user's move choice
int getMoveChoice(){
double moveChoice;
int numChoices = jumpsList.size() > 0 ? jumpsList.size() : movesList.size();
cout << "Which move would you like to do? \n";
cin >> moveChoice;
moveChoice = round(moveChoice);
while(moveChoice < 1 || moveChoice > numChoices){
cout << "That was an invalid choice.\nPlease give a number choice from the list of possible moves.\n";
cin >> moveChoice;
moveChoice = round(moveChoice);
}
return moveChoice;
}
void playGame(){
// Reserves vector space so pointers don't get messed up when vector resizes
movesList.reserve(200);
// User decides game board
double input;
cout << "Welcome to checkers!\nPlease respond with the number of your choice:\n";
cout << "1. Begin a new game\n2. Load a custom board\n";
cin >> input;
while(input != 1 && input != 2){
cout << "That was an invalid choice. Please respond with 1 or 2.\n";
cin >> input;
}
if(input == 1){
initStartBoard();
} else {
initUserBoard();
}
// User Vs AI or AI Vs AI
bool AIvsAI = false;
cout << "Please choose a game mode:\n";
cout << "1. User Vs AI\n2. AI Vs AI\n";
cin >> input;
while(input != 1 && input != 2){
cout << "That was an invalid choice. Please respond with 1 or 2.\n";
cin >> input;
}
AIvsAI = (input == 2);
// User inputs time limit
cout << "Please enter the number of seconds (integer value) that the AI has to move\n";
cin >> input;
while(floor(input) <= 1){
cout << "Please enter an integer value greater than 1\n";
cin >> input;
}
// Decides starting player
int startingPlayer;
if(player1Starts(!AIvsAI)){
startingPlayer = 1;
} else {
startingPlayer = 2;
}
cout << "The starting player is player " << startingPlayer << ".\n\n";
printBoard();
// Checks if the board can be played
if(gameOver() > 0){
return;
}
// Differs here if AI vs AI
if(AIvsAI){
playAIvsAI(startingPlayer, input);
return;
}
if(startingPlayer == 1){
getLegalMoves(1);
// If no moves then game ends
if (movesList.size() == 0) {
cout << "You have no possible moves. The AI wins.\n";
return;
}
printMoves();
ImplementMove(getMoveChoice());
printBoard();
}
while(true){
// Player 2 turn
getLegalMoves(2);
// If no moves then game ends
if(movesList.size() == 0){
cout << "The AI is unable to move. You win!\n";
return;
}
cout << "I am thinking...\n";
ImplementMove(iterativeDeepening(floor(input)));
printBoard();
// Checks for draw
if(callDraw()){
return;
}
// Player 1 turn
getLegalMoves(1);
// If no moves then game ends
if(movesList.size() == 0) {
cout << "You have no possible moves. The AI wins.\n";
return;
}
printMoves();
ImplementMove(getMoveChoice());
printBoard();
// Checks for draw
if(callDraw()){
return;
}
}
}
int main(int argc, const char * argv[]) {
srand(time(nullptr));
playGame();
return 0;
}