-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMancalaModel.java
More file actions
439 lines (382 loc) · 13.2 KB
/
Copy pathMancalaModel.java
File metadata and controls
439 lines (382 loc) · 13.2 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
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
import java.util.*;
import javax.swing.event.*;
/**
* The class that represents teh Model of the MVC design pattern. Stores game data and views.
* @author: Hung Phan
* @author: Lien Cao
* @author: Wendy Ta
* @version: 1.0 11/28/25
*/
public class MancalaModel {
private ArrayList<ChangeListener> listeners; //store views
private ArrayList<Pit> allPits;
private HashMap<String, Pit> nameToPit; //map the name of each pit to each Pit object
private HashMap<String, String> oppositePits;
private int number;
private String curPlayer = "A"; // 11/16
private String winner = "None";
private boolean freeTurn = false;
private Stack<CurrentGameInfo> stack = new Stack<>();
private BoardStyle boardStyle; //for different board styles 11/26/2025
private int undoA = 0, undoB = 0;
private boolean lastMoveUndo = false;
/**
* Constructs a model of the game
*/
public MancalaModel() {
this.listeners = new ArrayList<>();
this.allPits = new ArrayList<>();
for (int i=0; i<14; i++) {
allPits.add(i, null); //initialize with null elements
}
nameToPit = new HashMap<>();
oppositePits = new HashMap<>();
oppositePits.put("A1", "B6"); //initialize opposite pits
oppositePits.put("A2", "B5");
oppositePits.put("A3", "B4");
oppositePits.put("A4", "B3");
oppositePits.put("A5", "B2");
oppositePits.put("A6", "B1");
oppositePits.put("B1", "A6");
oppositePits.put("B2", "A5");
oppositePits.put("B3", "A4");
oppositePits.put("B4", "A3");
oppositePits.put("B5", "A2");
oppositePits.put("B6", "A1");
this.boardStyle = new BoardStyleDefault();
}
/**
* Method to reset the board game, set the number of stones for each pit
*/
public void resetBoard() {
for( Pit pit : allPits) {
if (pit != null && !pit.getName().equals("leftStore") && !pit.getName().equals("rightStore")) {
pit.setNumStones(number);
}
}
notifyViews();
}
/**
* The method that allows user to undo their move in the game
* @param p the stack of pits that keep track pits when playing
*/
public void undo() {
if(curPlayer.equals("A") && undoA >= 3 || curPlayer.equals("B") && undoB >=3 || lastMoveUndo) {
System.out.println("Cannot undo again.");
return;
}
if (stack.isEmpty()) { //nothing to undo if nothing in stack
return;
}
CurrentGameInfo lastInfo = stack.pop(); //retrieve the info from the last move
allPits = lastInfo.getAllPitsCopy(); //update pits/player/turn to the info from the last move
curPlayer = lastInfo.getCurrentPlayer();
freeTurn = lastInfo.getFreeTurn();
for (Pit pit : allPits) {
//Update 11/28/2025
Pit curr = nameToPit.get(pit.getName());
if(curr != null){
curr.setNumStones(pit.getNumStones());
allPits.set(allPits.indexOf(pit), curr);
}
}
if(curPlayer.equals("A")) {
undoA++;
} else {
undoB++;
}
lastMoveUndo = true;
notifyViews();
}
/**
* The methods that show how the game changes after a pit is clicked
* @param p a pit that is being to clicked
*/
public void gameRun(Pit p) { //p = the pit that was clicked
stack.push(new CurrentGameInfo(allPits, curPlayer, freeTurn));
int numStones = p.getNumStones();
if (numStones == 0) { //do nothing if no stones
return;
}
p.setNumStones(0); //empty current pit
Pit currentPit = p;
while (numStones > 0) {
Pit nextPit = getNextPit(currentPit);
// If there are more stones to go past your own Mancala,
// continue placing them into the opponent's pits.
// However, skip your opponent's Mancala
if ((curPlayer.equals("A") && nextPit.getName().equals("leftStore")) ||
(curPlayer.equals("B") && nextPit.getName().equals("rightStore"))) {
nextPit = getNextPit(nextPit);
}
// If the last stone you drop is in an empty pit on your side
// you get to take that stone and all of your opponents stones
// that are in the opposite pit
if (numStones == 1) { //check if last stone --> only 1 stone
String nextPitLetter = nextPit.getName().substring(0, 1);
if (nextPit.getNumStones() == 0 && nextPitLetter.equals(curPlayer)) { //check if in empty pit on your side
Pit oppositePit = getOppositePit(nextPit);
int num = oppositePit.getNumStones();
oppositePit.setNumStones(0);
nextPit.setNumStones(0);
Pit store;
if(curPlayer.equals("A")) {
store = nameToPit.get("rightStore");
} else {
store = nameToPit.get("leftStore");
}
store.setNumStones(store.getNumStones() + num + 1);
setFreeTurn(false);
break;
}
else if ((curPlayer.equals("A") && nextPit.getName().equals("rightStore")) ||
(curPlayer.equals("B") && nextPit.getName().equals("leftStore"))) {
// If the last stone you drop is in your own Mancala, you get a free turn
setFreeTurn(true);
}
else {
setFreeTurn(false);
}
}
lastMoveUndo = false;
nextPit.addStone();
numStones--;
currentPit = nextPit;
}
//The player who still has stones on his side of the board when the game ends
//captures all of those pieces and place them in his Mancala.
//The player who has the most stones in his Mancala wins.
String captureSide = checkEmpty();
if (captureSide.equals("None")) {
//do nothing, just continue playing
}
else {
setWinner(capturePieces(captureSide)); //player whose side is not empty captures remaining pieces, set the winner
}
//change players after each move
if (curPlayer.equals("A") && !getFreeTurn()) {
setCurrentPlayer("B");
} else if (curPlayer.equals("B") && !getFreeTurn()) {
setCurrentPlayer("A");
} else {
setCurrentPlayer(curPlayer);
}
notifyViews();
}
/**
* Sets winner of the game
* @param winner the winner
*/
public void setWinner(String winner) {
this.winner = winner;
}
/**
* Gets winner of the game
* @return the winner
*/
public String getWinner() {
return this.winner;
}
/**
* The method that check who is the winner
* @param captureSide the side of the board game
* @return the winner
*/
public String capturePieces(String captureSide) {
String storeToUpdate = "";
String otherStore = "";
if (captureSide.equals("A")) { //A's store = right store
storeToUpdate = "rightStore";
otherStore = "leftStore";
}
else {
storeToUpdate = "leftStore";
otherStore = "rightStore";
}
int totalStones = 0;
for (Pit p : allPits) { //get the total number of stones left on the board, excluding the stores
if (!p.getName().equals("leftStore") && !p.getName().equals("rightStore")) {
totalStones += p.getNumStones();
p.setNumStones(0); //set everything to 0
}
}
Pit store1 = nameToPit.get(storeToUpdate);
Pit store2 = nameToPit.get(otherStore);
store1.setNumStones(store1.getNumStones() + totalStones); //update the store
if (store1.getNumStones() > store2.getNumStones() && store1.getName().equals("rightStore") ||
store1.getNumStones() < store2.getNumStones() && store2.getName().equals("rightStore")) { //A has more stones --> A wins
System.out.println("Player A wins!");
return "A";
}
else {
System.out.println("Player B wins!");
return "B";
}
}
/**
* Checks if either side of the board is empty
* @return the side that is not empty, or none if both sides are not empty
*/
public String checkEmpty() {
boolean AEmpty = true;
boolean BEmpty = true;
for (Pit p : allPits) {
String pitLetter = p.getName().substring(0,1);
if (pitLetter.equals("A") && p.getNumStones() != 0) { //check if any of A's pits are not empty
AEmpty = false;
}
else if (pitLetter.equals("B") && p.getNumStones() != 0) { //check if any of B's pits are not empty
BEmpty = false;
}
}
if (AEmpty) {
return "B"; //return the side that is NOT empty, the side that gets to capture remaining pieces
}
else if (BEmpty) {
return "A";
}
return "None"; //neither side is empty
}
/**
* Gets the opposite pit
* @param p the pit that being checked
* @return the opposite pit
*/
public Pit getOppositePit(Pit p) {
// index: 00 01 02 03 04 05 06 07 08 09 10 11 12 13
// pit: left A1 A2 A3 A4 A5 A6 right B1 B2 B3 B4 B5 B6
int index = allPits.indexOf(p);
if (index == 0 || index == 7) { //check if pit is a store
return null;
}
String oppositeName = this.oppositePits.get(p.getName());
int oppositeIndex = allPits.indexOf(nameToPit.get(oppositeName));
return allPits.get(oppositeIndex);
}
/**
* The method that assign each pit with a name
* @param name the name is being assigned to a pit
* @param pit the pit that match with that name
*/
public void addToNameMap(String name, Pit pit) {
nameToPit.put(name, pit);
}
/**
* The method that assign peach pit with an index
* @param idx an index
* @param pit the pit
*/
public void addToAllPits(int idx, Pit pit) {
allPits.set(idx, pit);
}
/**
* The method to get all pits in the game
* @return a list of the pits
*/
public ArrayList<Pit> getAllPits() {
return this.allPits;
}
/**
* Gets a pit using its name
* Used when drawing the board to get the number of stones using its name
* @return a map that has connection between the pit and its name
*/
public HashMap<String, Pit> getNameToPit() {
return this.nameToPit;
}
/**
* Set initial number of stones
* @param n the number of stones
*/
public void setNumber(int n) {
this.number = n;
resetBoard();
notifyViews();
}
/**
* Gets initial number of stones to draw
* @return the number of stones
*/
public int getNumber() {
return number;
}
/**
* Gets the next pit
* @param current the current pit
* @return the next pit
*/
public Pit getNextPit(Pit current) {
if(current == null){
return null;
}
int index = allPits.indexOf(current);
if(index == -1){
return null;
}
return allPits.get((index + 1) % allPits.size());
}
/**
* Add listener views
* @param listener
*/
public void addChangeListener(ChangeListener listener) {
listeners.add(listener);
}
/**
* Notify each view of the change in this model
*/
public void notifyViews() {
ChangeEvent event = new ChangeEvent(this);
for (ChangeListener listener : listeners) {
listener.stateChanged(event);
}
}
/**
* Gets the current player
* @return the current player
*/
public String getCurrentPlayer() {
return curPlayer;
}
/**
* Sets the current player
* @param player the current player
*/
public void setCurrentPlayer(String player) {
this.curPlayer = player;
notifyViews();
}
/**
* Gets the free turn
* @return true, false otherwise
*/
public boolean getFreeTurn() {
return this.freeTurn;
}
/**
* Set free turn for the player
* @param value true or false
*/
public void setFreeTurn(boolean value) {
this.freeTurn = value;
}
/**
* Sets the board style
* @param style the style that being choose
*/
public void setBoardStyle(BoardStyle style) {
if(style == null){
boardStyle = new BoardStyleDefault(); // default style
return;
}
this.boardStyle = style;
notifyViews();
}
/**
* Gets the board style
* @return the new board style
*/
public BoardStyle getBoardStyle() {
return this.boardStyle;
}
}