-
Notifications
You must be signed in to change notification settings - Fork 1
/
Game.cpp
1544 lines (1408 loc) · 33.4 KB
/
Game.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 "Game.h"
#include "Utility.h"
#include <assert.h>
#include <algorithm>
using namespace std;
/** Utility Function */
bool isEqualPair(pair<int, int> P, pair<int, int> Q)
{
return (P.first == Q.first && P.second == Q.second);
}
Game::Game(int numberOfRings, int playerType)
{
// Initialise board specifications
numRings = numberOfRings;
boardSize = numberOfRings * 2 + 1;
numRingsToRemove = 3;
numRingsForRow = numberOfRings;
// Allocate a board
board = new int *[boardSize];
for (int i = 0; i < boardSize; i++)
{
board[i] = new int[boardSize];
}
// Initialize the board
// Allowed: Empty (0) | Disallowed: -7
Utility *allowed = new Utility();
for (int i = 0; i < boardSize; i++)
{
for (int j = 0; j < boardSize; j++)
{
board[i][j] = 0;
pair<int, int> polar = allowed->arrayToPolar(make_pair(i, j), boardSize);
int hex = polar.first, pos = polar.second;
if (hex > boardSize / 2 || (hex == boardSize / 2 && pos % hex == 0))
{
// not allowed
board[i][j] = -7;
}
}
}
// Allocate a 2D matrix for possible rows
nrows = new int *[boardSize];
for (int i = 0; i < boardSize; i++)
{
nrows[i] = new int[boardSize];
}
// filling up x_lims
int st, end;
for (int i = 0; i < boardSize; i++)
{
st = 0;
end = 0;
for (int j = 0; j < boardSize; j++)
{
if (board[i][j] != -7)
{
st = j;
break;
}
}
for (int j = boardSize - 1; j >= 0; j--)
{
if (board[i][j] != -7)
{
end = j;
break;
}
}
x_lims.push_back(make_pair(st, end));
}
//filling up y_lims
for (int i = 0; i < boardSize; i++)
{
st = 0;
end = 0;
for (int j = 0; j < boardSize; j++)
{
if (board[j][i] != -7)
{
st = j;
break;
}
}
for (int j = boardSize - 1; j >= 0; j--)
{
if (board[j][i] != -7)
{
end = j;
break;
}
}
y_lims.push_back(make_pair(st, end));
}
// filling up xy_lims
for (int i = 1 - boardSize; i < boardSize; i++)
{
st = 0;
end = 0;
for (int j = 0; j < boardSize; j++)
{
if (i + j >= 0 && i + j < boardSize && board[i + j][j] != -7)
{
st = j;
break;
}
}
for (int j = boardSize - 1; j >= 0; j--)
{
if (i + j >= 0 && i + j < boardSize && board[i + j][j] != -7)
{
end = j;
break;
}
}
xy_lims.push_back(make_pair(st, end));
}
//Initialise nrows
for (int i = 0; i < boardSize; i++)
{
for (int j = 0; j < boardSize; j++)
{
if (board[i][j] == -7)
{
nrows[i][j] = -7;
continue;
}
nrows[i][j] = getOverlaps(x_lims.at(i).first, x_lims.at(i).second, j);
nrows[i][j] += getOverlaps(y_lims.at(j).first, y_lims.at(j).second, i);
nrows[i][j] += getOverlaps(xy_lims.at(i - j + boardSize - 1).first, xy_lims.at(i - j + boardSize - 1).second, j);
}
}
// Allocate a board
playerPos = new int *[boardSize];
for (int i = 0; i < boardSize; i++)
{
playerPos[i] = new int[boardSize];
}
// Allocate a board
playerNeg = new int *[boardSize];
for (int i = 0; i < boardSize; i++)
{
playerNeg[i] = new int[boardSize];
}
// initialise playerPos and playerNeg
for (int i = 0; i < boardSize; i++)
{
for (int j = 0; j < boardSize; j++)
{
playerPos[i][j] = 0;
playerNeg[i][j] = 0;
}
}
// Initialise the game state
playerAssgn = playerType;
playerToMove = 1; // Always player 0 moves first
gameStatePos = 1; // Place Ring
gameStateNeg = 1; // Place Ring
posRingsPlaced = false;
negRingsPlaced = false;
}
int Game::getPlayerToMove()
{
return playerToMove;
}
int Game::getGameState()
{
if (playerToMove > 0)
{
return gameStatePos;
}
else
{
return gameStateNeg;
}
}
void Game::updateGameState(int player)
{
// cerr << "Updating Game State for player: " << player << endl;
// This function is called after making a valid sequence of moves
// Thus it is assumed that game CANNOT be in state 4
/*
contiguousMarkers markers = getAllContiguousMarkers(player);
// cerr << "In Game::updateGameState markers+.size()=" << markers.size() << endl;
if (markers.size() == 0)
{
// No marker streak to remove
gameStatePos = 2;
}
else
{
gameStatePos = 3;
}
return;
*/
if (player > 0)
{
// It's positive player
if (ringsPositive.size() < numRings && !posRingsPlaced)
{
// More rings yet to come
gameStatePos = 1;
}
else
{
// gameStatePos = 2; // REMOVE IT AFTER getAllContiguousMarkers Is Debugged
// return;
contiguousMarkers markers = getAllContiguousMarkers(1);
// cerr << "In Game::updateGameState markers+.size()=" << markers.size() << endl;
if (markers.size() == 0)
{
// No marker streak to remove
gameStatePos = 2;
}
else
{
gameStatePos = 3;
}
}
}
else
{
// It's positive player
if (ringsNegative.size() < numRings && !negRingsPlaced)
{
// More rings yet to come
gameStateNeg = 1;
}
else
{
// gameStateNeg = 2; // REMOVE IT AFTER getAllContiguousMarkers Is Debugged
// return;
contiguousMarkers markers = getAllContiguousMarkers(-1);
// cerr << "In Game::updateGameState markers-.size()=" << markers.size() << endl;
if (markers.size() == 0)
{
// No marker streak to remove
gameStateNeg = 2;
}
else
{
gameStateNeg = 3;
}
}
}
}
/** NOTE: I ASSUME THAT THE MOVE GIVEN TO ME IS VALID MOVE
* AND DO NOT CHECK VALIDITY FOR EFFICIENCY PURPOSES.
* FOR ERRORS WHICH CAN'T BE IGNORED FOR SAFE EXIT OF PROGRAM, I RETURN false
*/
/* playerToMove flipping is managed by ME now */
bool Game::makeMove(const Move &move)
{
// Make all the constituent micromove
bool status = true;
vector<MicroMove> moveSeq = move.getMoveSeq();
for (int m = 0; m < moveSeq.size(); m++)
{
status = status && makeMicroMove(moveSeq[m]);
}
// Update state of rings placed
if (ringsPositive.size() == numRings)
{
// All have been placed
posRingsPlaced = true;
}
if (ringsNegative.size() == numRings)
{
// All have been placed
negRingsPlaced = true;
}
updateGameState(1);
updateGameState(-1);
// updateGameState(playerToMove);
// int size = moveSeq.size();
// cerr << "In Game::makeMove " << moveSeq[0].cartesianToPolarString(boardSize);
// if (size > 0 && moveSeq[0].type == 'P')
// {
// // If move is one place ring then state may become 1
// // If ring is left to be placed, update accordingly
// if (moreToPlace())
// {
// setGameState(1);
// }
// }
// Flip the chance => DON'T => YES
playerToMove *= -1;
// Update game state for both the players
// cerr << "In Game::makeMove: posState= " << gameStatePos << " | negState= " << gameStateNeg << endl;
// Commenting below for now since a valid move will never have 'R' in it's end
/*
int size = moveSeq.size();
if (size > 0 && moveSeq[size - 1].type == 'R')
{
// If last move is one to pick markers then state will become 4
if (playerToMove > 0)
{
gameStatePos = 4;
updateGameState(-1);
}
else
{
gameStateNeg = 4;
updateGameState(1);
}
}
else
{
updateGameState(1);
updateGameState(-1);
}
*/
// success
return status;
}
/*
bool Game::unmakeMove(Move move)
{
// Unmake all the constituent micromove IN REVERSE ORDER
// Don't do following. RESPONSIBILITY OF CALLER to flip playerToMove
// Flip the chance first since we wanna see it with the perspective of player who played the game
// and not the current player
// playerToMove *= -1;
bool status = true;
vector<MicroMove> moveSeq = move.getMoveSeq();
for (int m = moveSeq.size() - 1; m >= 0; m--)
{
status = status && unmakeMicroMove(moveSeq[m]);
}
// Update game state for both the players
int size = moveSeq.size();
if (size > 0 && moveSeq[0].type == 'X')
{
// If first move was one to remove a ring then state will become 4
if (playerToMove > 0)
{
gameStatePos = 4;
updateGameState(-1);
}
else
{
gameStateNeg = 4;
updateGameState(1);
}
}
else
{
updateGameState(1);
updateGameState(-1);
}
// success
return status;
}
*/
bool Game::makeMicroMove(const MicroMove &move)
{
switch (move.type)
{
case 'P':
// Place the ring at the position
return placeRing(move.moveInfo[0]);
break;
case 'X':
// Remove a Ring
return removeRing(move.moveInfo[0]);
break;
case 'M':
// Select Ring and Move
return moveRing(move.moveInfo[0], move.moveInfo[1], true);
break;
case 'R':
// Remove a Row
return removeMarkers(move.moveInfo[0], move.moveInfo[1]);
break;
default:
// None of the above types of moves
cerr << "Invalid move given to Game::makeMicroMove()\n";
return false;
}
}
/** Play the converse of the move */
bool Game::unmakeMicroMove(const MicroMove &move)
{
switch (move.type)
{
case 'P':
// Remove the Ring
return removeRing(move.moveInfo[0]);
break;
case 'X':
// Place the ring at the position
return placeRing(move.moveInfo[0]);
break;
case 'M':
// Select Ring and Reverse Move
return moveRing(move.moveInfo[1], move.moveInfo[0], false);
break;
case 'R':
// Repopulate the Row
return populateMarkers(move.moveInfo[1], move.moveInfo[0], playerToMove);
break;
default:
// None of the above types of moves
cerr << "Invalid Move given to Game::unmakeMicroMove()\n";
return false;
}
}
bool Game::placeRing(const pair<int, int> &ringPos)
{
// Place the ring at the position
if (playerToMove > 0)
{
// positive player's turn
ringsPositive.push_back(ringPos);
board[ringPos.first][ringPos.second] = 2;
}
else
{
// negative player's turn
ringsNegative.push_back(ringPos);
board[ringPos.first][ringPos.second] = -2;
}
return true; // done
}
bool Game::removeRing(const pair<int, int> &ringPos)
{
if (playerToMove > 0) // positive player's turn
{
// Lookup the position of the ring and delete it
int ringNo = lookupRing(ringPos, 1); // position of this ring in the rings array
if (ringNo < 0)
return false;
ringsPositive.erase(ringsPositive.begin() + ringNo); // Remove the ring from the array
board[ringPos.first][ringPos.second] = 0; // Remove the ring from the board
}
else // negative player's turn
{
// Lookup the position of the ring and delete it
int ringNo = lookupRing(ringPos, -1); // position of this ring in the rings array
if (ringNo < 0)
return false;
ringsNegative.erase(ringsNegative.begin() + ringNo); // Remove the ring from the array
board[ringPos.first][ringPos.second] = 0; // Remove the ring from the board
}
return true;
}
bool Game::moveRing(const pair<int, int> &initialPos, const pair<int, int> &finalPos, bool isForwardMove)
{
if (playerToMove > 0) // positive player's turn
{
// move the ring
int ringNo = lookupRing(initialPos, 1); // position of this ring in the rings array
if (ringNo < 0)
return false; // could not find our ring in the array
ringsPositive[ringNo] = finalPos;
board[finalPos.first][finalPos.second] = 2;
// Leave a marker at the current position if actual move
if (isForwardMove)
board[initialPos.first][initialPos.second] = 1;
else
board[initialPos.first][initialPos.second] = 0;
// Flip the markers
return flipMarkers(initialPos, finalPos);
}
else // negative player's turn
{
// move the ring
int ringNo = lookupRing(initialPos, -1); // position of this ring in the rings array
if (ringNo < 0)
return false; // could not find our ring in the array
ringsNegative[ringNo] = finalPos;
board[finalPos.first][finalPos.second] = -2;
// Leave a marker at the current position if actual move
if (isForwardMove)
board[initialPos.first][initialPos.second] = -1;
else
board[initialPos.first][initialPos.second] = 0;
// Flip the markers
return flipMarkers(initialPos, finalPos);
}
}
int Game::lookupRing(const pair<int, int> &position, int player)
{
if (player > 0)
{
// positive player
for (int ringNo = 0; ringNo < ringsPositive.size(); ringNo++)
{
pair<int, int> ringPos = ringsPositive[ringNo];
if (ringPos.first == position.first && ringPos.second == position.second)
{
// found the ring in the array => return the index
return ringNo;
}
}
// Not found
return -1;
}
else
{
// negative player
for (int ringNo = 0; ringNo < ringsNegative.size(); ringNo++)
{
pair<int, int> ringPos = ringsNegative[ringNo];
if (ringPos.first == position.first && ringPos.second == position.second)
{
// found the ring in the array => return the index
return ringNo;
}
}
// Not found
return -1;
}
}
bool Game::flipMarkers(const pair<int, int> &startPoint, const pair<int, int> &endPoint)
{
pair<int, bool> traversalOrder = determineOrderTraversal(startPoint, endPoint);
if (traversalOrder.first < 0 || traversalOrder.first >= 3)
{
// Invalid
return false;
}
pair<int, int> currPoint = nextPosition(startPoint, traversalOrder);
while (!isEqualPair(currPoint, endPoint))
{
// Flip this marker
board[currPoint.first][currPoint.second] *= -1;
// Move forward
currPoint = nextPosition(currPoint, traversalOrder);
}
return true;
}
bool Game::removeMarkers(const pair<int, int> &startPoint, const pair<int, int> &endPoint)
{
pair<int, bool> traversalOrder = determineOrderTraversal(startPoint, endPoint);
if (traversalOrder.first < 0 || traversalOrder.first >= 3)
{
// Invalid
return false;
}
// Initialise the current position
pair<int, int> currPoint = startPoint;
// Remove the marker at the current position
board[currPoint.first][currPoint.second] = 0;
if (isEqualPair(currPoint, endPoint)) // this is the end
return true;
do
{
// Move forward
currPoint = nextPosition(currPoint, traversalOrder);
// Remove the marker at the current position
board[currPoint.first][currPoint.second] = 0;
} while (!isEqualPair(currPoint, endPoint));
return true;
}
bool Game::populateMarkers(const pair<int, int> &startPoint, const pair<int, int> &endPoint, int player)
{
pair<int, bool> traversalOrder = determineOrderTraversal(startPoint, endPoint);
if (traversalOrder.first < 0 || traversalOrder.first >= 3)
{
// Invalid
return false;
}
// Get the color of marker you wanna populate with
int markerColor = (player > 0) ? 1 : -1;
// Initialise the current position
pair<int, int> currPoint = startPoint;
// Remove the marker at the current position
board[currPoint.first][currPoint.second] = markerColor;
if (isEqualPair(currPoint, endPoint)) // this is the end
return true;
do
{
// Move forward
currPoint = nextPosition(currPoint, traversalOrder);
// Remove the marker at the current position
board[currPoint.first][currPoint.second] = markerColor;
} while (!isEqualPair(currPoint, endPoint));
return true;
}
pair<int, bool> Game::determineOrderTraversal(const pair<int, int> &startPoint, const pair<int, int> &endPoint)
{
if (startPoint.second == endPoint.second)
{
// along X
return make_pair(0, startPoint.first < endPoint.first);
}
else if (startPoint.first == endPoint.first)
{
// along Y
return make_pair(1, startPoint.second < endPoint.second);
}
else if (startPoint.first - endPoint.first == startPoint.second - endPoint.second)
{
// along X = Y + c
return make_pair(2, startPoint.first < endPoint.first);
}
else
{
// Invalid
return make_pair(3, true);
}
}
pair<int, int> Game::nextPosition(const pair<int, int> &position, const pair<int, bool> &traversalOrder)
{
switch (traversalOrder.first)
{
case 0: // X
if (traversalOrder.second)
return make_pair(position.first + 1, position.second);
else
return make_pair(position.first - 1, position.second);
break;
case 1: // Y
if (traversalOrder.second)
return make_pair(position.first, position.second + 1);
else
return make_pair(position.first, position.second - 1);
break;
default: // Assuming X = Y + c
if (traversalOrder.second)
return make_pair(position.first + 1, position.second + 1);
else
return make_pair(position.first - 1, position.second - 1);
break;
}
}
vector<MicroMove> Game::getAllMoves(bool sortOrder)
{
// Gets all the possible moves from the current state
int stateOfGame = (playerToMove > 0) ? gameStatePos : gameStateNeg;
switch (stateOfGame)
{
case 1:
// Have to place ring
return getAllPlaceRingMoves(playerToMove, sortOrder);
case 2:
// Have to select and move a ring
return getAllSelectMoveMoves(playerToMove, sortOrder);
case 3:
// Have to remove a row
return getAllRemoveRowMoves(playerToMove, sortOrder);
case 4:
// Remove a ring
return getAllRemoveRingMoves(playerToMove, sortOrder);
default:
// Return empty
cerr << "Game state invalid in Game::getAllMoves()\n";
return vector<MicroMove>();
}
}
vector<MicroMove> Game::getAllPlaceRingMoves(int player, bool sortOrder)
{
// Iterate the board to find an empty position => Make a move out of it
vector<MicroMove> possibleMoves;
for (int i = 0; i < boardSize; i++)
{
for (int j = 0; j < boardSize; j++)
{
if (board[i][j] == 0)
{
// Insert it
vector<pair<int, int>> moveInfo;
moveInfo.push_back(make_pair(i, j));
MicroMove move('P', moveInfo);
// cerr << "In Game::getAllPlaceRingMoves, moveInfo.size() = " << moveInfo.size() << move.cartesianToPolarString(boardSize) << endl;
possibleMoves.push_back(move);
}
}
}
return sortMoves(possibleMoves, sortOrder);
// return possibleMoves;
}
vector<MicroMove> Game::getAllRemoveRingMoves(int player, bool sortOrder)
{
// Iterate through the players rings and return corresponding remove moves
vector<MicroMove> possibleMoves;
vector<pair<int, int>> rings = (player > 0) ? ringsPositive : ringsNegative;
// Iterate through all rings
for (auto it = rings.begin(); it != rings.end(); it++)
{
// Insert it
vector<pair<int, int>> moveInfo(1, make_pair((*it).first, (*it).second));
MicroMove move = MicroMove('X', moveInfo);
possibleMoves.push_back(move);
}
return sortMoves(possibleMoves, sortOrder);
// return possibleMoves;
}
vector<MicroMove> Game::getAllRemoveRowMoves(int player, bool sortOrder)
{
// Find all contiguous rows and transform them into corresponding move to remove
contiguousMarkers markersArr = getAllContiguousMarkers(player);
vector<MicroMove> possibleMoves;
for (auto conMarker = markersArr.begin(); conMarker != markersArr.end(); conMarker++)
{
// Insert it
vector<pair<int, int>> moveInfo;
moveInfo.push_back((*conMarker).first);
moveInfo.push_back((*conMarker).second);
MicroMove move = MicroMove('R', moveInfo);
possibleMoves.push_back(move);
}
return sortMoves(possibleMoves, sortOrder);
// return possibleMoves;
}
vector<MicroMove> Game::getAllSelectMoveMoves(int player, bool sortOrder)
{
// Select a ring and find all possible destinations
vector<MicroMove> possibleMoves;
vector<pair<int, int>> rings = (player > 0) ? ringsPositive : ringsNegative;
for (auto ring = rings.begin(); ring != rings.end(); ring++)
{
// Iterate in all directions and get possible directions
for (int direc = 0; direc < 6; direc++)
{
vector<pair<int, int>> destinations = getAllPossibleDestinationsInDirection(*ring, direc);
for (auto dest = destinations.begin(); dest != destinations.end(); dest++)
{
// Make a move out of it
vector<pair<int, int>> moveInfo;
moveInfo.push_back(*ring);
moveInfo.push_back(*dest);
MicroMove move = MicroMove('M', moveInfo);
possibleMoves.push_back(move);
}
}
}
return sortMoves(possibleMoves, sortOrder);
// return possibleMoves;
}
bool compareMicroMoveUtilityLesser(const pair<double, MicroMove> &P1, const pair<double, MicroMove> &P2)
{
return P1.first < P2.first;
}
bool compareMicroMoveUtilityGreater(const pair<double, MicroMove> &P1, const pair<double, MicroMove> &P2)
{
return P1.first > P2.first;
}
vector<MicroMove> Game::sortMoves(const vector<MicroMove> &moves, bool sortOrder)
{
// Sorts the moves in order of their utility
vector<pair<double, MicroMove>> utilMoves;
for (auto mv = moves.begin(); mv != moves.end(); mv++)
{
utilMoves.push_back(make_pair(getMicroMoveUtility(*mv), *mv));
}
if (sortOrder)
{
// Sort in ascending order
sort(utilMoves.begin(), utilMoves.end(), compareMicroMoveUtilityLesser);
}
else
{
// Sort in Descending order
sort(utilMoves.begin(), utilMoves.end(), compareMicroMoveUtilityGreater);
}
vector<MicroMove> sortedMoves;
for (auto mv = utilMoves.begin(); mv != utilMoves.end(); mv++)
{
sortedMoves.push_back((*mv).second);
}
return sortedMoves;
}
double Game::getMicroMoveUtility(const MicroMove &move)
{
// Make the move
bool status = makeMicroMove(move);
if (!status)
cerr << "Unable to makeMove in Game::getMicroMoveUtility" << endl;
double utility = getUtility();
// Unmake the move
status = unmakeMicroMove(move);
if (!status)
cerr << "Unable to unmakeMove in Game::getMicroMoveUtility" << endl;
return utility;
}
vector<pair<int, int>> Game::getAllPossibleDestinationsInDirection(pair<int, int> ringPos, int direc)
{
// Traverse in the given direction and get destinations
vector<pair<int, int>> destinations;
bool hasSkipped = false; // has it skipped a row of markers till yet
// Move one step in the direction
ringPos = advanceInDirection(ringPos, direc);
while (!isOutOfBounds(ringPos))
{
int x = ringPos.first, y = ringPos.second;
if (board[x][y] == -7 || board[x][y] == 2 || board[x][y] == -2)
break; // reached the edge (really or virtually)
if (board[x][y] == 0)
{
// A destination
destinations.push_back(make_pair(x, y));
if (hasSkipped)
break; // Has skipped so have to stop here
}
else
{
// This is a marker
hasSkipped = true;
}
// Move forward
ringPos = advanceInDirection(ringPos, direc);
}
return destinations;
}
bool Game::isOutOfBounds(pair<int, int> pos)
{
return (pos.first < 0 || pos.first >= boardSize || pos.second < 0 || pos.second >= boardSize);
}
pair<int, int> Game::advanceInDirection(pair<int, int> pos, int direc)
{
// Direc is anticlockwise
switch (direc)
{
case 0:
// +Y
pos.second++;
return pos;
case 1:
// -X
pos.first--;
return pos;
case 2:
// -(X+Y)
pos.first--;
pos.second--;
return pos;
case 3:
// -Y
pos.second--;
return pos;
case 4:
// +X
pos.first++;
return pos;
case 5:
// +(X+Y)
pos.first++;
pos.second++;
return pos;
default:
cerr << "Invalid direction in Game::advanceInDirection\n";
return pos;
}
}
bool Game::isTerminalState()
{
// if (getGameState() == 1)
if (gameStatePos == 1 || gameStateNeg == 1)
{
// Game in ring placing stage
return false;
}
if (numRings - ringsPositive.size() >= numRingsToRemove ||
numRings - ringsNegative.size() >= numRingsToRemove)
{
// Either one of the player has removed sufficient number of rings
return true;
}
// No one has removed sufficient number of rings
return false;
}
contiguousMarkers Game::getOneRow(int i, int j, int ctr, int mode)
{
contiguousMarkers oneRow = contiguousMarkers();
if (mode == 0)
{
oneRow.push_back(make_pair(make_pair(i, j - numRingsForRow), make_pair(i, j - 1)));
if (ctr > numRingsForRow)
{
oneRow.push_back(make_pair(make_pair(i, j - ctr), make_pair(i, j - ctr + numRingsForRow - 1)));
}
}
else if (mode == 1)
{
oneRow.push_back(make_pair(make_pair(j - numRingsForRow, i), make_pair(j - 1, i)));
if (ctr > numRingsForRow)
{
oneRow.push_back(make_pair(make_pair(j - ctr, i), make_pair(j + numRingsForRow - ctr - 1, i)));
}
}
else if (mode == 2)
{
oneRow.push_back(make_pair(make_pair(i - numRingsForRow, j - numRingsForRow), make_pair(i - 1, j - 1)));
if (ctr > numRingsForRow)
{
oneRow.push_back(make_pair(make_pair(i - ctr, j - ctr), make_pair(i + numRingsForRow - ctr - 1, j + numRingsForRow - ctr - 1)));
}
}
return oneRow;
}
contiguousMarkers Game::getAllContiguousMarkers(int player)
{
// Place holder
contiguousMarkers markers = contiguousMarkers(), row;