-
Notifications
You must be signed in to change notification settings - Fork 0
/
Graph.cpp
1493 lines (1307 loc) · 46.6 KB
/
Graph.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
//#include <vector>
#include <utility>
#include <set>
#include <map>
#include <unordered_set>
#include "Graph.h"
#include <iostream>
#include <cmath>
#include <sstream>
#include <string>
#include <queue>
#include <iomanip>
#include <tuple>
#include <bits/stdc++.h>
#include <climits>
using namespace std;
stateSpace::stateSpace() {
}
stateSpace::stateSpace(State& start) {
start_ = &start;
isInit = false;
nodeList = NULL;
edgeList = NULL;
}
stateSpace::~stateSpace() {
//delete start_;
if (isInit) {
delete nodeList;
delete edgeList;
}
// deleteHelper(start_);
// delete nodeList;
// delete edgeList;
}
void stateSpace::initi() {
isInit = true;
nodeList = new map<pair<int, int>, State*>;
edgeList = new map<pair<int, int>, int>;
}
void stateSpace::drawBoard(int code) {
int** a = getBoard(code);
cout <<" " << "0 1 2"<<endl<<endl;
cout <<"0) " << drawP(a[0][0]) << " " << drawP(a[0][1]) << " "<< drawP(a[0][2]) << " " << endl;
cout << "1) "<<drawP(a[1][0]) << " " <<drawP(a[1][1]) << " "<< drawP(a[1][2]) << " " << endl;
cout << "2) "<<drawP(a[2][0]) << " " << drawP(a[2][1]) << " "<<drawP( a[2][2]) << " " << endl;
deleteBroad(a);
}
string stateSpace::drawP(int num) {
if (num == 1) {
return "O";
} else if (num == 0) {
return "-";
} else {
return "X";
}
}
void stateSpace::deleteHelper(State* s) {
if (!(s->getNext())->empty()) {
for (State* n : *(s->getNext())) {
deleteHelper(n);
}
}
delete s;
}
map<pair<int,int>, State*> * stateSpace::getNodeList() {
return nodeList;
}
map<pair<int, int>, int> * stateSpace::geteEdgeList() {
return edgeList;
}
State * stateSpace::getStart() {
return start_;
}
int stateSpace::toInt(string str) {
int num;
stringstream ss;
ss << str;
ss >> num;
return num;
}
std::string stateSpace::toString(int value) {
std::ostringstream os;
os << value;
return os.str();
}
std::string stateSpace::toString9(int value) { //保证返回来的是9位数的string,是001002001,而不是1002001!
std::ostringstream os;
os << setw(9) << setfill('0') << value << setw(9) << setfill('0');
return os.str();
}
int stateSpace::DecToTer(int n) {
//std::string str_ret;
if (n==0) return 0;
if (n<0) n = 0 - n;
int ret=0;
int mul=1;
while(n>0)
{
int digitofternary=n%3;
n/=3;
ret += mul * digitofternary;
mul *= 10;
//std::string str= stateSpace::toString(digitofternary);
//str_ret.append(str);
}
return ret;
//reverse(str_ret.begin(), str_ret.end());
//cout << str_ret << endl;
// return stateSpace::toInt(str_ret);
}
int stateSpace::TerToDec(int s) {
int multiplier=1, result=0;
if (s == 0) {
return 0;
}
if (s<0) s = 0 - s;
while(s>0)
{
result=result+s%10*multiplier;
multiplier=multiplier*3;
s=s/10;
}
return result;
}
int** stateSpace::getBoard(int code) { //十进制转三进制再转board//每一次getBoard请务必记得delete。
int** a = new int*[3];
for (int i = 0; i < 3; ++i) {
a[i] = new int[3];
}
int one = DecToTer(code);
string codeStr = toString9(one);
for (int j = 0; j < 3; j++) {
for (int k = 0; k < 3; k++) {
a[j][k] = toInt(codeStr.substr(0,1));
codeStr = codeStr.substr(1);
}
}
return a;
}
void stateSpace::deleteBroad(int** a) {
for (int i = 0; i < 3; i++) {
delete[] a[i];
}
delete[] a;
}
bool stateSpace::intheBound(int x_Coor, int y_Coor) {
if (x_Coor < 0 || x_Coor > 2 ||y_Coor < 0 || y_Coor > 2) {
return false;
}
return true;
}
std::set<pair<int, int> > * stateSpace::allValidMoveNode(int code, int color) { //调用后一定要delete。
set<pair<int, int> >* theSet = new set<pair<int, int> >;
int** a = stateSpace::getBoard(code);
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
if (a[i][j] == color) {
theSet->insert(make_pair(i, j));
}
}
}
stateSpace::deleteBroad(a);
return theSet;
}
std::set<pair<int, int> >* stateSpace::allEmptyPosition(int code) {
set<pair<int, int> > * emptyPos = new set<pair<int, int> >;
int** a = getBoard(code);
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
if (a[i][j] == 0) {
emptyPos->insert(make_pair(i, j));
}
}
}
deleteBroad(a);
return emptyPos;
}
bool stateSpace::isMovePhase(int code) {
int count = 0;
int terCode = stateSpace::DecToTer(code);
string codeStr = stateSpace::toString9(terCode);
for (std::string::size_type i = 0; i < codeStr.size(); ++i) {
if (codeStr[i] == '0') {
count++;
}
}
if (count == 3) {
return true;
} else {
return false;
}
}
bool stateSpace::checkPositionValid(int code, int x_Coor, int y_Coor) {
if (!intheBound(x_Coor, y_Coor)) {
//std::cout << "invalid position - out of bound" << endl;
return false;
}
int terCode = stateSpace::DecToTer(code);
string codeStr = stateSpace::toString9(terCode);
if (codeStr.at(x_Coor * 3 + y_Coor) != '0') {
//std::cout << codeStr.at(x_Coor * 3 + y_Coor) << endl;
return false;
}
return true;
}
std::set<pair<int, int> > * stateSpace::validNextMove(int code, int x, int y) {
set<pair<int, int> > * neighbors = new set<pair<int, int> >;
if (((x == 1 && y == 1) || (x == 2 && y == 2)) && checkPositionValid(code, x - 1, y - 1)) {
neighbors->insert( pair<int,int>(x - 1, y - 1));
}
if (checkPositionValid(code, x - 1, y)) {
neighbors->insert(pair<int,int>(x - 1, y));
}
if (((x == 1 && y == 1) || (x == 2 && y == 0)) && checkPositionValid(code, x - 1, y + 1)) {
neighbors->insert( pair<int,int>(x - 1, y + 1));
}
if (checkPositionValid(code, x, y - 1)) {
neighbors->insert( pair<int,int>(x, y - 1));
}
if (checkPositionValid(code, x, y + 1)) {
neighbors->insert( pair<int,int>(x, y + 1));
}
if (((x == 1 && y == 1) || (x == 0 && y == 2)) && checkPositionValid(code, x + 1, y - 1)) {
neighbors->insert( pair<int,int>(x + 1, y - 1));
}
if (checkPositionValid(code, x + 1, y)) {
neighbors->insert( pair<int,int>(x + 1, y));
}
if (((x == 1 && y == 1) || (x == 0 && y == 0)) && checkPositionValid(code, x + 1, y + 1)) {
neighbors->insert( pair<int,int>(x + 1, y + 1));
}
return neighbors;
}
int stateSpace::checkEndGame(int code) {
int** _board = stateSpace::getBoard(code);
int _endGame = 0;
for (int i = 0; i < 3; i++) {
int y0 = _board[i][0];
int y1 = _board[i][1];
int y2 = _board[i][2];
if (y0 == y1 && y1 == y2 && y0 != 0) {
//_endGame = y0;
if (y0 == 1) _endGame = 1;
if (y0 == 2) _endGame = -1;
deleteBroad(_board);
return _endGame;
}
int x0 = _board[0][i];
int x1 = _board[1][i];
int x2 = _board[2][i];
if (x0 == x1 && x1 == x2 && x0 != 0) {
//_endGame = x0;
if (x0 == 1) _endGame = 1;
if (x0 == 2) _endGame = -1;
deleteBroad(_board);
return _endGame;
}
}
// check cross
int c00 = _board[0][0];
int c22 = _board[2][2];
int c11 = _board[1][1];
int c20 = _board[2][0];
int c02 = _board[0][2];
if (c00 == c11 && c11 == c22 && c00 != 0) {
if (c00 == 1) _endGame = 1;
if (c00 == 2) _endGame = -1;
deleteBroad(_board);
//_endGame = c00;
return _endGame;
}
if (c20 == c11 && c11 == c02 && c20 != 0) {
//_endGame = c20;
if (c20 == 1) _endGame = 1;
if (c20 == 2) _endGame = -1;
deleteBroad(_board);
return c20;
}
deleteBroad(_board);
return 0;
}
int stateSpace::addOneStepADD(int code, int x_Coor, int y_Coor, int color) {
if (x_Coor < 0 || x_Coor > 2 ||y_Coor < 0 || y_Coor > 2) {
//std::cout << "invalid position - out of bound" << endl;
return code;
}
string newCodeStr;
if (stateSpace::checkEndGame(code) == 0) {
int tre = stateSpace::DecToTer(code);
string codeStr = stateSpace::toString9(tre);
int position = x_Coor *3 + y_Coor;
if (codeStr.at(position) == '0') {
newCodeStr.append(codeStr.substr(0, position));
newCodeStr.append(toString(color));
newCodeStr.append(codeStr.substr(position+1));
} else {
//std::cout << "invalid postion - already have node here" << std::endl;
return code;
}
} else {
//std::cout << "Game already over!" << std::endl;
return code;
}
return stateSpace::TerToDec(stateSpace::toInt(newCodeStr));
}
int stateSpace::addOneStepMOVE(int code, pair<int, int> from, pair<int, int> to) {
int tre = stateSpace::DecToTer(code);
string codeStr = stateSpace::toString9(tre);
int position = from.first *3 + from.second;
string thecolor;
thecolor.push_back(codeStr.at(position));
int color = toInt(thecolor);
string newCodeStr;
newCodeStr.append(codeStr.substr(0, position));
newCodeStr.append("0");
newCodeStr.append(codeStr.substr(position+1));
int newcode = stateSpace::TerToDec(toInt(newCodeStr));
return stateSpace::addOneStepADD(newcode, to.first, to.second, color);
}
std::set<int> * stateSpace::getNextSteps(int code, int color) {
set<int> * nextStep = new set<int>; //toreturn
if (isMovePhase(code)){ //移棋状态
set<pair<int, int> > * moveNode = stateSpace::allValidMoveNode(code, color); //找出所有可能挪动的棋子(全部黑棋,全部白棋)
for (std::set<pair<int, int> >::iterator it=moveNode->begin(); it!=moveNode->end(); ++it) {//对于每一个可能挪动棋子
pair<int, int> from = make_pair(it->first, it->second);
std::set<pair<int, int> > * validNextPoint = stateSpace::validNextMove(code, it->first, it->second);//找出每个可能挪动棋子的下一步可能移动坐标
//生成可能状态并储存
for (std::set<pair<int, int> >::iterator it2=validNextPoint->begin(); it2!=validNextPoint->end(); ++it2) {
nextStep->insert(addOneStepMOVE(code, from, make_pair(it2->first, it2->second)));
}
delete validNextPoint;
}
delete moveNode;
} else { //下棋状态
set<pair<int, int> > * addNode = stateSpace::allEmptyPosition(code); //找出所有可能下棋的位置(全部空白)
//生成可能状态并储存
for (std::set<pair<int, int> >::iterator it=addNode->begin(); it!=addNode->end(); ++it) { //有没有可能在这一步就避免对称呢? 没有
nextStep->insert(stateSpace::addOneStepADD(code, it->first, it->second, color));
}
delete addNode;
}
return nextStep;
}
void stateSpace::putStart(State * s) {
start_ = s;
}
void stateSpace::insertNode(pair<int,int> node, State * s) {
nodeList->insert( pair<pair<int, int>, State*> (node, s) );
}
void stateSpace::insertEdge(pair<int,int> edge, int color) {
(*edgeList)[edge] = color;
}
void stateSpace::growFrom2(stateSpace& space, pair<int, int> start) {
/*
stateSpace空的nodelist
在nodelist里面放入-9841(1赢),-19682(2赢)这两种状态
stateSpace空的edgelist
给定空的queue
1. 给定一个状态放queue
2. while
取第一个
get所有nextstep
对于这些nextstep
如果1赢:生成新的edge,放入edgelist,(不放入新node,不加入queue)
如果2赢:生成新的edge,放入edgelist,
如果nodeset里面有这个step,
生成新的edge,放入edgelist,生成对应state(不放入新node,不加入queue)
否则如果没有这个nodestep
将本step加入nodeset,生成新的edge,放入edgelist,生成对应state,加入queue
结果:private variable被initialize. nodelist,edgelist
*/
<<<<<<< HEAD
=======
>>>>>>> 043f995019bdce0780c18358b5680a0a3961cb7b
space.initi();
State * Bwin = new State(-9481, 1); //?
State * Wwin = new State(-19682, 2);
space.insertNode(pair<int,int>(-9841,0), Bwin);
space.insertNode(pair<int,int>(-19682,0), Wwin);
std::queue<State * > q;
if (checkEndGame(start.first) == 0) {
State * st = new State(start.first, start.second);
space.putStart(st);
q.push(st);
space.insertNode(pair<int,int>(start.first,start.second),st);
}
// int i = 0; //change i in the while loop to change num of steps
while (! q.empty()) { //&& i < 2000) {//&& i<6
State * s = q.front();
q.pop();
int nextColor = 0;
if (s->getColor() == 1) {
nextColor = 2;
} else {
nextColor = 1;
}
std::set<int> * possibleSet = stateSpace::getNextSteps(s->getCode(), nextColor);
for (std::set<int>::iterator it=possibleSet->begin(); it!=possibleSet->end(); ++it) {
//State * stNext = new State(*it, nextColor);
//s->putNext(stNext);
if (checkEndGame(*it) == 1) {
State * stNext = new State(*it, nextColor);
stNext->putNext(Bwin);
s->putNext(stNext);
Bwin->increNumOfParents();
stNext->increNumOfParents();
pair<int, int> transit(s->getCode(), *it);
space.insertEdge(transit, nextColor);
pair<int, int> transit2(s->getCode(), -9841);
space.insertEdge(transit2, nextColor);
} else if (checkEndGame(*it) == 2) {
State * stNext = new State(*it, nextColor);
stNext->putNext(Wwin);
s->putNext(stNext);
Wwin->increNumOfParents();
stNext->increNumOfParents();
pair<int, int> transit(s->getCode(), *it);
space.insertEdge(transit, nextColor);
pair<int, int> transit2(s->getCode(), -19682);
space.insertEdge(transit2, nextColor);
} else {
std::map<pair<int,int>, State* >::iterator it2 = (space.getNodeList())->find( pair<int,int>(*it,nextColor) );
if (it2 != (space.getNodeList())->end()) { //在next中
pair<int, int> transit(s->getCode(), *it);
space.insertEdge(transit, nextColor);
s->putNext(it2->second);
(it2->second)->increNumOfParents();
//delete stNext;
} else {
State * stNext = new State(*it, nextColor);
space.insertNode(pair<int,int>(*it,nextColor),stNext);
pair<int, int> transit(s->getCode(), *it);
space.insertEdge(transit, nextColor);
//pair<int, int> state(*it, nextColor); //需不需要用new?
//State* n = stNext;
s->putNext(stNext);
stNext->increNumOfParents();
q.push(stNext);
}
}
}
delete possibleSet;
}
// for (pair<pair<int,int>, State* > node : *space.nodeList) {
// for (pair<pair<int,int>,int> edge : *space.edgeList) {
// if (edge.first.first == node.first.first) {
// for (pair<pair<int,int>,State* > node2 : *space.nodeList) {
// if (node2.first.first == edge.first.second) {
// node.second->putNext(node2.second);
// }
// }
// }
// }
// }
}
int stateSpace::bestMove(int code, int color) { //放入地方造成的棋盘
int mycolor = 0;
if (color == 1) {
mycolor = 2;
} else {
mycolor = 1;
}
if (!isMovePhase(code)) {
//cout << "check phase " << code << " with " << color << endl;
pair<int, int> possAddCritical = isAddCritical(code, color);
//out << possAddCritical.first << " and " << possAddCritical.second << endl;
if (possAddCritical.first != -1) {
return addOneStepADD(code, possAddCritical.first, possAddCritical.second, mycolor);
}
}
pair<int, int> bestmove = pair<int, int>(-1,-10000);
std::set<int> * possibleSet = stateSpace::getNextSteps(code, mycolor);
for (std::set<int>::iterator it=possibleSet->begin(); it!=possibleSet->end(); ++it) {
int move = minimax(pair<int, int>(*it, mycolor), mycolor, 7, -10000, 10000);
if (move > bestmove.second) {
bestmove.first = *it;
bestmove.second = move;
}
}
return bestmove.first;
}
pair<int, int> stateSpace::isAddCritical(int code, int color) {
int** _board = stateSpace::getBoard(code);
int other = 0;
if (color == 1) {
other = 2;
} else {
other = 1;
}
pair< pair<int, int>, int> numEmpty = pair< pair<int, int>, int>(pair<int, int>(-1,-1),0);
//vertical
for (int i = 0; i < 3; i++) {
numEmpty = pair< pair<int, int>, int>(pair<int, int>(-1,-1),0);
int num1 = 0;
int num2 = 0;
for (int j = 0; j < 3; j++) {
if (_board[j][i] == 0) {
numEmpty.first = pair<int, int>(j,i);
numEmpty.second += 1;
} else if (_board[j][i] == color) {
num1++;
} else {
num2++;
}
}
if (num1 == 2 && numEmpty.second == 1) {
deleteBroad(_board);
return numEmpty.first;
}
}
//horizontal check
for (int i = 0; i < 3; i++) {
numEmpty = pair< pair<int, int>, int>(pair<int, int>(-1,-1),0);
int num1 = 0;
int num2 = 0;
for (int j = 0; j < 3; j++) {
if (_board[i][j] == 0) {
numEmpty.first = pair<int, int>(i,j);
numEmpty.second += 1;
} else if (_board[i][j] == color) {
num1++;
} else {
num2++;
}
}
if (num1 == 2 && numEmpty.second == 1) {
deleteBroad(_board);
return numEmpty.first;
}
}
//diag
numEmpty = pair< pair<int, int>, int>(pair<int, int>(-1,-1),0);
int num1 = 0;
int num2 = 0;
for (int i = 0; i < 3; i++) {
if (_board[i][i] == 0) {
numEmpty.first = pair<int, int>(i,i);
numEmpty.second += 1;
} else if (_board[i][i] == color) {
num1++;
} else {
num2++;
}
}
if (num1 == 2 && numEmpty.second == 1) {
deleteBroad(_board);
return numEmpty.first;
}
numEmpty = pair< pair<int, int>, int>(pair<int, int>(-1,-1),0);
num1 = 0;
num2 = 0;
for (int i = 2; i >= 0; i--) {
if (_board[i][2 - i] == 0) {
numEmpty.first = pair<int, int>(i,2 - i);
numEmpty.second += 1;
} else if (_board[i][2 - i] == color) {
num1++;
} else {
num2++;
}
}
if (num1 == 2 && numEmpty.second == 1) {
deleteBroad(_board);
return numEmpty.first;
}
numEmpty = pair< pair<int, int>, int>(pair<int, int>(-1,-1),0);
deleteBroad(_board);
return numEmpty.first;
}
int stateSpace::minimax(pair<int, int> state, int mycolor, int depth,int alpha ,int beta) { //放入由对方造成的局面
int othercolor = 0;
if (mycolor == 1) {
othercolor = 2;
} else {
othercolor = 1;
}
if (checkEndGame(state.first) == othercolor) {
return -1000;
} else if (checkEndGame(state.first) == mycolor) {
return 1000;
} else if (depth == 0) {
return 0;
} else {
if (state.second == othercolor) { //现在是对方造成的局面,我来接着下棋,max
int bestVal = -10000;
std::set<int> * possibleSet = stateSpace::getNextSteps(state.first, state.second);
for (std::set<int>::iterator it=possibleSet->begin(); it!=possibleSet->end(); ++it) {
bestVal = std::max(bestVal, minimax(pair<int, int>(*it, mycolor), mycolor, depth-1, alpha, beta));
alpha = std::max(alpha, bestVal);
if (beta <= alpha) {
break;
}
}
delete possibleSet;
return bestVal;
} else { //state.second == mycolor 我造成的局面, 对方会选择他的最优解,min
int bestVal = 10000;
std::set<int> * possibleSet = stateSpace::getNextSteps(state.first, othercolor);
for (std::set<int>::iterator it=possibleSet->begin(); it!=possibleSet->end(); ++it) {
bestVal = std::min(bestVal, minimax(pair<int, int>(*it, othercolor), mycolor, depth-1, alpha, beta));
beta = std::min(beta, bestVal);
if (beta <= alpha) {
break;
}
}
delete possibleSet;
return bestVal;
}
}
}
int stateSpace::getBoardScore(int code, int color) {
int other = 0;
if (color == 1) {
other = 2;
} else {
other = 1;
}
int score = 0;
int otherscore = 0;
if (criticalPoint(code,color) != 0) {
score = criticalPoint(code, color);
} else if (badPositionPoint(code, color) != 0) {
score = badPositionPoint(code, color);
} else {
score = freedPoint(code, color);
}
cout <<score<< endl;
if (criticalPoint(code,other) != 0) {
otherscore = criticalPoint(code, other);
} else if (badPositionPoint(code, other) != 0) {
otherscore = badPositionPoint(code, other);
} else {
otherscore = freedPoint(code, other);
}
cout <<otherscore<< endl;
return score - otherscore;
}
int stateSpace::freedPoint(int code, int color) {
std::set<int> * possibleSet = stateSpace::getNextSteps(code, color);
for (std::set<int>::iterator it=possibleSet->begin(); it!=possibleSet->end(); ++it) {
cout << stateSpace::DecToTer(*it) <<endl;
}
int points = 3 * possibleSet->size();
delete possibleSet;
return points;
}
int stateSpace::criticalPoint(int code, int color) {
int** _board = stateSpace::getBoard(code);
drawBoard(code);
int other = 0;
if (color == 1) {
other = 2;
} else {
other = 1;
}
//vertical
for (int i = 0; i < 3; i++) {
int num1 = 0;
int num2 = 0;
pair< pair<int, int>, int> numEmpty = pair< pair<int, int>, int>(pair<int, int>(0,0),0);
for (int j = 0; j < 3; j++) {
if (_board[j][i] == 0) {
numEmpty.first = pair<int, int>(j,i);
numEmpty.second += 1;
} else if (_board[j][i] == color) {
num1++;
} else {
num2++;
}
}
if (num1 == 2 && numEmpty.second == 1) {
pair<int, int> neighber = sameColorneighber(code, numEmpty.first.first, numEmpty.first.second, color, true);
std::set<pair<int, int> > * validNextPoint = stateSpace::validNextMove(code, neighber.first, neighber.second);//找出棋所有下一步可挪动坐标
std::set<pair<int,int> >::iterator it = validNextPoint->find( numEmpty.first );
if (it != (validNextPoint)->end()) {
deleteBroad(_board);
delete validNextPoint;
return 30;
}
delete validNextPoint;
}
}
//horizontal check
for (int i = 0; i < 3; i++) {
int num1 = 0;
int num2 = 0;
pair< pair<int, int>, int> numEmpty = pair< pair<int, int>, int>(pair<int, int>(0,0),0);
for (int j = 0; j < 3; j++) {
if (_board[i][j] == 0) {
numEmpty.first = pair<int, int>(i,j);
numEmpty.second += 1;
} else if (_board[i][j] == color) {
num1++;
} else {
num2++;
}
}
if (num1 == 2 && numEmpty.second == 1) {
pair<int, int> neighber = sameColorneighber(code, numEmpty.first.first, numEmpty.first.second, color, false);
cout << neighber.first << " and " << neighber.second << endl;
std::set<pair<int, int> > * validNextPoint = stateSpace::validNextMove(code, neighber.first, neighber.second);
// for (std::set<pair<int, int> >::iterator it2=validNextPoint->begin(); it2!=validNextPoint->end(); ++it2) {
// cout << "Here are valid " << it2->first << " and " << it2->second << endl;
// }
std::set<pair<int,int> >::iterator it = validNextPoint->find( numEmpty.first );
if (it != (validNextPoint)->end()) {
deleteBroad(_board);
delete validNextPoint;
return 30;
}
delete validNextPoint;
}
}
//diag
if (_board[0][0] == _board[2][2] &&_board[0][0] == color && _board[1][1] == 0) {
return 30;
}
if (_board[0][2] == _board[2][0] &&_board[0][2] == color && _board[1][1] == 0) {
return 30;
}
deleteBroad(_board);
return 0;
}
int stateSpace::badPositionPoint(int code, int color) {
int** _board = stateSpace::getBoard(code);
int other = 0;
if (color == 1) {
other = 2;
} else {
other = 1;
}
for (int i = 0; i < 3; i++) {
if (_board[0][i] == _board[2][i] && _board[0][i] == color && _board[1][i] == other) {
return 5;
}
if (_board[i][0] == _board[i][2] && _board[i][0] == color && _board[i][1] == other) {
return 5;
}
}
if (_board[0][0] == color && _board[0][0] == _board[2][2] && _board[1][1] == other) {
return 5;
}
if (_board[2][0] == color && _board[2][0] == _board[0][2] && _board[1][1] == other) {
return 5;
}
deleteBroad(_board);
return 0;
}
pair<int, int> stateSpace::sameColorneighber(int code, int x, int y, int color, bool isVertical) {
int** _board = stateSpace::getBoard(code);
pair<int, int> toreturn = pair<int, int>(-1,-1);
if (isVertical) {
int x_Coor = 0;
for (int i = -1; i < 2; i++) {
x_Coor = x+i;
if (intheBound(x_Coor, y-1) && _board[x_Coor][y-1] == color) {
return pair<int, int>(x_Coor, y-1);
}
if (intheBound(x_Coor, y+1) && _board[x_Coor][y+1] == color) {
return pair<int, int>(x_Coor, y+1);
}
}
} else {
int y_Coor = 0;
for (int i = -1; i < 2; i++) {
y_Coor = y+i;
if (intheBound(x-1, y_Coor) && _board[x-1][y_Coor] == color) {
return pair<int, int>(x-1, y_Coor);
}
if (intheBound(x+1, y_Coor) && _board[x+1][y_Coor] == color) {
return pair<int, int>(x+1, y_Coor);
}
}
}
deleteBroad(_board);
return pair<int, int>(-1, -1);
}
//pageRank
void stateSpace::pageRank() {
double pr[nodeList->size()];
int i = 0;
for (std::map<pair<int,int>,State*>::iterator it=nodeList->begin(); it!=nodeList->end(); ++it){
pr[i] = 1.0/(double)nodeList->size();
(it->second)->setRank(pr[i]);
}
for (int j = 0; j < 6; j++) {
int i = 0;
for (std::map<pair<int,int>,State*>::iterator it=nodeList->begin(); it!=nodeList->end(); ++it) {
pr[i] = 0;
for (State* s : *(it->second)->getNext()) {
pr[i] += (s->getRank() / s->numOfParents());
}
//if (pr[i] == 0) pr[i] =
//converge = converge && (pr[i] == (it->second)->getRank());
(it->second)->setRank(pr[i]);
i++;
}
}
}
// void stateSpace::growFrom2(stateSpace& space, pair<int, int> start) {
// /*
// stateSpace空的nodelist
// 在nodelist里面放入-9841(1赢),-19682(2赢)这两种状态
// stateSpace空的edgelist
// 给定空的queue
// 1. 给定一个状态放queue
// 2. while
// 取第一个
// get所有nextstep
// 对于这些nextstep
// 如果1赢:生成新的edge,放入edgelist,(不放入新node,不加入queue)
// 如果2赢:生成新的edge,放入edgelist,
// 如果nodeset里面有这个step,
// 生成新的edge,放入edgelist,生成对应state(不放入新node,不加入queue)
// 否则如果没有这个nodestep
// 将本step加入nodeset,生成新的edge,放入edgelist,生成对应state,加入queue
// 结果:private variable被initialize. nodelist,edgelist
// */
// space.insertNode(pair<int,int>(-9841,0));
// space.insertNode(pair<int,int>(-19682,0));
// std::queue<pair<int, int> > q;
// if (checkEndGame(start.first) == 0) {
// State * st = new State(start.first, start.second);
// space.putStart( st);
// q.push(start);
// space.insertNode(pair<int,int>(pair<int,int>(start.first,start.second)));
// }
// int i = 0; //change i in the while loop to change num of steps
// while (! q.empty()) {//&& i<6
// pair<int, int> s = q.front();
// q.pop();
// int nextColor = 0;
// if (s.second == 1) {
// nextColor = 2;
// } else {
// nextColor = 1;
// }
// std::set<int> * possibleSet = stateSpace::getNextSteps(s.first, nextColor);
// for (std::set<int>::iterator it=possibleSet->begin(); it!=possibleSet->end(); ++it) {
// State * stNext = new State(*it, nextColor);
// st->putNext(stNext);
// if (checkEndGame(*it) == 1) {
// pair<int, int> transit(s.first, -9841);
// space.insertEdge(transit, nextColor);
// } else if (checkEndGame(*it) == 2) {
// pair<int, int> transit(s.first, -19682);
// space.insertEdge(transit, nextColor);
// } else {
// std::set<pair<int,int> >::iterator it2 = (space.getNodeList())->find( pair<int,int>(*it,nextColor) );
// if (it2 != (space.getNodeList())->end()) {
// pair<int, int> transit(s.first, *it);
// space.insertEdge(transit, nextColor);
// } else {
// space.insertNode(pair<int,int>(*it,nextColor));
// pair<int, int> transit(s.first, *it);
// space.insertEdge(transit, nextColor);
// pair<int, int> state(*it, nextColor); //需不需要用new?
// q.push(state);
// }
// }
// }
// delete possibleSet;
// i++;
// //std::cout <<"11111111111111111111"<< endl;
// }
// }
///////////////////////THE ITERATOR//////////////////////
// class spaceTraversal {
// public:
// class Iterator : std::iterator<std::forward_iterator_tag, State> {
// public:
// Iterator(stateSpace graph_);
// Iterator(spaceTraversal* traversal_, State state_);
// Iterator & operator++();
// State operator*();
// bool operator!=(const Iterator &other);
// private:
// spaceTraversal* traversal;