-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
1388 lines (1140 loc) · 48.2 KB
/
Copy pathmain.cpp
File metadata and controls
1388 lines (1140 loc) · 48.2 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
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include <bits/stdc++.h>
using namespace std;
using std::chrono::high_resolution_clock;
using std::chrono::duration;
using std::chrono::duration_cast;
struct Piece {
// true is white, false is black
bool color;
char name;
vector<string> possibleMove;
};
struct Task {
vector<vector<Piece>> boardState;
string move;
};
// global queue
queue<Task> taskQueue;
pthread_mutex_t queueLock;
pthread_mutex_t resultsLock;
vector<pair<int, string>> results;
// functions for establishing the graph
void initialBoard(vector<vector<Piece>>& boardState);
void printBoard(vector<vector<Piece>>& boardState);
void printPossibleMoves(vector<vector<Piece>>& boardState);
vector<string> possibleMoves(vector<vector<Piece>>& boardState, int i, int j);
bool inCheck(vector<vector<Piece>>& boardState, bool team);
bool validSpot(string& pos, vector<vector<Piece>>& boardState);
// functions to play the game
void playFirstMoves(vector<vector<Piece>>& boardState, vector<string>& moveList);
void convertToIJ(string& move, int& iVal, int& jVal);
string convertToUCI(int iCurr, int jCurr, int iEnd, int jEnd);
// minimax functions
pair<int, string> minimax(vector<vector<Piece>> boardState, int depth, bool team, string bestMove, int alpha, int beta);
int evaluateScore(vector<vector<Piece>>& boardState, bool team);
void simulateMove(vector<vector<Piece>>& boardState, int i, int j, string& move);
// parallel function
// no need for trampoline, queue is globally used
void* worker(void* arg) {
while (true) {
Task task;
// Lock
pthread_mutex_lock(&queueLock);
if (taskQueue.empty()) {
pthread_mutex_unlock(&queueLock);
// nothing else for this thread to do!
break;
}
// Pop a task from the queue
task = taskQueue.front();
taskQueue.pop();
pthread_mutex_unlock(&queueLock);
// Compute the minimax result
string bestMove;
int score = minimax(task.boardState, 1, false, bestMove, -INT_MAX, INT_MAX).first;
// Lock
pthread_mutex_lock(&resultsLock);
results.push_back({score, task.move});
pthread_mutex_unlock(&resultsLock);
}
return NULL;
}
int main(int argc, char* argv[]) {
if(argc != 2) throw runtime_error("Please include number of threads in arguements.");
int ntasks = stoi(argv[1]);
ios_base::sync_with_stdio(false);
cin.tie(NULL);
vector<vector<Piece>> boardState(8, vector<Piece>(8));
initialBoard(boardState);
// Example input:
// If depth is odd olways chooses a black move
/*
Example Game vs Stockfish
Graph Generated on this Input
24
e2e4 e7e5 g1f3 b8c6 f1c4 g8f6 d2d3 f8c5 c2c3 d7d6 c4f7 e8f7 c3c4 f7e8 b1c3 h7h6 f3e5 d6e5 d1h5 f6h5 c3d5 c5f2 e1f2 h8f8
4
e2e4 g8f6 e4e5 f6d5
*/
int numInput;
cin >> numInput;
vector<string> moveList(numInput);
for(auto& move : moveList) cin >> move;
playFirstMoves(boardState, moveList);
printBoard(boardState);
for(int i = 0; i < 8; ++i) {
for(int j = 0; j < 8; ++j) {
if(boardState[i][j].name != '-') {
boardState[i][j].possibleMove = possibleMoves(boardState, i, j);
}
}
}
printPossibleMoves(boardState);
// here the board has the most updated moves, so we want to set up all of the tasks
// each task holds a new updated board state, and the move associated with that state
for(int i = 0; i < 8; ++i) {
for(int j = 0; j < 8; ++j) {
if(boardState[i][j].name != '-' && boardState[i][j].color) {
for(auto x : boardState[i][j].possibleMove) {
// void simulateMove(vector<vector<Piece>>& boardState, int i, int j, string& move);
vector<vector<Piece>> boardStateCpy = boardState;
simulateMove(boardStateCpy, i, j, x);
Task toPush;
toPush.boardState = boardStateCpy;
toPush.move = to_string(i) + to_string(j) + x;
taskQueue.push(toPush);
}
}
}
}
pthread_mutex_init(&queueLock, NULL);
pthread_mutex_init(&resultsLock, NULL);
string printMove;
bool team = true;
// Start a timer
high_resolution_clock::time_point begin = high_resolution_clock::now();
// setup thread vector
vector<pthread_t> threads(ntasks);
for(int i=0; i < ntasks; ++i) {
int status = ::pthread_create(&threads[i], nullptr, worker, nullptr);
if (status != 0) {
::perror("thread create");
return 1;
}
}
// Wait for all to finish
for(int i=0; i < ntasks; ++i) {
pthread_join(threads[i], nullptr);
}
int bestScore = -INT_MAX;
string bestMove;
cout << endl << endl << endl;
for(int i = 0; i < results.size(); ++i) {
if(results[i].first > bestScore) {
bestScore = results[i].first;
bestMove = results[i].second;
}
}
cout << endl << bestScore << " " << bestMove << endl;
// bestMove = minimax(boardState, 0, team, bestMove, -INT_MAX, INT_MAX).second;
// int score = minimax(boardState, 0, !team, bestMove, -INT_MAX, INT_MAX).first;
// cout << "score " << score << endl << endl;
printMove = convertToUCI(stoi(bestMove.substr(0, 1)), stoi(bestMove.substr(1, 1)), stoi(bestMove.substr(2, 1)), stoi(bestMove.substr(3, 1)));
cout << printMove << endl;
int currI = stoi(bestMove.substr(0, 1));
int currJ = stoi(bestMove.substr(1, 1));
string tempMove = bestMove.substr(2, 2);
for(int i = 0; i < 8; ++i) {
for(int j = 0; j < 8; ++j) {
if(boardState[i][j].name != '-') {
boardState[i][j].possibleMove = possibleMoves(boardState, i, j);
}
}
}
simulateMove(boardState, currI, currJ, tempMove);
printBoard(boardState);
auto time_span = duration_cast<duration<double>>(high_resolution_clock::now() - begin);
std::cerr << endl << ntasks << " Total Threads: " << time_span.count() << '\n';
return 0;
}
pair<int, string> minimax(vector<vector<Piece>> boardState, int depth, bool team, string bestMove, int alpha, int beta) {
// Base case: when the depth limit is reached, evaluate the board
if(depth == 4) {
return make_pair(evaluateScore(boardState, team), bestMove);
}
vector<vector<Piece>> copyState;
int bestScore = team ? -INT_MAX : INT_MAX;
for(int i = 0; i < 8; ++i) {
for(int j = 0; j < 8; ++j) {
if(boardState[i][j].name != '-') {
boardState[i][j].possibleMove = possibleMoves(boardState, i, j);
if(boardState[i][j].color == team) {
for(auto& move : boardState[i][j].possibleMove) {
copyState = boardState;
simulateMove(copyState, i, j, move);
//cout << move << endl;
// printBoard(copyState);
int tempScore = minimax(copyState, depth + 1, !team, bestMove, alpha, beta).first;
//cout << "Score " << temp << endl;
if (team) { // Max
if (tempScore > bestScore) {
bestScore = tempScore;
bestMove = to_string(i) + to_string(j) + move;
}
// tracks best possible score
alpha = max(alpha, bestScore);
} else { // Mini
if (tempScore < bestScore) {
bestScore = tempScore;
bestMove = to_string(i) + to_string(j) + move;
}
// tracks worst possible score
beta = min(beta, bestScore);
}
// since the beta route is chosen by the opponent, this wont actually be able to run
// so we prune
if (alpha >= beta) {
break;
}
}
}
}
}
}
return make_pair(bestScore, bestMove); // Return the best score found
}
bool inCheck(vector<vector<Piece>>& boardState, bool team) {
// find position of king for team
int kingI = -1;
int kingJ = -1;
for(int i = 0; i < 8; ++i) {
for(int j = 0; j < 8; ++j) {
if(boardState[i][j].name == 'K' && boardState[i][j].color == team) {
kingI = i;
kingJ = j;
break;
}
}
}
if (kingI == -1 || kingJ == -1) return false;
for(int i = 0; i < 8; ++i) {
for(int j = 0; j < 8; ++j) {
if(boardState[i][j].name != '-') {
boardState[i][j].possibleMove = possibleMoves(boardState, i, j);
}
}
}
string kingPos = to_string(kingI) + to_string(kingJ);
for(int i = 0; i < 8; ++i) {
for(int j = 0; j < 8; ++j) {
if(boardState[i][j].color != team) {
for(auto& x : boardState[i][j].possibleMove) {
if (x == kingPos) return true;
}
}
}
}
return false;
}
int evaluateScore(vector<vector<Piece>>& boardState, bool team) {
// used ai for the scoring tables
// only effective for midgame
int pawnTable[8][8] = {
{ 0, 5, 5, -10, -10, 5, 5, 0 },
{ 0, 10, 10, 0, 0, 10, 10, 0 },
{ 0, 10, 20, 30, 30, 20, 10, 0 },
{ 0, 10, 10, 20, 20, 10, 10, 0 },
{ 0, 10, 10, 20, 20, 10, 10, 0 },
{ 0, 10, 10, 0, 0, 10, 10, 0 },
{ 0, 5, 5, -10, -10, 5, 5, 0 },
{ 0, 0, 0, 0, 0, 0, 0, 0 }
};
int knightTable[8][8] = {
{ -50, -40, -30, -30, -30, -30, -40, -50 },
{ -40, -20, 0, 0, 0, 0, -20, -40 },
{ -30, 0, 10, 15, 15, 10, 0, -30 },
{ -30, 5, 15, 20, 20, 15, 5, -30 },
{ -30, 0, 15, 20, 20, 15, 0, -30 },
{ -30, 5, 10, 15, 15, 10, 5, -30 },
{ -40, -20, 0, 5, 5, 0, -20, -40 },
{ -50, -40, -30, -30, -30, -30, -40, -50 }
};
int bishopTable[8][8] = {
{ -20, -10, -10, -10, -10, -10, -10, -20 },
{ -10, 5, 0, 0, 0, 0, 5, -10 },
{ -10, 10, 10, 10, 10, 10, 10, -10 },
{ -10, 0, 10, 10, 10, 10, 0, -10 },
{ -10, 5, 5, 10, 10, 5, 5, -10 },
{ -10, 0, 5, 10, 10, 5, 0, -10 },
{ -10, 0, 0, 0, 0, 0, 0, -10 },
{ -20, -10, -10, -10, -10, -10, -10, -20 }
};
int rookTable[8][8] = {
{ 0, 0, 0, 5, 5, 0, 0, 0 },
{ 0, 0, 0, 5, 5, 0, 0, 0 },
{ 0, 0, 0, 5, 5, 0, 0, 0 },
{ 5, 5, 5, 10, 10, 5, 5, 5 },
{ 5, 5, 5, 10, 10, 5, 5, 5 },
{ 0, 0, 0, 5, 5, 0, 0, 0 },
{ 0, 0, 0, 5, 5, 0, 0, 0 },
{ 0, 0, 0, 0, 0, 0, 0, 0 }
};
int queenTable[8][8] = {
{ -20, -10, -10, -5, -5, -10, -10, -20 },
{ -10, 0, 0, 0, 0, 0, 0, -10 },
{ -10, 0, 5, 5, 5, 5, 0, -10 },
{ -5, 0, 5, 5, 5, 5, 0, -5 },
{ 0, 0, 5, 5, 5, 5, 0, -5 },
{ -10, 5, 5, 5, 5, 5, 0, -10 },
{ -10, 0, 5, 0, 0, 0, 0, -10 },
{ -20, -10, -10, -5, -5, -10, -10, -20 }
};
int kingTable[8][8] = {
{ -30, -40, -40, -50, -50, -40, -40, -30 },
{ -30, -40, -40, -50, -50, -40, -40, -30 },
{ -30, -40, -40, -50, -50, -40, -40, -30 },
{ -30, -40, -40, -50, -50, -40, -40, -30 },
{ -20, -30, -30, -40, -40, -30, -30, -20 },
{ -10, -20, -20, -20, -20, -20, -20, -10 },
{ 20, 20, 0, 0, 0, 0, 20, 20 },
{ 20, 30, 10, 0, 0, 10, 30, 20 }
};
int score = 0;
for (int i = 0; i < 8; ++i) {
for (int j = 0; j < 8; ++j) {
if (boardState[i][j].name == '-') {
continue;
}
int pieceValue = 0;
if (boardState[i][j].name == 'K') pieceValue = 100 + kingTable[i][j];
else if (boardState[i][j].name == 'Q') pieceValue = 9 + queenTable[i][j];
else if (boardState[i][j].name == 'B') pieceValue = 3 + bishopTable[i][j];
else if (boardState[i][j].name == 'R') pieceValue = 3 + rookTable[i][j];
else if (boardState[i][j].name == 'N') pieceValue = 5 + knightTable[i][j];
else if (boardState[i][j].name == 'P') pieceValue = 1 + pawnTable[i][j];
if (boardState[i][j].color == team) {
score += pieceValue;
} else {
score -= pieceValue;
}
}
}
if (inCheck(boardState, team)) {
score -= 60; // Apply a large penalty if the king is in check
}
// Check if the opponent's king is in check
if (inCheck(boardState, !team)) {
score += 60; // Apply a large reward if the opponent's king is in check
}
return score;
}
void simulateMove(vector<vector<Piece>>& boardState, int i, int j, string& move) {
string endPos, convertedEndPos;
int currI, currJ, endI, endJ;
Piece currPiece;
currI = i;
currJ = j;
endI = stoi(move.substr(0, 1));
endJ = stoi(move.substr(1, 1));
convertedEndPos = to_string(endI) + to_string(endJ);
if(boardState[currI][currJ].name != '-') {
boardState[i][j].possibleMove = possibleMoves(boardState, i, j);
for(auto& possibleMove : boardState[currI][currJ].possibleMove) {
if(possibleMove == convertedEndPos) {
if(boardState[endI][endJ].name == '-') {
swap(boardState[currI][currJ], boardState[endI][endJ]);
}
else {
boardState[endI][endJ].name = '-';
boardState[endI][endJ].color = true;
swap(boardState[currI][currJ], boardState[endI][endJ]);
}
break;
}
}
}
}
void playFirstMoves(vector<vector<Piece>>& boardState, vector<string>& moveList) {
string currPos, endPos, convertedEndPos;
int currI, currJ, endI, endJ;
Piece currPiece;
// we want to start as white
bool colorOrder = true;
for(auto& move : moveList) {
currPos = move.substr(0, 2);
convertToIJ(currPos, currI, currJ);
endPos = move.substr(2, 2);
convertToIJ(endPos, endI, endJ);
convertedEndPos = to_string(endI) + to_string(endJ);
boardState[currI][currJ].possibleMove = possibleMoves(boardState, currI, currJ);
if(boardState[currI][currJ].name != '-' && boardState[currI][currJ].color == colorOrder) {
for(auto& possibleMove : boardState[currI][currJ].possibleMove) {
if(possibleMove == convertedEndPos) {
if(boardState[endI][endJ].name == '-') {
swap(boardState[currI][currJ], boardState[endI][endJ]);
}
else {
boardState[endI][endJ].name = '-';
boardState[endI][endJ].color = true;
swap(boardState[currI][currJ], boardState[endI][endJ]);
}
continue;
}
}
}
else throw runtime_error("Incorrect Piece Position Called");
colorOrder = !colorOrder;
}
}
void convertToIJ(string& move, int& iVal, int& jVal) {
iVal = 7 - (move[1] - 49);
jVal = move[0] - 'a';
}
string convertToUCI(int iCurr, int jCurr, int iEnd, int jEnd) {
char rowStart = '1' + (7 - iCurr);
char colStart = 'a' + jCurr;
char rowEnd = '1' + (7 - iEnd);
char colEnd = 'a' + jEnd;
return string(1, colStart) + string(1, rowStart) + string(1, colEnd) + string(1, rowEnd);
}
void initialBoard(vector<vector<Piece>>& boardState) {
for(int i = 2; i < 6; ++i) {
for(int j = 0; j < 8; ++j) {
Piece basic;
basic.color = true;
basic.name = '-';
boardState[i][j] = basic;
}
}
for(int j = 0; j < 8; ++j) {
Piece pawnWhite;
pawnWhite.color = true;
pawnWhite.name = 'P';
Piece pawnBlack;
pawnBlack.color = false;
pawnBlack.name = 'P';
boardState[6][j] = pawnWhite;
boardState[1][j] = pawnBlack;
}
Piece knightWhite;
knightWhite.color = true;
knightWhite.name = 'N';
Piece knightBlack;
knightBlack.color = false;
knightBlack.name = 'N';
boardState[7][1] = knightWhite;
boardState[0][1] = knightBlack;
Piece knightWhite2;
knightWhite2.color = true;
knightWhite2.name = 'N';
Piece knightBlack2;
knightBlack2.color = false;
knightBlack2.name = 'N';
boardState[7][6] = knightWhite2;
boardState[0][6] = knightBlack2;
Piece rookWhite;
rookWhite.color = true;
rookWhite.name = 'R';
Piece rookBlack;
rookBlack.color = false;
rookBlack.name = 'R';
boardState[7][0] = rookWhite;
boardState[0][0] = rookBlack;
Piece rookWhite2;
rookWhite2.color = true;
rookWhite2.name = 'R';
Piece rookBlack2;
rookBlack2.color = false;
rookBlack2.name = 'R';
boardState[7][7] = rookWhite2;
boardState[0][7] = rookBlack2;
Piece bishopWhite;
bishopWhite.color = true;
bishopWhite.name = 'B';
Piece bishopBlack;
bishopBlack.color = false;
bishopBlack.name = 'B';
boardState[7][2] = bishopWhite;
boardState[0][2] = bishopBlack;
Piece bishopWhite2;
bishopWhite2.color = true;
bishopWhite2.name = 'B';
Piece bishopBlack2;
bishopBlack2.color = false;
bishopBlack2.name = 'B';
boardState[7][5] = bishopWhite2;
boardState[0][5] = bishopBlack2;
Piece kingWhite;
kingWhite.color = true;
kingWhite.name = 'K';
Piece kingBlack;
kingBlack.color = false;
kingBlack.name = 'K';
boardState[7][4] = kingWhite;
boardState[0][4] = kingBlack;
Piece queenWhite;
queenWhite.color = true;
queenWhite.name = 'Q';
Piece queenBlack;
queenBlack.color = false;
queenBlack.name = 'Q';
boardState[7][3] = queenWhite;
boardState[0][3] = queenBlack;
}
void printBoard(vector<vector<Piece>>& boardState) {
for(int i = 0; i < 8; ++i) {
for(int j = 0; j < 8; ++j) {
if(boardState[i][j].name == '-') cout << " " << boardState[i][j].name << " ";
else {
cout << boardState[i][j].name;
if(boardState[i][j].color) cout << "w ";
else cout << "b ";
}
}
cout << endl;
}
}
void printPossibleMoves(vector<vector<Piece>>& boardState) {
for(int i = 0; i < 8; ++i) {
for(int j = 0; j < 8; ++j) {
if(boardState[i][j].name != '-') {
cout << boardState[i][j].name;
if(boardState[i][j].color) cout << "w ";
else cout << "b ";
vector<string> moves = possibleMoves(boardState, i, j);
for(auto& s : boardState[i][j].possibleMove) cout << s << " ";
cout << endl;
}
}
}
}
vector<string> possibleMoves(vector<vector<Piece>>& boardState, int i, int j) {
vector<string> toReturn;
if(boardState[i][j].name == '-') return toReturn;
else if(boardState[i][j].name == 'P') {
if(boardState[i][j].color) { // if white
if(i > 0) {
string toAdd = to_string(i-1) + to_string(j);
if(validSpot(toAdd, boardState)) {
toReturn.push_back(toAdd);
if(i == 6) {
string toAdd = to_string(i-2) + to_string(j);
if(validSpot(toAdd, boardState)) toReturn.push_back(toAdd);
}
}
}
if(i > 0) {
if(j > 0) {
if(boardState[i-1][j-1].name != '-' && !boardState[i-1][j-1].color) {
string toAdd = to_string(i-1) + to_string(j-1);
// no need to check if "valid", taking a piece
toReturn.push_back(toAdd);
}
}
if(j < 7) {
if(boardState[i-1][j+1].name != '-' && !boardState[i-1][j+1].color) {
string toAdd = to_string(i-1) + to_string(j+1);
// no need to check if "valid", taking a piece
toReturn.push_back(toAdd);
}
}
}
}
else { // if black
if(i < 7) {
string toAdd = to_string(i+1) + to_string(j);
if(validSpot(toAdd, boardState)) {
toReturn.push_back(toAdd);
if(i == 1) {
string toAdd = to_string(i+2) + to_string(j);
if(validSpot(toAdd, boardState)) toReturn.push_back(toAdd);
}
}
}
if(i < 7) {
if(j > 0) {
if(boardState[i+1][j-1].name != '-' && boardState[i+1][j-1].color) {
string toAdd = to_string(i+1) + to_string(j-1);
// no need to check if "valid", taking a piece
toReturn.push_back(toAdd);
}
}
if(j < 7) {
if(boardState[i+1][j+1].name != '-' && boardState[i+1][j+1].color) {
string toAdd = to_string(i+1) + to_string(j+1);
// no need to check if "valid", taking a piece
toReturn.push_back(toAdd);
}
}
}
}
return toReturn;
}
else if(boardState[i][j].name == 'N') { // Knight
if(boardState[i][j].color) { // if white
if(i > 0 && j < 6) {
if(boardState[i-1][j+2].name == '-' || !boardState[i-1][j+2].color) {
string toAdd = to_string(i-1) + to_string(j+2);
toReturn.push_back(toAdd);
}
}
if(i > 0 && j > 2) {
if(boardState[i-1][j-2].name == '-' || !boardState[i-1][j-2].color) {
string toAdd = to_string(i-1) + to_string(j-2);
toReturn.push_back(toAdd);
}
}
if(i < 7 && j < 6) {
if(boardState[i+1][j+2].name == '-' || !boardState[i+1][j+2].color) {
string toAdd = to_string(i+1) + to_string(j+2);
toReturn.push_back(toAdd);
}
}
if(i < 7 && j > 2) {
if(boardState[i+1][j-2].name == '-' || !boardState[i+1][j-2].color) {
string toAdd = to_string(i+1) + to_string(j-2);
toReturn.push_back(toAdd);
}
}
if(i > 1 && j < 7) {
if(boardState[i-2][j+1].name == '-' || !boardState[i-2][j+1].color) {
string toAdd = to_string(i-2) + to_string(j+1);
toReturn.push_back(toAdd);
}
}
if(i > 1 && j > 0) {
if(boardState[i-2][j-1].name == '-' || !boardState[i-2][j-1].color) {
string toAdd = to_string(i-2) + to_string(j-1);
toReturn.push_back(toAdd);
}
}
if(i < 6 && j < 7) {
if(boardState[i+2][j+1].name == '-' || !boardState[i+2][j+1].color) {
string toAdd = to_string(i+2) + to_string(j+1);
toReturn.push_back(toAdd);
}
}
if(i < 6 && j > 0) {
if(boardState[i+2][j-1].name == '-' || !boardState[i+2][j-1].color) {
string toAdd = to_string(i+2) + to_string(j-1);
toReturn.push_back(toAdd);
}
}
}
else { // if black
if(i > 0 && j < 6) {
if(boardState[i-1][j+2].color) {
string toAdd = to_string(i-1) + to_string(j+2);
toReturn.push_back(toAdd);
}
}
if(i > 0 && j > 2) {
if(boardState[i-1][j-2].color) {
string toAdd = to_string(i-1) + to_string(j-2);
toReturn.push_back(toAdd);
}
}
if(i < 7 && j < 6) {
if(boardState[i+1][j+2].color) {
string toAdd = to_string(i+1) + to_string(j+2);
toReturn.push_back(toAdd);
}
}
if(i < 7 && j > 1) {
if(boardState[i+1][j-2].color) {
string toAdd = to_string(i+1) + to_string(j-2);
toReturn.push_back(toAdd);
}
}
if(i > 1 && j < 7) {
if(boardState[i-2][j+1].color) {
string toAdd = to_string(i-2) + to_string(j+1);
toReturn.push_back(toAdd);
}
}
if(i > 1 && j > 0) {
if(boardState[i-2][j-1].color) {
string toAdd = to_string(i-2) + to_string(j-1);
toReturn.push_back(toAdd);
}
}
if(i < 6 && j < 7) {
if(boardState[i+2][j+1].color) {
string toAdd = to_string(i+2) + to_string(j+1);
toReturn.push_back(toAdd);
}
}
if(i < 6 && j > 0) {
if(boardState[i+2][j-1].color) {
string toAdd = to_string(i+2) + to_string(j-1);
toReturn.push_back(toAdd);
}
}
}
return toReturn;
}
else if(boardState[i][j].name == 'R') { // Rook
if(boardState[i][j].color) { // if white
bool yesPlus = true;
bool yesMinus = true;
for(int c = 1; c < 8; ++c) {
if(!yesPlus && !yesMinus) break;
if(yesPlus && i + c < 8) {
if(boardState[i + c][j].name == '-' || !boardState[i + c][j].color) {
string toAdd = to_string(i+c) + to_string(j);
toReturn.push_back(toAdd);
if (!boardState[i + c][j].color) yesPlus = false;
}
else yesPlus = false;
}
else yesPlus = false;
if(yesMinus && i - c > -1) {
if(boardState[i - c][j].name == '-' || !boardState[i - c][j].color) {
string toAdd = to_string(i-c) + to_string(j);
toReturn.push_back(toAdd);
if (!boardState[i - c][j].color) yesMinus = false;
}
else yesMinus = false;
}
else yesMinus = false;
}
yesPlus = true;
yesMinus = true;
for(int c = 1; c < 8; ++c) {
if(!yesPlus && !yesMinus) break;
if(yesPlus && j + c < 8) {
if(boardState[i][j + c].name == '-' || !boardState[i][j + c].color) {
string toAdd = to_string(i) + to_string(j+c);
toReturn.push_back(toAdd);
if (!boardState[i][j + c].color) yesPlus = false;
}
else yesPlus = false;
}
else yesPlus = false;
if(yesMinus && j - c > -1) {
if(boardState[i][j - c].name == '-' || !boardState[i][j - c].color) {
string toAdd = to_string(i) + to_string(j - c);
toReturn.push_back(toAdd);
if (!boardState[i][j - c].color) yesMinus = false;
}
else yesMinus = false;
}
else yesMinus = false;
}
}
else { // if black
bool yesPlus = true;
bool yesMinus = true;
for(int c = 1; c < 8; ++c) {
if(!yesPlus && !yesMinus) break;
if(yesPlus && i + c < 8) {
if(boardState[i + c][j].name == '-' || boardState[i + c][j].color) {
string toAdd = to_string(i+c) + to_string(j);
toReturn.push_back(toAdd);
if (boardState[i + c][j].name != '-' && boardState[i + c][j].color) yesPlus = false;
}
else yesPlus = false;
}
else yesPlus = false;
if(yesMinus && i - c > -1) {
if(boardState[i - c][j].name == '-' || boardState[i - c][j].color) {
string toAdd = to_string(i-c) + to_string(j);
toReturn.push_back(toAdd);
if (boardState[i - c][j].name != '-' && boardState[i - c][j].color) yesMinus = false;
}
else yesMinus = false;
}
else yesMinus = false;
}
yesPlus = true;
yesMinus = true;
for(int c = 1; c < 8; ++c) {
if(!yesPlus && !yesMinus) break;
if(yesPlus && j + c < 8) {
if(boardState[i][j + c].name == '-' || boardState[i][j + c].color) {
string toAdd = to_string(i) + to_string(j+c);
toReturn.push_back(toAdd);
if (boardState[i][j + c].name != '-' && boardState[i][j + c].color) yesPlus = false;
}
else yesPlus = false;
}
else yesPlus = false;
if(yesMinus && j - c > -1) {
if(boardState[i][j - c].name == '-' || boardState[i][j - c].color) {
string toAdd = to_string(i) + to_string(j - c);
toReturn.push_back(toAdd);
if (boardState[i][j - c].name != '-' && boardState[i][j - c].color) yesMinus = false;
}
else yesMinus = false;
}
else yesMinus = false;
}
}
}
else if(boardState[i][j].name == 'B') { // Bishop
if(boardState[i][j].color) { // if white
bool yesPlus = true;
bool yesMinus = true;
for(int c = 1; c < 8; ++c) {
if(!yesPlus && !yesMinus) break;
if(yesPlus && i + c < 8 && j + c < 8) {
if(boardState[i + c][j + c].name == '-' || !boardState[i + c][j + c].color) {
string toAdd = to_string(i+c) + to_string(j+c);
toReturn.push_back(toAdd);
if (!boardState[i + c][j + c].color) yesPlus = false;
}
else yesPlus = false;
}
else yesPlus = false;
if(yesMinus && i - c > -1 && j - c > -1) {
if(boardState[i - c][j - c].name == '-' || !boardState[i - c][j - c].color) {
string toAdd = to_string(i-c) + to_string(j-c);
toReturn.push_back(toAdd);
if (!boardState[i - c][j - c].color) yesMinus = false;
}
else yesMinus = false;
}
else yesMinus = false;
}
yesPlus = true;
yesMinus = true;
for(int c = 1; c < 8; ++c) {
if(!yesPlus && !yesMinus) break;
if(yesPlus && i + c < 8 && j - c > -1) {
if(boardState[i + c][j - c].name == '-' || !boardState[i + c][j - c].color) {
string toAdd = to_string(i+c) + to_string(j-c);
toReturn.push_back(toAdd);
if (!boardState[i + c][j - c].color) yesPlus = false;
}
else yesPlus = false;
}
else yesPlus = false;
if(yesMinus && i - c > -1 && j + c < 8) {
if(boardState[i - c][j + c].name == '-' || !boardState[i - c][j + c].color) {
string toAdd = to_string(i-c) + to_string(j+c);