-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMancalaGameWithoutSteal.java
283 lines (245 loc) · 8.51 KB
/
MancalaGameWithoutSteal.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
import java.util.Scanner;
/**
* Represents a game of Mancala.
*/
public class MancalaGameWithoutSteal {
/**
* The number of rows on the game board.
*/
private static final int ROWS = 2;
/**
* The number of columns on the game board.
*/
private static final int COLUMNS = 6;
/**
* The index of player 1.
*/
private static final int PLAYER1 = 0;
/**
* The index of player 2.
*/
private static final int PLAYER2 = 1;
/**
* The 2D array representing the stones on the game board.
*/
private int[][] board = new int[ROWS][COLUMNS];
/**
* The stores of each player.
*/
private int[] store = new int[2];
/**
* Constructor for MancalaGame.
* Initializes the game board.
*/
public MancalaGameWithoutSteal() {
initializeBoard();
}
/**
* Initializes the game board.
* Sets all pits to 4 stones and both stores to 0.
*/
public void initializeBoard() {
// initialize the board
for (int i = 0; i < ROWS; i++) {
for (int j = 0; j < COLUMNS; j++)
board[i][j] = 4;
}
// initialize the store
store[PLAYER1] = 0;
store[PLAYER2] = 0;
}
/**
* Prints the current state of the game board to the console.
*/
public void printBoard() {
System.out.println("+----+----+----+----+----+----+----+----+");
System.out.print("| |");
for (int i = 0; i < COLUMNS; i++)
System.out.printf("%3d |", board[0][i]);
System.out.println(" |");
System.out.println("| " + String.format("%2d", store[0]) + " |----+----+----+----+----+----| "
+ String.format("%2d", store[1]) + " |");
System.out.print("| |");
for (int i = 0; i < COLUMNS; i++)
System.out.printf("%3d |", board[1][i]);
System.out.println(" |");
System.out.println("+----+----+----+----+----+----+----+----+");
}
/**
* Prints the game board with a header.
* The header includes the column numbers.
*/
private void printBoardWithHeader() {
System.out.println(" | 1 | 2 | 3 | 4 | 5 | 6 |");
printBoard();
}
/**
* Checks if the game is over.
* The game is over if all the pits of one player are empty.
*
* @return true if the game is over, false otherwise
*/
public boolean isGameOver() {
// if all the pits of one player are empty, the game is over
boolean player1Empty = true;
boolean player2Empty = true;
for (int i = 0; i < COLUMNS; i++) {
if (board[PLAYER1][i] != 0)
player1Empty = false;
if (board[PLAYER2][i] != 0)
player2Empty = false;
}
return player1Empty || player2Empty;
}
/**
* Plays the game for a given player and pit number.
*
* @param playerIndex The index of the player.
* @param pitNumber The number of the pit.
* @return true if the player gets a second turn, false otherwise.
*/
public boolean play(int playerIndex, int pitNumber) {
// convert pitNumber to index
pitNumber--;
// get the number of stones in the pit
int stones = board[playerIndex][pitNumber];
// empty the pit
board[playerIndex][pitNumber] = 0;
int row = playerIndex;
// distribute the stones
// if the player is player 1, move towareds left
// second turn
boolean secondTurn = false;
while (stones > 0) {
// player 1
if (row == 0) {
int i = pitNumber - 1;
// distribute the stones
while (i >= 0 && stones > 0) {
board[row][i]++;
stones--;
i--;
}
// if there are still stones left, put them in the store
// and it is their own mancala
if (row == playerIndex) {
if (stones > 0) {
store[row]++;
stones--;
if (stones == 0) {
secondTurn = true;
}
}
}
row = PLAYER2;
pitNumber = -1;
} else {
// distribute the stones
int i = pitNumber + 1;
while (i < COLUMNS && stones > 0) {
board[row][i]++;
stones--;
i++;
}
// if there are still stones left, put them in the store
// and it is their own mancala
if (row == playerIndex) {
if (stones > 0) {
store[row]++;
stones--;
if (stones == 0) {
secondTurn = true;
}
}
}
row = PLAYER1;
pitNumber = COLUMNS;
}
}
return secondTurn;
}
/**
* Determines the winner of the game.
*
* @return The index of the winning player, or -1 if it's a draw.
*/
public int winner() {
// it is guarranteed that the game is over
// count total number of stones in each player's store
int player1Stones = store[PLAYER1];
int player2Stones = store[PLAYER2];
// add the stones in the pits to the stores
for (int i = 0; i < COLUMNS; i++) {
player1Stones += board[PLAYER1][i];
player2Stones += board[PLAYER2][i];
}
if (player1Stones > player2Stones)
return PLAYER1;
else if (player1Stones < player2Stones)
return PLAYER2;
else
return -1;
}
/**
* The main method that runs the Mancala game.
*
* @param args The command line arguments.
*/
public static void main(String[] args) {
MancalaGameWithoutSteal game = new MancalaGameWithoutSteal();
// ask for user to input the pit number
Scanner input = new Scanner(System.in);
// print header for the game
System.out.println("+---------------------------------------------+");
System.out.println("| Mancala Game |");
System.out.println("+---------------------------------------------+");
System.out.println();
// ask for name of the player
System.out.print("Enter your name to continue: ");
String name = input.nextLine();
game.printBoardWithHeader();
int currentPlayer = 1; // user starts first
while (!game.isGameOver()) {
boolean secondTurn = false;
int pitNumber = 0;
// computer is taking turn
if (currentPlayer == 0) {
// randomly choose a pit (1 - 6)
pitNumber = (int) (Math.random() * COLUMNS) + 1;
// if the pit is empty, choose another pit
while (game.board[currentPlayer][pitNumber-1] == 0) {
pitNumber = (int) (Math.random() * COLUMNS) + 1;
}
System.out.println("Computer chooses pit " + pitNumber);
System.out.print("Press enter to continue...");
input.nextLine();
} else {
System.out.print(name + "! choose a pit [1-6]: ");
pitNumber = input.nextInt();
// if the input is invalid, or the pit is empty, ask for input again
while (pitNumber < 1 || pitNumber > 6 || game.board[currentPlayer][pitNumber - 1] == 0) {
System.out.print("Invalid input. Choose a (non-empty) pit [1-6]: ");
pitNumber = input.nextInt();
}
// discard the rest of the line
input.nextLine();
}
secondTurn = game.play(currentPlayer, pitNumber);
game.printBoard();
if (!secondTurn) {
// switch player
currentPlayer = (currentPlayer + 1) % 2;
}
System.err.println();
}
// print the winner
int winner = game.winner();
if (winner == -1)
System.out.println("It is a tie!");
else if (winner == 0)
System.out.println("Computer wins!");
else
System.out.println(name + " wins!");
input.close();
}
}