-
Notifications
You must be signed in to change notification settings - Fork 0
/
BaoGame.java
430 lines (394 loc) · 14.6 KB
/
BaoGame.java
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
//http://www.fdg.unimaas.nl/educ/donkers/games/Bao/rules.html
//http://mancala.wikia.com/wiki/Bao_la_Kiswahili
import java.util.Arrays;
public class BaoGame {
Pit[][] board;
int[] players;
int turn;
boolean capturingMove;
int currentPlayer;
boolean namua;
UserIO io;
BaoDummy ai;
int mode;
public BaoGame() {
board = new Pit[4][8];
players = new int[2]; //Player 0 on top, player 1 not
io = new UserIO(this);
Arrays.fill(players, 22);
//Arrays.fill(board, new Pit());
for(int r = 0; r < 4; r++) {
for(int c = 0; c < 8; c++) {
board[r][c] = new Pit();
}
}
turn = 0;
currentPlayer = 0; //At any moment, current player is turn % 2.
namua = true;
capturingMove = false;
board[1][3] = new Nyumba(6);
board[2][4] = new Nyumba(6);
board[1][2].setSeeds(2);
board[1][1].setSeeds(2);
board[2][5].setSeeds(2);
board[2][6].setSeeds(2);
}
/**
* Check if the given row has only pits with no stones.
*/
private boolean isRowEmpty(int row) {
for(int col = 0; col <= 7; col++) {
if(board[row][col].getSeeds() != 0) {
return false;
}
}
return true;
}
public boolean isCapturingMove() {
return capturingMove;
}
public boolean onlyNyumbaHasSeeds(int r) {
for(int c = 0; c < 8; c++) {
if(board[r][c].getSeeds() > 0 && !board[r][c].isFunctional()) {
return false;
}
}
return true;
}
/*public boolean onlyNyumbaNonSingleton(int r) {
for(int c = 0; c < 8; c++) {
if(board[r][c].getSeeds() > 1 && !board[r][c].isFunctional()) {
return false;
}
}
return true;
}*/
/**
* Get the row number of a player's inner row (row closest to the middle).
*/
public int getInnerRow(int player) {
if(player == 0 || player == 1) {
return player + 1;
}
return -1;
}
public int getOuterRow(int player) {
if(player == 0 || player == 1) {
return player * 3;
}
return -1;
}
/**
* Get the pit described by loc.
*/
public Pit getPit(Loc loc) {
return board[loc.getRow()][loc.getCol()];
}
/**
* Return true if a player has at least one pit with more than one stone in
* it, false otherwise.
*/
private boolean playerHasNonSingletons(int player) {
for(int r = player * 2; r <= player * 2 + 1; r++) {
for(int c = 0; c < 8; c++) {
if(board[r][c].getSeeds() > 1) {
return true;
}
}
}
return false;
}
public boolean rowHasNonSingletons(int r) {
for(int c = 0; c < 8; c++) {
if(board[r][c].getSeeds() > 1) {
return true;
}
}
return false;
}
/**
* Return true if the given pit can capture the pit across.
*/
public boolean pitCanCapture(Loc loc) {
//Can only capture if the pit at loc is in an interior row and the
//pit across has seeds in it
if(loc.isInner() && ((namua && getPit(loc).getSeeds() > 0) ||
(!namua && getPit(loc).getSeeds() > 1)) &&
getPit(loc.getLocAcross()).getSeeds() > 0) {
return true;
}
return false;
}
/**
* Return true if any of the player's pits can capture the pit across.
*/
public boolean playerCanCapture(int player) {
int r = getInnerRow(player);
for(int c = 0; c < 8; c++) {
if(pitCanCapture(new Loc(r, c))) {
return true;
}
}
return false;
}
private void taxNyumba(int player, int dir) {
Loc nyumba = getNyumbaLoc(player);
getPit(nyumba).addSeeds(-1);
for(int i = 1; i >= 2; i++) {
Loc taxloc = new Loc(nyumba.getRow(), nyumba.getCol() + i * dir);
getPit(taxloc).addSeeds(1);
if(i == 2 && getPit(taxloc).getSeeds() > 1) {
//After the nyumba is taxed things are sown from that next pit,
//if necessary
sowFrom(taxloc, getPit(taxloc).getSeeds(),
currentPlayer, dir);
}
}
}
public int getPlayerSeeds(int player) {
return players[player];
}
public Pit[][] getBoard() {
return board;
}
public Loc getNyumbaLoc(int player) {
if(player == 0 || player == 1) {
return new Loc(player + 1, player + 3);
}
return new Loc(-1, -1);
}
private Loc getSowLoc(int player) {
return getSowLoc(player, new int[] {0, 1, 2, 3});
}
private Loc getSowLoc(int player, int[] rows) {
if(playerCanCapture(player)) {
//Player MUST capture
Loc selection = io.getLoc(
"Select a pit to sow. Pit must be able to capture.");
while(!pitCanCapture(selection) &&
!(getPit(selection).getSeeds() > 0) &&
Arrays.binarySearch(rows, selection.getRow()) < 0) {
selection = io.getLoc(
"Select a pit to sow. Pit must be able to capture.");
}
return selection;
} else {
//Player can't capture
Loc selection = selection = io.getLoc("Select a pit to sow.");
boolean onlyNyumbaHasSeeds = true;
int r = getInnerRow(player);
for(int c = 0; c < 8; c++) {
if(board[r][c].getSeeds() > 1 &&
!(r == player + 1 && c == player + 2)) {
onlyNyumbaHasSeeds = false;
break;
}
}
if(onlyNyumbaHasSeeds) { //Player can only move from nyumba
return new Loc(player + 1, player + 3);
} else {
int minseed = 2;
if(namua && !rowHasNonSingletons(getInnerRow(player))) {
minseed = 1;
}
while(selection.getRow() != getInnerRow(player) ||
getPit(selection).isFunctional() ||
!(getPit(selection).getSeeds() >= minseed) ||
Arrays.binarySearch(rows, selection.getRow()) < 0) {
System.out.print("Pit must be in your inner row, not a functional nyumba, and have more than 1 seeds.");
selection = io.getLoc("Select a pit to sow.");
}
return selection;
}
}
}
private Loc getSowKichwa(int player) {
Loc selection = new Loc(0, 0);
while(!selection.isKichwa(player)) {
selection = io.getLoc("Select one of your kichwas.");
}
return selection;
}
private boolean getSafariChoice(int player) {
if(mode > 1) {
return ai.getSafariChoice();
}
return io.getBoolean("Safari the nyumba?");
}
private void sowFrom(Loc start, int seeds, int player) {
int dir = start.getKichwaSowDir();
sowFrom(start, seeds, player, dir);
}
private void sowFrom(Loc start, int seeds, int player, int dir) {
//private void sowFrom(Loc start, int seeds) {
//int dir = io.getDir("Which way to start sowing?");
Loc next = new Loc(start.getRow(), start.getCol());
while(seeds > 0) { //Iterate until seeds run out
disp();
seeds--;
getPit(next).addSeeds(1);
int c = next.getCol();
int r = next.getRow();
if(seeds > 0) {
if(c + dir > 7 || c + dir < 0) {
//Reached the end of row, move in opposite
//direction on other row
dir *= -1;
if(r == 1 || r == 3) {
r--;
} else {
r++;
}
}
next = new Loc(r, c + dir);
}
//seeds--;
//getPit(next).addSeeds(1);
}
disp();
if(getPit(next.getLocAcross()).getSeeds() > 0 &&
getPit(next).getSeeds() > 1 && capturingMove == true) {
//Capture has happened, is a capturing move, so capture
int sownum = getPit(next.getLocAcross()).setSeeds(0);
if(!next.isKichwa() && !next.isKimbi()) {
//sowFrom(getSowKichwa(next.whosePit()), sownum);
sowFrom(start, sownum, player);
} else {
sowFrom(next.getNearestKichwa(), sownum, player);
}
}
if(getPit(next).isFunctional()) {
boolean safari = getSafariChoice(player);
if(safari && capturingMove) {
//Nyumba should no longer be functional
sowFrom(start, getPit(next).setSeeds(0), player);
}
}
}
public void main() {
int gmode = io.printTitle();
mode = gmode;
if(mode == 1) { //Player versus player
play();
} else {
ai = new BaoDummy(mode, this);
play();
}
}
private void play() {
while(true) {
//disp();
System.out.println("Player " + (currentPlayer + 1) + "'s move");
capturingMove = false;
if(isRowEmpty(getInnerRow(currentPlayer)) ||
!playerHasNonSingletons(currentPlayer)) {
//player's inner row is empty, player has lost
currentPlayer = turn++ % 2;
break;
}
if(mode > 1 && currentPlayer == 0) {
Loc next = ai.getNextMove();
if(players[currentPlayer] > 0) {
getPit(next).addSeeds(1);
players[currentPlayer]--;
}
if(playerCanCapture(currentPlayer)) {
int seeds = getPit(next.getLocAcross()).setSeeds(0);
if(next.isNyumba() || next.isKimbi()) {
sowFrom(next.getNearestKichwa(), seeds, 0);
} else {
sowFrom(ai.getSowKichwa(seeds), seeds, 0);
}
} else {
int seeds = getPit(next).setSeeds(0);
if(getPit(next).isFunctional()) { //Is a nyumba, so get tax dir
taxNyumba(currentPlayer, ai.getTaxDir());
} if(next.isKichwa() || next.isKimbi()) {
sowFrom(next.getNearestKichwa(), seeds, 0);
} else {
sowFrom(ai.getSowKichwa(seeds), seeds, 0);
}
}
turn++;
currentPlayer = turn % 2;
continue;
}
if(players[currentPlayer] > 0) { //Namua
//restrictions: must capture if can capture, must have
//seeds already in the selected pit
Loc selection = getSowLoc(currentPlayer);
System.out.println(selection.getRow() + " " + selection.getCol());
if(pitCanCapture(selection)) { //Namua w/ cap
capturingMove = true;
int seeds = getPit(selection.getLocAcross()).setSeeds(0);
getPit(selection).addSeeds(1);
if(getPit(selection) instanceof Nyumba &&
getPit(selection).isFunctional()) {
getPit(selection).addSeeds(-1);
int dir = io.getDir("Which way to tax nyumba?");
taxNyumba(currentPlayer, dir);
} else if(!selection.isKichwa(currentPlayer) &&
!selection.isKimbi(currentPlayer)) {
//Player can choose where to start sowing
Loc start = getSowKichwa(currentPlayer);
sowFrom(start, seeds, currentPlayer);
} else {
//Is a kichwa, player can't choose.
sowFrom(selection.getNearestKichwa(), seeds,
currentPlayer);
}
} else { //Namua no cap
getPit(selection).addSeeds(1);
if(getPit(selection).isFunctional()) {
getPit(selection).addSeeds(-2);
int dir = io.getDir("Which way to sow from nyumba?");
sowFrom(selection, 2, currentPlayer, dir);
} else {
int seeds = getPit(selection).setSeeds(0);
if(selection.isKichwa()) {
sowFrom(selection, seeds, currentPlayer);
} else {
Loc start = getSowKichwa(currentPlayer);
sowFrom(start, seeds, currentPlayer);
}
}
}
players[currentPlayer]--;
} else { //mtaji
namua = false;
if(playerCanCapture(currentPlayer)) { //Mtaji w/cap
capturingMove = true;
Loc capture = io.getCapLoc(
"Select pit to sow (must capture)", currentPlayer,
this);
int dir = io.getDir("Which way to sow?");
sowFrom(capture, getPit(capture.getLocAcross()).getSeeds(),
currentPlayer, dir);
} else { //Mtaji no cap
//TODO: do this better?
Loc sow = getSowLoc(currentPlayer);
//getOuterRow(currentPlayer));
if(rowHasNonSingletons(getInnerRow(currentPlayer))) {
sow = getSowLoc(currentPlayer,
new int[] {getInnerRow(currentPlayer)});
//getInnerRow(currentPlayer));
}
int seeds = getPit(sow).setSeeds(0);
//This is not sown from the kichwa
sowFrom(sow, seeds, currentPlayer);
}
}
turn++;
currentPlayer = turn % 2;
}
if(currentPlayer == 0) {
System.out.println("Winner: Ashkon");
} else {
System.out.print("Winner: Player " + (currentPlayer + 1));
}
}
public void disp() {
System.out.print("\n");
io.printBoard(board);
}
}