-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathOthello.cpp
1082 lines (991 loc) · 43.9 KB
/
Othello.cpp
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
/**
* This program is to implement the othello game. Also an AI.
*
* @author Shixi Chen(Ethan)
* @section CPSC 1160 - 01
* @date Oct 25, 2019
*/
/**
* CAUTION!!
* Since I have used "●" and "◯" as the symbols of the disc, I am not sure
* is it gonna cause compilation error.(I have that issue on my home
* computer but not my laptop). If that is the case, just change the symbol in the
* function - "displayBoard". I think it's around line 315 - 317.
*/
/**
* Computer's features:
*
* Computer’s strategy is to evaluate next possible positions’
* scores and place it to the highest-score corresponding position. The method
* of evaluating possible coordinates is: for every single possible
* coordinate, calculate the number of discs that user will be flipped
* and also user’s number of available coordinates in the next round.
* Make a ratio between number of flipped disc and the number of user
* available positions. Also, the computer will add additional biases
* to the corners and sides. It will also try to eliminate user’s chance of
* getting corners by dividing a constant bias. At the end, computer
* will pick the highest ratio, and that will be the computer’s move.
*/
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
//Global data field
int width = 8;
int length = 8;
char black[8][8];
char white[8][8];
bool quit = false;
//Store all the existing coordinates into the vector
vector<string> checkDuplicate;
//Store B/C existing coordinates into the vector
vector<string> blackCoordinates;
vector<string> whiteCoordinates;
//Available next steps' coordinates are stored in this array
vector<string> nextStep;
//Function prototypes
string getUserInput();
void displayBoard(char blackC[][8], char whiteC[][8], vector<string> &nextCoor);
bool isGameOver();
vector<string> generateValidCoordinates(int turns, char blackC[][8], char whiteC[][8], vector<string> &blackCoor, vector<string> &whiteCoor);
void flip(string &s, int order, char blackC[][8], char whiteC[][8], vector<string> &blackCoor, vector<string> &whiteCoor);
void displayAvailableCoordinates(vector<string> &coor);
string computerMove();
bool isValidKey(string &key);
/**
* Print out all the elements in the vector.(linear)
* @tparam T, type
* @param a, vector
*/
template<typename T>
void printArray(vector<T> a) {
for (int i = 0; i < a.size(); ++i) {
cout << a[i] << " ";
}
cout << endl;
}
/**
* Erase the target element in the vector
* @tparam T, type
* @param vector, target vector
* @param value, the target element
*/
template<typename T>
void erase(vector<T> &vector, T const &value) {
vector.erase(remove(begin(vector), end(vector), value), end(vector));
}
//TODO Odd index means black!
int main() {
int passCheck = 0;
//Set the default values before the game starts
checkDuplicate.push_back("4d");
black[3][4] = 'b';
blackCoordinates.push_back("34");
checkDuplicate.push_back("4e");
black[4][3] = 'b';
blackCoordinates.push_back("43");
checkDuplicate.push_back("5d");
white[3][3] = 'w';
whiteCoordinates.push_back("33");
checkDuplicate.push_back("5e");
white[4][4] = 'w';
whiteCoordinates.push_back("44");
for (int i = 1; i <= 100 && !isGameOver() && !quit; i++) {
string user;
string computer;
string finalCoorComputer;
nextStep = generateValidCoordinates(i, black, white, blackCoordinates, whiteCoordinates);
displayBoard(black, white, nextStep);
displayAvailableCoordinates(nextStep);
cout << "Black: " << blackCoordinates.size() << " White: " << whiteCoordinates.size() << endl;
if (i % 2 != 0) {//Odd represents black player
//Check PASS
if (nextStep.size() == 0) {
cout << "PASS!" << endl;
passCheck++;
if (passCheck >= 2) {
displayBoard(black, white, nextStep);
cout << "Black: " << blackCoordinates.size() << " White: " << whiteCoordinates.size() << endl;
cout << "Gameover! The winner is: ";
whiteCoordinates.size() > blackCoordinates.size() ? cout << "WHITE." << endl : cout << "BLACK." << endl;
exit(0);
} else {
passCheck = 0;
continue;
}
}
cout << "Black's turn" << endl;
user = getUserInput();
int first = user[0] - '1'; //zong zuo biao
int second = user[1] - 'a'; // heng zuo biao
string coor = to_string(first) + to_string(second); //Coordinates in index's form
black[first][second] = 'b';
blackCoordinates.push_back(coor);
} else {//Even represents white player(Computer)
cout << "Press 'y' to let computer place its disc: ";
char y;
cin >> y;
//Check PASS
if (nextStep.size() == 0) {
cout << "PASS!" << endl;
passCheck++;
if (passCheck >= 2) {
displayBoard(black, white, nextStep);
cout << "Gameover! The winner is: ";
whiteCoordinates.size() > blackCoordinates.size() ? cout << "WHITE." << endl : cout << "BLACK." << endl;
exit(0);
} else {
passCheck = 0;
continue;
}
}
computer = computerMove();
char x = (char) (computer[0] + 1);
char y1 = (char) (computer[1] - '0' + 'a');
cout << computer << endl;
int x1 = computer[0] - '0';
int y2 = computer[1] - '0';
white[x1][y2] = 'w';
finalCoorComputer = string(1, x) + y1;
whiteCoordinates.push_back(computer);
cout << "Computer's choice: " << finalCoorComputer << endl;
}
if (i % 2 != 0) {
flip(user, i, black, white, blackCoordinates, whiteCoordinates);
} else {
flip(finalCoorComputer, 2, black, white, blackCoordinates, whiteCoordinates);
}
}
//When the game is finished
displayBoard(black, white, nextStep);
cout << "Black: " << blackCoordinates.size() << " White: " << whiteCoordinates.size() << endl;
cout << "Gameover! The winner is: ";
whiteCoordinates.size() > blackCoordinates.size() ? cout << "WHITE" << endl : cout << "BLACK" << endl;
return 0;
}
/**
* Generate computer's move
*/
string computerMove() {
double ratio = 0;
double previousRatio = 0;
string finalComputerCoordinate;
for (int i = 0; i < nextStep.size(); i++) {
//Copy their coordinates
vector<string> testBlackCoordinates;
vector<string> testWhiteCoordinates;
char testWhite[8][8];
char testBlack[8][8];
//Deep copy white and black array
for (int j = 0; j < width; j++) {
for (int x = 0; x < length; x++) {
testBlack[j][x] = black[j][x];
testWhite[j][x] = white[j][x];
}
}
for (int k = 0; k < blackCoordinates.size(); k++) {
testBlackCoordinates.push_back(blackCoordinates.at(k));
}
for (int l = 0; l < whiteCoordinates.size(); l++) {
testWhiteCoordinates.push_back(whiteCoordinates.at(l));
}
int first = nextStep.at(i)[0] - '0';
int second = nextStep.at(i)[1] - '0';
testWhite[first][second] = 'w';
char c1 = (char) (nextStep.at(i)[0] + 1);
char c2 = (char) (nextStep.at(i)[1] - '0' + 'a');
string realCoorForFlip = string(1, c1) + c2;
testWhiteCoordinates.push_back(nextStep.at(i));
int beforeFlip = testBlackCoordinates.size();
flip(realCoorForFlip, 2, testBlack, testWhite, testBlackCoordinates, testWhiteCoordinates);
vector<string> userAvailableStep = generateValidCoordinates(1, testBlack, testWhite, testBlackCoordinates,
testWhiteCoordinates);
bool userHasCorner = false;
for (int m = 0; m < userAvailableStep.size(); m++) {
int a1 = userAvailableStep.at(m)[0] - '0';
int a2 = userAvailableStep.at(m)[1] - '0';
userHasCorner = (a1 == 0 && a2 == 0) || (a1 == 0 && a2 == 7) || (a1 == 7 && a2 == 0) || (a1 == 7 && a2 == 7);
if(userHasCorner) break;
}
int afterFlipped = testBlackCoordinates.size();
double numberOfFlippedDis = beforeFlip - afterFlipped;
double userAvailable = userAvailableStep.size();
ratio = numberOfFlippedDis / userAvailable;
bool isCorner = (first == 0 && second == 0) || (first == 0 && second == 7) || (first == 7 && second == 0) || (first == 7 && second == 7);
bool isSide = (first == 0 && (second >= 0 && second <= 7)) || (first == 7 && (second >= 0 && second <= 7))
|| (second == 0 && (first >= 0 && first <= 7)) || (second == 7 && (first >= 0 && first <= 7));
if(isCorner){
ratio *= 100;
}else if(isSide && !isCorner){
ratio *= 20;
}else if(userHasCorner){
ratio /= 1000;
}
//Find the largest ratio
if (ratio > previousRatio) {
previousRatio = ratio;
//Store the final coordinate to the variable
finalComputerCoordinate = nextStep.at(i);
}
}//For loop bracket
return finalComputerCoordinate;
}
/**
* Check whether the game is over.
* @return true - the game is over || false - the game keeps going
*/
bool isGameOver() {
bool occuoied = (whiteCoordinates.size() + blackCoordinates.size() >= width * length);
bool noWhiteOrBlack = whiteCoordinates.size() == 0 || blackCoordinates.size() == 0;
return occuoied || noWhiteOrBlack;
}
/**
* Check whether the coordinate is validate
* @param key, coordinates
* @return true -> valid corrdinate, false -> invalid coordinate
*/
bool isValidKey(string &key) {
bool firstPart = key[0] >= '0' && key[0] <= '7';
bool secondPart = key[1] >= '0' && key[1] <= '7';
bool l = key.length() == 2;
return firstPart && secondPart && l;
}
/**
* Display the chess board.
* @param blackC, existing black coordinates
* @param whiteC, existing white coordinates
* @param nextCoor, available next-step coordinates
*/
void displayBoard(char blackC[][8], char whiteC[][8], vector<string> &nextCoor) {
//Display the alphabets
cout << " ";
for (int i = 0; i < width; i++) {
cout << char('a' + i) << " ";
}
cout << endl;
for (int i = 1; i <= width; i++) {
//Display the dashes
cout << " ";
for (int k = 0; k < width; k++) {
cout << "----";
}
cout << endl;
cout << i << " ";
for (int j = 1; j <= length; j++) {
//Display the disc
bool hasBlack = blackC[i - 1][j - 1] == 'b';
bool hasWhite = whiteC[i - 1][j - 1] == 'w';
string key = to_string(i - 1) + to_string(j - 1);
bool hasNextStep = count(nextCoor.begin(), nextCoor.end(), key);
if (hasBlack) {
cout << "| ● ";
} else if (hasWhite) {
cout << "| ◯ ";
} else if (hasNextStep) {
cout << "| ? ";
} else {
cout << "| ";//3spaces
}
if (j == length) {
cout << "|";
}
}
cout << endl;
//Display the bottom dashes
if (i == width) {
cout << " ";
for (int k = 0; k < width; k++) {
cout << "----";
}
cout << endl;
}
}
}
/**
* Display available coordinates.
* E.g: Your available coordinates are: 3c 3e 5c
* @param coor, next-step available coordinates
*/
void displayAvailableCoordinates(vector<string> &coor) {
cout << "Your available coordinates are: ";
for (int i = 0; i < coor.size(); i++) {
char a = char(coor[i][0] + 1);
char b = char(coor[i][1] - '0' + 'a');
cout << a << b << " ";
}
cout << endl;
}
/**
* Get user valid input. Also check whether user's input is valid
* or invalid, or wanna quit the game.
* @return user's valid coordinate
*/
string getUserInput() {
string user;
cout << "(Enter (quit) to exit the program)" << endl;
cout << "Please enter a coordinate: ";
bool valid = true;
while (valid) {
cin >> user;
if(user == "quit"){
quit = true;
break;
}
if (isdigit(user[0]) && isalpha(user[1])) {
//check whether the input is in the range
bool firstPart = user[0] >= '1' && user[0] <= '8';
bool secondPart = user[1] >= 'a' && user[1] <= 'h';
//Check the first and second index of user's input, and also check duplicate
if (firstPart && secondPart) {
//Put two chars into a string
char a = user[0] - 1;
char b = (char) ('0' + (user[1] - 'a'));
string k = string(1, a) + b;
//Check if it's an element of nextStep
if (count(nextStep.begin(), nextStep.end(), k)) {
checkDuplicate.push_back(k);
valid = false;
} else {
cout << "Invalid coordinate. ";
cout << "Please enter a coordinate: ";
continue;
}
} else {
cout << "Invalid coordinate. ";
cout << "Please enter a coordinate: ";
continue;
}
} else {
cout << "Invalid coordinate. ";
cout << "Please enter a coordinate: ";
}
}//while loop
return user;
}
/**
* Generate valid coordinates for next round.
* @param turns, odd means is black's term, even means is white's term
* @param blackC, existing black coordinates
* @param whiteC, existing white coordinates
* @param blackCoor, existing black coordinates (in the form of vector)
* @param whiteCoor, existing white coordinates (in the form of vector)
* @return a vector that holds all the available coordinates for next step
*/
vector<string> generateValidCoordinates(int turns, char blackC[][8], char whiteC[][8], vector<string> &blackCoor,
vector<string> &whiteCoor) {
vector<string> result;
if (turns % 2 != 0) {//generate black avaliable coordinates
//Search for the all the black disc
for (int i = 0; i < blackCoor.size(); i++) {
//Get the coordinates of existing black disc
int coor = stoi(blackCoor[i]);
int shu = coor / 10;
int heng = coor % 10;
int countW = 0;
//Searching horizontally -- East
for (int j = 1; j < width; j++) {
//If there's non-white disc next to a black, break!
if (whiteC[shu][heng + 1] != 'w') break;
//Otherwise(there's white disc next to it), search for empty space
else {
string key = to_string(shu) + to_string(heng + j);
if (whiteC[shu][heng + j] == 'w') countW++;
//When the place is empty
if (whiteC[shu][heng + j] != 'w' && blackC[shu][heng + j] != 'b') {
if (countW == j - 1 && !count(result.begin(), result.end(), key) && isValidKey(key)) {
result.push_back(key);
}
break;
}
}
}// H-East
countW = 0;
//Searching horizontally -- west
for (int j = 1; j < width; j++) {
//If there's non-white disc next to a black, break!
if (whiteC[shu][heng - 1] != 'w') break;
//Otherwise(there's white disc next to it), search for empty space
else {
string key = to_string(shu) + to_string(heng - j);
if (whiteC[shu][heng - j] == 'w') countW++;
//When the place is empty
if (whiteC[shu][heng - j] != 'w' && blackC[shu][heng - j] != 'b') {
if (countW == j - 1 && !count(result.begin(), result.end(), key) && isValidKey(key)) {
result.push_back(key);
}
break;
}
}
}// H-West
countW = 0;
//Searching vertically -- North
for (int j = 1; j < width; j++) {
//If there's non-white disc next to a black, break!
if (whiteC[shu - 1][heng] != 'w') { break; }
//Otherwise there's white disc next to it, search for empty space
else {
if (whiteC[shu - j][heng] == 'w') countW++;
string key = to_string(shu - j) + to_string(heng);
if (whiteC[shu - j][heng] != 'w' && blackC[shu - j][heng] != 'b') {
if (countW == j - 1 && !count(result.begin(), result.end(), key) && isValidKey(key)) {
result.push_back(key);
}
break;
}
}
}// V-North
countW = 0;
//Searching vertically -- South
for (int j = 1; j < length; j++) {
//If there's non-white disc next to a black, break!
if (whiteC[shu + 1][heng] != 'w') { break; }
//Otherwise(there's white disc next to it, search for empty space
else {
if (whiteC[shu + j][heng] == 'w') countW++;
string key = to_string(shu + j) + to_string(heng);
if (whiteC[shu + j][heng] != 'w' && blackC[shu + j][heng] != 'b') {
if (countW == (j - 1) && !count(result.begin(), result.end(), key) && isValidKey(key)) {
result.push_back(key);
}
break;
}
}
}// V-South
countW = 0;
//Searching diagonally-- North West
for (int j = 1; j < width; j++) {
//If there's non-white disc next to a black, break!
if (whiteC[shu - 1][heng - 1] != 'w') { break; }
//Otherwise there's white disc next to it, search for empty space
else {
if (whiteC[shu - j][heng - j] == 'w') countW++;
string key = to_string(shu - j) + to_string(heng - j);
if (whiteC[shu - j][heng - j] != 'w' && blackC[shu - j][heng - j] != 'b') {
if (countW == j - 1 && !count(result.begin(), result.end(), key) && isValidKey(key)) {
result.push_back(key);
}
break;
}
}
}// North-West
countW = 0;
//Searching diagonally-- North East
for (int j = 1; j < width; j++) {
//If there's non-white disc next to a black, break!
if (whiteC[shu - 1][heng + 1] != 'w') { break; }
//Otherwise there's white disc next to it, search for empty space
else {
if (whiteC[shu - j][heng + j] == 'w') countW++;
string key = to_string(shu - j) + to_string(heng + j);
if (whiteC[shu - j][heng + j] != 'w' && blackC[shu - j][heng + j] != 'b') {
if (countW == j - 1 && !count(result.begin(), result.end(), key) && isValidKey(key)) {
result.push_back(key);
}
break;
}
}
}// North-East
countW = 0;
//Searching diagonally -- South West
for (int j = 1; j < width; j++) {
//If there's non-white disc next to a black, break!
if (whiteC[shu + 1][heng - 1] != 'w') { break; }
//Otherwise there's white disc next to it, search for empty space
else {
if (whiteC[shu + j][heng - j] == 'w') countW++;
string key = to_string(shu + j) + to_string(heng - j);
if (whiteC[shu + j][heng - j] != 'w' && blackC[shu + j][heng - j] != 'b') {
if (countW == j - 1 && !count(result.begin(), result.end(), key) && isValidKey(key)) {
result.push_back(key);
}
break;
}
}
}// South-West
countW = 0;
//Searching diagonally -- South East
for (int j = 1; j < width; j++) {
//If there's non-white disc next to a black, break!
if (whiteC[shu + 1][heng + 1] != 'w') { break; }
//Otherwise there's white disc next to it, search for empty space
else {
if (whiteC[shu + j][heng + j] == 'w') countW++;
string key = to_string(shu + j) + to_string(heng + j);
if (whiteC[shu + j][heng + j] != 'w' && blackC[shu + j][heng + j] != 'b') {
if (countW == j - 1 && !count(result.begin(), result.end(), key) && isValidKey(key)) {
result.push_back(key);
}
break;
}
}
}// South-East
countW = 0;
}
} else {//Search for white disc
// Mechanisms are mostly same as the previous if block
//Search for the all the white disc
for (int i = 0; i < whiteCoor.size(); i++) {
//Get the coordinates of existing black disc
int coor = stoi(whiteCoor[i]);
int shu = coor / 10;
int heng = coor % 10;
int countB = 0;
//Searching horizontally -- to right
for (int j = 1; j < width; j++) {
//If there's non-white disc next to a black, break!
if (blackC[shu][heng + 1] != 'b') break;
//Otherwise(there's white disc next to it), search for empty space
else {
string key = to_string(shu) + to_string(heng + j);
if (blackC[shu][heng + j] == 'b') countB++;
//When the place is empty
if (blackC[shu][heng + j] != 'b' && whiteC[shu][heng + j] != 'w') {
if (countB == j - 1 && !count(result.begin(), result.end(), key) && isValidKey(key)) {
result.push_back(key);
}
break;
}
}
}// H-Right
countB = 0;
//Searching horizontally -- to left
for (int j = 1; j < width; j++) {
//If there's non-white disc next to a black, break!
if (blackC[shu][heng - 1] != 'b') break;
//Otherwise(there's white disc next to it), search for empty space
else {
string key = to_string(shu) + to_string(heng - j);
if (blackC[shu][heng - j] == 'b') countB++;
//When the place is empty
if (blackC[shu][heng - j] != 'b' && whiteC[shu][heng - j] != 'w') {
if (countB == j - 1 && !count(result.begin(), result.end(), key) && isValidKey(key)) {
result.push_back(key);
}
break;
}
}
}// H-Left
countB = 0;
//Searching vertically -- North
for (int j = 1; j < width; j++) {
//If there's non-white disc next to a black, break!
if (blackC[shu - 1][heng] != 'b') { break; }
//Otherwise there's white disc next to it, search for empty space
else {
if (blackC[shu - j][heng] == 'b') countB++;
string key = to_string(shu - j) + to_string(heng);
if (blackC[shu - j][heng] != 'b' && whiteC[shu - j][heng] != 'w') {
if (countB == j - 1 && !count(result.begin(), result.end(), key) && isValidKey(key)) {
result.push_back(key);
}
break;
}
}
}// H-Right
countB = 0;
//Searching vertically -- South
for (int j = 1; j < length; j++) {
//If there's non-white disc next to a black, break!
if (blackC[shu + 1][heng] != 'b') { break; }
//Otherwise(there's white disc next to it, search for empty space
else {
if (blackC[shu + j][heng] == 'b') countB++;
string key = to_string(shu + j) + to_string(heng);
if (blackC[shu + j][heng] != 'b' && whiteC[shu + j][heng] != 'w') {
if (countB == j - 1 && !count(result.begin(), result.end(), key) && isValidKey(key)) {
result.push_back(key);
}
break;
}
}
}// South
countB = 0;
//Searching diagonally-- North West
for (int j = 1; j < width; j++) {
//If there's non-white disc next to a black, break!
if (blackC[shu - 1][heng - 1] != 'b') { break; }
//Otherwise there's white disc next to it, search for empty space
else {
if (blackC[shu - j][heng - j] == 'b') countB++;
string key = to_string(shu - j) + to_string(heng - j);
if (blackC[shu - j][heng - j] != 'b' && whiteC[shu - j][heng - j] != 'w') {
if (countB == j - 1 && !count(result.begin(), result.end(), key) && isValidKey(key)) {
result.push_back(key);
}
break;
}
}
}// North-West
countB = 0;
//Searching diagonally-- North East
for (int j = 1; j < width; j++) {
//If there's non-white disc next to a black, break!
if (blackC[shu - 1][heng + 1] != 'b') { break; }
//Otherwise there's white disc next to it, search for empty space
else {
if (blackC[shu - j][heng + j] == 'b') countB++;
string key = to_string(shu - j) + to_string(heng + j);
if (blackC[shu - j][heng + j] != 'b' && whiteC[shu - j][heng + j] != 'w') {
if (countB == j - 1 && !count(result.begin(), result.end(), key) && isValidKey(key)) {
result.push_back(key);
}
break;
}
}
}// North-East
countB = 0;
//Searching diagonally-- South West
for (int j = 1; j < width; j++) {
//If there's non-white disc next to a black, break!
if (blackC[shu + 1][heng - 1] != 'b') { break; }
//Otherwise there's white disc next to it, search for empty space
else {
if (blackC[shu + j][heng - j] == 'b') countB++;
string key = to_string(shu + j) + to_string(heng - j);
if (blackC[shu + j][heng - j] != 'b' && whiteC[shu + j][heng - j] != 'w') {
if (countB == j - 1 && !count(result.begin(), result.end(), key) && isValidKey(key)) {
result.push_back(key);
}
break;
}
}
}// South-West
countB = 0;
//Searching diagonally-- South East
for (int j = 1; j < width; j++) {
//If there's non-white disc next to a black, break!
if (blackC[shu + 1][heng + 1] != 'b') { break; }
//Otherwise there's white disc next to it, search for empty space
else {
if (blackC[shu + j][heng + j] == 'b') countB++;
string key = to_string(shu + j) + to_string(heng + j);
if (blackC[shu + j][heng + j] != 'b' && whiteC[shu + j][heng + j] != 'w') {
if (countB == j - 1 && !count(result.begin(), result.end(), key) && isValidKey(key)) {
result.push_back(key);
}
break;
}
}
}// South-East
countB = 0;
}//outer for loop
}//else block
//Filter out invalid coordinates
for (int i = 0; i < result.size(); i++) {
string &element = result.at(i);
bool firstPart = element[0] >= '0' && element[0] <= '7';
bool secondPart = element[1] >= '0' && element[1] <= '7';
if (!firstPart || !secondPart) {
erase(result, element);
}
}
//sort the vector
sort(result.begin(), result.end());
return result;
}
/**
* According to whoes term, flip opponent's disc.
* @param s, user's or computer's coordinates
* @param order, odd means is black term, even means is white's term
* @param blackC existing black coordinates
* @param whiteC, existing white coordinates
* @param blackCoor, existing black coordinates (in the form of vector)
* @param whiteCoor, existing white coordinates (in the form of vector)
*/
void flip(string &s, int order, char blackC[][8], char whiteC[][8], vector<string> &blackCoor, vector<string> &whiteCoor) {
int shu = s[0] - '1'; //zong zuo biao
int heng = s[1] - 'a'; // heng zuo biao
if (order % 2 != 0) {//ODD flip the white disc
//Searching horizontally -- to right
for (int i = 1; i < width && ((heng + i) <= 7); i++) {
if (blackC[shu][heng + i] == 'b') {
//Flip the white disc
for (int j = 1; j <= i; j++) {
whiteC[shu][heng + j] = '\0';
blackC[shu][heng + j] = 'b';
//Update the vector
string coordinateInString = to_string(shu) + to_string(heng + j);
if (!count(blackCoor.begin(), blackCoor.end(), coordinateInString) &&
isValidKey(coordinateInString)) {
blackCoor.push_back(coordinateInString);
erase(whiteCoor, coordinateInString);
}
}
break;
} else if (whiteC[shu][heng + i] == 'w') continue;
else break;
}// H-Right
//H-Left
for (int i = 1; i < width && ((heng - i) >= 0); i++) {
if (blackC[shu][heng - i] == 'b') {
//Flip the white disc
for (int j = 1; j <= i; j++) {
whiteC[shu][heng - j] = '\0';
blackC[shu][heng - j] = 'b';
string coordinateInString = to_string(shu) + to_string(heng - j);
if (!count(blackCoor.begin(), blackCoor.end(), coordinateInString) &&
isValidKey(coordinateInString)) {
blackCoor.push_back(coordinateInString);
erase(whiteCoor, coordinateInString);
}
}
break;
} else if (whiteC[shu][heng - i] == 'w') continue;
else break;
}
//Verticle Upward
for (int i = 1; i < width && ((shu - i) >= 0); i++) {
if (blackC[shu - i][heng] == 'b') {
//Flip the white disc
for (int j = 1; j <= i; j++) {
whiteC[shu - j][heng] = '\0';
blackC[shu - j][heng] = 'b';
string coordinateInString = to_string(shu - j) + to_string(heng);
if (!count(blackCoor.begin(), blackCoor.end(), coordinateInString) &&
isValidKey(coordinateInString)) {
blackCoor.push_back(coordinateInString);
erase(whiteCoor, coordinateInString);
}
}
break;
} else if (whiteC[shu - i][heng] == 'w') continue;
else break;
}
//Vertical Downward
for (int i = 1; i < width && ((shu + i) <= 7); i++) {
if (blackC[shu + i][heng] == 'b') {
//Flip the white disc
for (int j = 1; j <= i; j++) {
whiteC[shu + j][heng] = '\0';
blackC[shu + j][heng] = 'b';
string coordinateInString = to_string(shu + j) + to_string(heng);
if (!count(blackCoor.begin(), blackCoor.end(), coordinateInString) &&
isValidKey(coordinateInString)) {
blackCoor.push_back(coordinateInString);
erase(whiteCoor, coordinateInString);
}
}
break;
} else if (whiteC[shu + i][heng] == 'w') continue;
else break;
}
//Diagnoal Direction -> South East
for (int i = 1; i < width && ((shu + i) <= 7) && ((heng + i) <= 7); i++) {
if (blackC[shu + i][heng + i] == 'b') {
//Flip the white disc
for (int j = 1; j <= i; j++) {
whiteC[shu + j][heng + j] = '\0';
blackC[shu + j][heng + j] = 'b';
string coordinateInString = to_string(shu + j) + to_string(heng + j);
if (!count(blackCoor.begin(), blackCoor.end(), coordinateInString) &&
isValidKey(coordinateInString)) {
blackCoor.push_back(coordinateInString);
erase(whiteCoor, coordinateInString);
}
}
break;
} else if (whiteC[shu + i][heng + i] == 'w') continue;
else break;
}
//Diagnoal Direction -> North West
for (int i = 1; i < width && ((shu - i) >= 0) && ((heng - i) >= 0); i++) {
if (blackC[shu - i][heng - i] == 'b') {
//Flip the white disc
for (int j = 0; j < i; j++) {
whiteC[shu - j][heng - j] = '\0';
blackC[shu - j][heng - j] = 'b';
string coordinateInString = to_string(shu - j) + to_string(heng - j);
if (!count(blackCoor.begin(), blackCoor.end(), coordinateInString) &&
isValidKey(coordinateInString)) {
blackCoor.push_back(coordinateInString);
erase(whiteCoor, coordinateInString);
}
}
break;
} else if (whiteC[shu - i][heng - i] == 'w') continue;
else break;
}
//Diagnoal Direction -> North East
for (int i = 1; i < width && ((shu - i) >= 0) && ((heng + i) <= 7); i++) {
if (blackC[shu - i][heng + i] == 'b') {
//Flip the white disc
for (int j = 0; j < i; j++) {
whiteC[shu - j][heng + j] = '\0';
blackC[shu - j][heng + j] = 'b';
string coordinateInString = to_string(shu - j) + to_string(heng + j);
if (!count(blackCoor.begin(), blackCoor.end(), coordinateInString) &&
isValidKey(coordinateInString)) {
blackCoor.push_back(coordinateInString);
erase(whiteCoor, coordinateInString);
}
}
break;
} else if (whiteC[shu - i][heng + i] == 'w') continue;
else break;
}
//Diagnoal Direction -> South West
for (int i = 1; i < width && ((shu + i) <= 7) && ((heng - i) >= 0); i++) {
if (blackC[shu + i][heng - i] == 'b') {
//Flip the white disc
for (int j = 0; j < i; j++) {
whiteC[shu + j][heng - j] = '\0';
blackC[shu + j][heng - j] = 'b';
string coordinateInString = to_string(shu + j) + to_string(heng - j);
if (!count(blackCoor.begin(), blackCoor.end(), coordinateInString) &&
isValidKey(coordinateInString)) {
blackCoor.push_back(coordinateInString);
erase(whiteCoor, coordinateInString);
}
}
break;
} else if (whiteC[shu + i][heng - i] == 'w') continue;
else break;
}
} else {//flip the black disc
//H-right
for (int i = 1; i < width && ((heng + i) <= 7); i++) {
if (whiteC[shu][heng + i] == 'w') {
//Flip the white disc
for (int j = 1; j <= i; j++) {
blackC[shu][heng + j] = '\0';
whiteC[shu][heng + j] = 'w';
string coordinateInString = to_string(shu) + to_string(heng + j);
if (!count(whiteCoor.begin(), whiteCoor.end(), coordinateInString) &&
isValidKey(coordinateInString)) {
whiteCoor.push_back(coordinateInString);
erase(blackCoor, coordinateInString);
}
}
break;
} else if (blackC[shu][heng + i] == 'b') continue;
else break;
}// H-Right
//H-Left
for (int i = 1; i < width && ((heng - i) >= 0); i++) {
if (whiteC[shu][heng - i] == 'w') {
//Flip the white disc
for (int j = 1; j <= i; j++) {
blackC[shu][heng - j] = '\0';
whiteC[shu][heng - j] = 'w';
string coordinateInString = to_string(shu) + to_string(heng - j);
if (!count(whiteCoor.begin(), whiteCoor.end(), coordinateInString) &&
isValidKey(coordinateInString)) {
whiteCoor.push_back(coordinateInString);
erase(blackCoor, coordinateInString);
}
}
break;
} else if (blackC[shu][heng - i] == 'b') continue;
else break;
}
//Vertical Upward
for (int i = 1; i < width && ((shu - i) >= 0); i++) {
if (whiteC[shu - i][heng] == 'w') {
//Flip the white disc
for (int j = 1; j <= i; j++) {
blackC[shu - j][heng] = '\0';
whiteC[shu - j][heng] = 'w';
string coordinateInString = to_string(shu - j) + to_string(heng);
if (!count(whiteCoor.begin(), whiteCoor.end(), coordinateInString) &&
isValidKey(coordinateInString)) {
whiteCoor.push_back(coordinateInString);
erase(blackCoor, coordinateInString);
}
}
break;
} else if (blackC[shu - i][heng] == 'b') continue;
else break;
}
//Vertical Downward
for (int i = 1; i < width && ((shu + i) <= 7); i++) {
if (whiteC[shu + i][heng] == 'w') {
//Flip the white disc
for (int j = 1; j <= i; j++) {
blackC[shu + j][heng] = '\0';
whiteC[shu + j][heng] = 'w';
string coordinateInString = to_string(shu + j) + to_string(heng);
if (!count(whiteCoor.begin(), whiteCoor.end(), coordinateInString) &&
isValidKey(coordinateInString)) {
whiteCoor.push_back(coordinateInString);
erase(blackCoor, coordinateInString);
}
}
break;
} else if (blackC[shu + i][heng] == 'b') continue;
else break;