-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGameState.java
More file actions
294 lines (260 loc) · 8.4 KB
/
Copy pathGameState.java
File metadata and controls
294 lines (260 loc) · 8.4 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
/****************************************************************
* GameState.java
*
*/
public final class GameState
{
/** returned by status() when the game is not over **/
public static final int GAME_NOT_OVER = Integer.MIN_VALUE;
/** returned by status() when the player on the lower row has won **/
public static final int GAME_OVER_WIN = 1;
/** returned by status() when game is over but the players have tied **/
public static final int GAME_OVER_TIE = 0;
/** returned by status() when the player on the upper row has won **/
public static final int GAME_OVER_LOSE = -1;
public int[] state = { 4, 4, 4, 4, 4, 4, 0, 4, 4, 4, 4, 4, 4, 0 };
/**
* This default constructor will create an initial board for the beginning
* of a game. Each pocket has 4 stones initially.
*/
public GameState() {}
/**
* This constructor assigns a different initial number of stones in the
* bins of the mancala board than the default 4.
*
* @param initialStones the initial number of stones in the bins
* must be > 0, otherwise it defaults to 4.
*/
public GameState(int initialStones)
{
if (initialStones < 1) initialStones = 4;
for (int i=0; i<6; i++)
state[i] = state[i+7] = initialStones;
}
/**
* A copy constructor
* @param source the object out of which to construct a copy
*/
public GameState(GameState source)
{
this.state = arrayCopy(source.state);
}
/**
* returns a class constant indicating the current status of the game
* @return the current game status as defined by the class constants
*/
public int status()
{
if (stonesCount(7,12) != 0 && stonesCount(0,5) != 0)
return GAME_NOT_OVER;
else if (stonesCount(7,13) < stonesCount(0,6))
return GAME_OVER_WIN;
else if (stonesCount(7,13) == stonesCount(0,6))
return GAME_OVER_TIE;
else
return GAME_OVER_LOSE;
}
/**
* returns whether or not the game is over by making a call to status()
* @return true; the game is over<br>false; otherwise
*/
public boolean gameOver()
{
return status() != GAME_NOT_OVER;
}
/**
* provides a copy of the mancala board as an array of integers where the bin
* numbers are indexed in the following way:<pre>
-----------------------------------------
| | 12 | 11 | 10 | 9 | 8 | 7 | |
| 13 |-----------------------------| 6 |
| | 0 | 1 | 2 | 3 | 4 | 5 | |
-----------------------------------------</pre>
* @return an array of the number of stones in each bin
*/
public int[] toArray()
{
return arrayCopy(state);
}
/**
* provides a copy of the manacala board as a matrix of integers where the bin
* coordinates are indexed in the following way:<pre>
-------------------------------------------------
| |(1,5)|(1,4)|(1,3)|(1,2)|(1,1)|(1,0)| |
|(1,6)|-----------------------------------|(0,6)|
| |(0,0)|(0,1)|(0,2)|(0,3)|(0,4)|(0,5)| |
-------------------------------------------------</pre>
* @return a matrix of the number of stones in each bin
*/
public int[][] toMatrix()
{
int[][] matrix = new int[2][7];
for (int i = 0; i < 14; ++i)
matrix[i/7][i%7] = state[i];
return matrix;
}
/**
* an accesor for the number of stones in the bin where the bin numbers are
* indexed in the following way:<pre>
-----------------------------------------
| | 12 | 1 | 10 | 9 | 8 | 7 | |
| 13 |-----------------------------| 6 |
| | 0 | 1 | 2 | 3 | 4 | 5 | |
-----------------------------------------</pre>
* @param bin the bin to querry for number of stones
* @return the number of stones in the bin
*/
public int stoneCount(int bin)
{
return state[bin];
}
/**
* an accessor for the number of stones in the bin where the bin numbers are
* indexed in the following way:<pre>
-------------------------------------------------
| |(1,5)|(1,4)|(1,3)|(1,2)|(1,1)|(1,0)| |
|(1,6)|-----------------------------------|(0,6)|
| |(0,0)|(0,1)|(0,2)|(0,3)|(0,4)|(0,5)| |
-------------------------------------------------</pre>
* @param bin1 the first coordinate
* @param bin2 the second coordinate
* @return the number of stones in the bin
*/
public int stoneCount(int bin1, int bin2)
{
return state[7*bin1 + bin2];
}
/**
* returns a boolean based upon the state and the bin chosen. For efficiency,
* this method does not take into account state status or turns.
*
* @param bin the proposed bin from which to advance the stones
* must be between 0 and 5 inclusive
* @return true; the move from this state is legal<br>
* false; otherwise
*/
public boolean illegalMove(int bin)
{
return (state[bin] == 0 || bin < 0 || bin == 6 || bin > 12);
}
/**
* applies the given move to the given state
* @param state the initial state before the method executes,
* and the resulting state once the method completes
* @param bin the move to apply to the state
*
* @returns true if the player gets to move again, false otherwise.
*/
public boolean applyMove(int bin)
{
int stones = state[bin];
//clear the original bin
state[bin] = 0;
for (int i = 0; i < stones; ++i)
{
int nextBin = (bin+i+1)%14;
if (!(nextBin == 6 && bin > 6) && !(nextBin == 13 && bin < 7))
++state[nextBin];
else
++stones;
}
int lastBin = (bin+stones)%14;
boolean lastBinEmpty = state[lastBin] == 1;
boolean lastBinOnYourSide = bin/7 == lastBin/7;
if ((lastBin == 6 || lastBin == 13) && !gameOver())
{
return true;
}
if (lastBinEmpty && lastBinOnYourSide && lastBin != 6 && lastBin != 13)
{
int mancalaBin = mancalaOf(bin);
int neighborBin = neighborOf(lastBin);
state[mancalaBin] += state[neighborBin] + 1;
state[neighborBin] = 0;
state[lastBin] = 0;
}
if (gameOver())
stonesToMancalas();
return false;
}
/**
* switches the board's current perspective so that the bottom and top rows
* switch.
*/
public void rotate()
{
int[] rotatedState = new int[state.length];
for (int i = 0; i < state.length; ++i)
rotatedState[(i+7)%14] = state[i];
state = rotatedState;
}
/**
* provides a nice String representation of a state
*
* @param state the state to convert to a String
* @return a nice String representation of the state
*/
public String toString() {
StringBuffer buffer = new StringBuffer();
// buffer.append(" 6 5 4 3 2 1 \n");
buffer.append("+---------------------------------------+\n");
buffer.append("| |");
for (int i = 12; i >= 7; --i) {
buffer.append(toString(state[i]));
}
buffer.append(" |\n");
buffer.append("|");
buffer.append(toString(state[13]));
buffer.append("-----------------------------|");
buffer.append(toString(state[6]));
buffer.append("\n");
buffer.append("| |");
for (int i = 0; i <= 5; ++i) {
buffer.append(toString(state[i]));
}
buffer.append(" |\n");
buffer.append("+---------------------------------------+\n");
buffer.append(" 0 1 2 3 4 5 \n");
return buffer.toString();
}
private int stonesCount(int bin1, int bin2) {
int stones = 0;
for (int i = bin1; i <= bin2; ++i)
stones += state[i];
return stones;
}
/* xxx private*/ void stonesToMancalas() {
state[6] += stonesCount(0,5);
state[13] += stonesCount(7,12);
for (int i = 0; i < 6; ++i) {
state[i] = 0;
state[i+7] = 0;
}
}
public int neighborOf(int bin) {
if (bin == 13)
return bin;
else
return 12-bin;
}
public int mancalaOf(int bin) {
return bin / 7 * 7 + 6;
}
private int[] arrayCopy(int[] source) {
if (source == null)
return null;
int[] sink = new int[source.length];
for (int i = 0; i < source.length; ++i)
sink[i] = source[i];
return sink;
}
private static String toString(int stones) {
if (stones < 10)
return " " + stones + " |";
else
return " " + stones + " |";
}
GameState(int[] state) {
this.state = state;
}
}