-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathGame.h
More file actions
297 lines (244 loc) · 8.45 KB
/
Game.h
File metadata and controls
297 lines (244 loc) · 8.45 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
#ifndef GAME_H
#define GAME_H
#define contiguousMarkers vector<pair< pair<int, int>, pair<int, int> >>
#include <vector>
#include <iostream>
#include <iomanip>
#include "Move.h"
#include "MicroMove.h"
using namespace std;
class Game
{
private: // for testing purposes => please make private
// Game parameters
int numRings;
int boardSize;
int numRingsToRemove;
int numRingsForRow;
// Game Board
int **board; // 0: Empty | 1: Positive Marker | 2: Positive Ring | -1: Negative Marker | -2: Negative Ring
int **nrows; // -7: illegal, n: number of possible rows of numRingsForRow rings at (i,j)
int **playerPos; // number of player 0's rings controlling each square
int **playerNeg; // number of player 1's rings controlling each square
vector<pair<int, int>> ringsPositive; // Positions of rings of the positive player
vector<pair<int, int>> ringsNegative; // Positions of rings of the positive player
vector<pair<int,int>> x_lims; // limits of i-th row along x-axis
vector<pair<int,int>> y_lims; // limits of i-th row along y-axis
vector<pair<int,int>> xy_lims; // limits of i-th row along xy-axis (x - y = constant)
int getOverlaps(int l, int r, int pt);
// Game State (from point of view of both players)
int playerAssgn; // 0 means first move, 1 means second move
int playerToMove; // -1 or 1
int gameStatePos; // 1: Place Ring | 2: Select Ring and Move Ring | 3: Remove a Row | 4: Remove a Ring
int gameStateNeg; // 1: Place Ring | 2: Select Ring and Move Ring | 3: Remove a Row | 4: Remove a Ring
bool posRingsPlaced; // Have all rings been placed
bool negRingsPlaced; // Have all rings been placed
public:
/**
* @constructor
* Makes an empty board of given specification
* Initializes the game to appropriate game state
*/
// Game();
Game(int numberOfRings, int playerType);
/**
* Returns the `playerToMove`
*/
int getPlayerToMove ();
vector<pair<int, int>> getRingsPositive () {
return ringsPositive;
}
vector<pair<int, int>> getRingsNegative () {
return ringsNegative;
}
void flipPlayerToMove () {
playerToMove *= -1;
}
int getBoardSize () {
return boardSize;
}
void setGameState (int state) {
if (playerToMove > 0)
{
gameStatePos = state;
}
else
{
gameStateNeg = state;
}
}
bool moreToPlace() {
if (playerToMove > 0) {
return (ringsPositive.size() < numRings);
} else {
return (ringsNegative.size() < numRings);
}
}
/**
* Returns the `gameState`
*/
int getGameState ();
/**
* Looks at the board configuration and playerToMove, and updates the game state
*/
void updateGameState(int player);
/**
* Return all the moves that the `playerToMove` can play from the given state
* sortOrder true for increasing sequence of utilities
*/
vector<MicroMove> getAllMoves(bool sortOrder);
/**
* Returns all possible moves to place a ring on the board
*/
vector<MicroMove> getAllPlaceRingMoves(int player, bool sortOrder);
/**
* Returns all possible moves to select and move a possible ring
*/
vector<MicroMove> getAllSelectMoveMoves(int player, bool sortOrder);
/**
* Returns all possible moves to remove a contiguous row
*/
vector<MicroMove> getAllRemoveRowMoves(int player, bool sortOrder);
/**
* Returns all possible moves to remove a ring
*/
vector<MicroMove> getAllRemoveRingMoves(int player, bool sortOrder);
/**
* Returns the given moves in sorted order of utility
* sortOrder = true sorts in increasing order
*/
vector<MicroMove> sortMoves(const vector<MicroMove> &moves, bool sortOrder);
/**
* Applies given move to board
* computes the utility
* unmake the move and returns the utility
*/
double getMicroMoveUtility (const MicroMove &move);
/**
* Returns all positions where the given ring can be moved to in a given direction[0-5] (Aniclokwise from top)
*/
// vector<pair<int, int>> getAllPossibleDestinations(pair<int, int> ringPos);
pair<int, int> advanceInDirection(pair<int, int> pos, int direc);
vector<pair<int, int>> getAllPossibleDestinationsInDirection(pair<int, int> ringPos, int direc);
/**
* Tells if the given position is out of bounds
*/
bool isOutOfBounds(pair<int, int> pos);
/**
* Play the full move, and return success status
*/
bool makeMove(const Move &move);
/**
* Unplay the full move, and return success status
*/
// bool unmakeMove(Move &move);
/**
* Play the micro-move, and return success status
*/
bool makeMicroMove(const MicroMove &move);
/**
* Unplay the micro-move, and return success status
*/
bool unmakeMicroMove(const MicroMove &move);
/**
* Place the ring at the given position for the `playerToMove`
* return state of success
*/
bool placeRing(const pair<int, int>& ringPos);
/**
* Remove the ring at the given position for the `playerToMove`
* return state of success
*/
bool removeRing(const pair<int, int> &ringPos);
/**
* Moves the ring of the current player from initialPos to finalPos
* Depending on actual or reverse move, either leaves a marker at old position or leave it empty
* Flips all markers in both cases
* Note: Does not check if the move is really valid to move
* returns status of success
*/
bool moveRing (const pair<int, int> &initialPos, const pair<int, int> &finalPos, bool isForwardMove);
/**
* Lookup the position of the ring in the respective array and return the index
*/
int lookupRing(const pair<int, int> &position, int player);
/**
* Flips the markers between startPoint and endPoint (exclusive)
* returns state of success
*/
bool flipMarkers (const pair<int, int> &startPoint, const pair<int, int> &endPoint);
/**
* Removes the markers between startPoint and endPoint (inclusive)
* return state of success
*/
bool removeMarkers (const pair<int, int> &startPoint, const pair<int, int> &endPoint);
/**
* Populates the board from startPoint to endPoint (inclusive),
* with the markers of the Player 'player'
* return state of success
*/
bool populateMarkers (const pair<int, int> &startPoint, const pair<int, int> &endPoint, int player);
/**
* Determines in which direction to traverse the board to go from startPoint to endPoint
* 0: X | 1: Y | 2: X+Y | 3: Invalid
* true: do variable++ to iterate | false: do variable-- to iterate
*/
pair<int, bool> determineOrderTraversal (const pair<int, int> &startPoint, const pair<int, int> &endPoint);
/**
* Moves the iterator forward, returning the next position of the iterator
* according to the current position and direction of movement
*/
pair<int, int> nextPosition(const pair<int, int> &position, const pair<int, bool> &traversalOrder);
/**
* Returns whether the current game state is a win for the `playerToMove`
*/
bool isTerminalState();
/**
* Returns an array of all continous streak of markers of player
*/
contiguousMarkers getAllContiguousMarkers (int player);
contiguousMarkers getOneRow(int index, int curr, int ctr, int mode);
/**
* Displays current board state
*/
void displayBoard();
/**
* Display the board in hexagonal format
*/
void displayHexagonalBoard();
/**
* Displays positive player influence
*/
void displayP();
/**
* Displays negative player influence
*/
void displayN();
/**
* Displays possible rows for each location on game board
*/
void displayNrows();
/**
* Displays size limits of gameboard in vertical, horizontal and diagonal directions
*/
void dispLims();
/**
* Computes 'influence' of both players at each board location
*/
void computePlayerPos();
void computePlayerNeg();
void addRing(int**player, int x, int y);
/**
* Computes metric based on player influence
*/
double computeMetric();
double computeMetric2(int player);
int selectRowLength(int curr, int prev, int dist);
/**
* Returns the utility of this state w.r.t. `playerToMove`
* Called each time when minimax is cut-off
*/
double getUtility(); // @Soumya
double getRingUtility();
};
#endif // GAME_H