-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDataModel.java
457 lines (380 loc) · 9.22 KB
/
DataModel.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
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
import java.awt.Color;
import java.awt.Font;
import java.util.ArrayList;
import javax.swing.JFrame;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
/**
*
* @author Samuel Boulanger
* @author Cindy Ho
* @author Aldo Barrientos
* holds/manipulates pits inside holes
*
*/
public class DataModel
{
ArrayList<Hole> holes;
ArrayList<Hole> pastHoles;
ArrayList<ChangeListener> listeners;
private int nStones;
private boolean justRepeated;
private Pit A1, A2, A3, A4, A5, A6;
private Mancala A;
private Pit B1, B2, B3, B4, B5, B6;
private Mancala B;
private Player player;
public static int mancalaAPosition = 0;
public static int mancalaBPosition = 7;
/**
*
* helps determined whether game should
* continue or not and who finished
*
*/
enum GAMECONDITION
{
CONTINUE, AFINISHED, BFINISHED ;
}
/**
* obj. which has an array of holes
* @param nStones the amount of stones
* each hole will start with
*/
public DataModel(int nStones)
{
this.nStones = nStones;
this.holes = new ArrayList<Hole>();
this.listeners = new ArrayList<ChangeListener>();
A = new Mancala(0,0);
this.holes.add(A);
B1 = new Pit(nStones,1);
this.holes.add(B1);
B2 = new Pit(nStones,2);
this.holes.add(B2);
B3 = new Pit(nStones,3);
this.holes.add(B3);
B4 = new Pit(nStones,4);
this.holes.add(B4);
B5 = new Pit(nStones,5);
this.holes.add(B5);
B6 = new Pit(nStones,6);
this.holes.add(B6);
B = new Mancala(0,7);
this.holes.add(B);
A1 = new Pit(nStones,8);
this.holes.add(A1);
A2 = new Pit(nStones,9);
this.holes.add(A2);
A3 = new Pit(nStones,10);
this.holes.add(A3);
A4 = new Pit(nStones,11);
this.holes.add(A4);
A5 = new Pit(nStones,12);
this.holes.add(A5);
A6 = new Pit(nStones,13);
this.holes.add(A6);
this.pastHoles = new ArrayList<Hole>();
cloneState();
player = Player.PLAYERA;
}
/**
* controls the algorithms which simulates a move in the
* game and manipulates the array
* @param position the position where you start your move
*/
public void move(int position){
justRepeated = false;
System.out.println(position);
GAMECONDITION current;
Player winner = null;
int addPosition = 0; //start position
int amount_stones = this.holes.get(position).getStones(); //amount of stones in whatever hole you're in
this.holes.get(position).setStones(0); //set the amount of current hole to 0
for (int i = 0; i < amount_stones;i++)//do it until you run out of stones
{
addPosition = position + i + 1; //to next hole
if (addPosition > 13) //if past mancala a
{
addPosition %= 14; //sets back to 0
}
if (isMancala(addPosition)) //if current is mancala
{ System.out.println("is mancala");
//if you're player a & on mancala 0 then skip or if you're player b & on mancala 7 then skip
if((player == Player.PLAYERA && addPosition == 7) || (player == Player.PLAYERB && addPosition == 0)) {
amount_stones++;
continue;
}
}
this.holes.get(addPosition).setStones(this.holes.get(addPosition).getStones()+1); //adding one
}
if(isMancala(addPosition) && isPlayerHole(player, holes.get(addPosition)))
{
justRepeated = true;
togglePlayer();
}
else if (wasEmpty(addPosition))
{
if(isPlayerHole(player, holes.get(addPosition)))
{
int oppStones = this.holes.get(14 - addPosition).getStones();
int currStones = this.holes.get(addPosition).getStones();
this.holes.get(14 - addPosition).setStones(0);
holes.get(addPosition).setStones(currStones + oppStones);
}
}
current = gameIsDone();
if(current != GAMECONDITION.CONTINUE)
{
System.out.println("game is done");
if(current == GAMECONDITION.BFINISHED)
{
holes.get(0).setStones(getRemainingCount(current) + holes.get(0).getStones());
}
else
{
holes.get(7).setStones(getRemainingCount(current) + holes.get(0).getStones());
}
for(int i = 0; i < holes.size(); i++)
{
if(i == 0 || i == 7)
continue;
holes.get(i).setStones(0);
}
winner = whoWon();
Board.dislayWinner(winner);
}
updateBoard();
}
/**
* saves the current states before a move to pastHoles
*/
public void saveState(){
for (int i = 0; i < this.holes.size(); i++){
this.pastHoles.set(i, this.holes.get(i).clone());
}
}
/**
* creates the initial copy of holes arraylist
*/
public void cloneState(){
for (Hole h : holes){
this.pastHoles.add(h.clone());
}
}
/**
* copies holes to pastHoles before a move
*/
public void restorePastData(){
for (int i = 0; i < this.holes.size(); i++){
this.holes.set(i, this.pastHoles.get(i));
}
}
/**
* checks if the current game has ended
* @return the game condition which determines
* if game is done
*/
public GAMECONDITION gameIsDone()
{
int aEmptyCount = 0;
int bEmptyCount = 0;
for(int i = 1; i < 7; i++)
{
if(holes.get(i).getStones() == 0)
bEmptyCount++;
}
for(int i = 8; i < 13; i++)
{
if(holes.get(i).getStones() != 0)
aEmptyCount++;
}
if(aEmptyCount == 6)
return GAMECONDITION.AFINISHED;
else if(bEmptyCount == 6)
return GAMECONDITION.BFINISHED;
return GAMECONDITION.CONTINUE;
}
/**
* calculates the remaining stones in a player's pits
* @param player the player whose stone you want to check
* @return amount of stones a player has left
*/
public int getRemainingCount(GAMECONDITION player)
{
System.out.println("get remaining");
int j = 0;
int k = 0;
int count = 0;
if(player == GAMECONDITION.AFINISHED)
{
j = 1;
k = 7;
}
else
{
j = 8;
k =14;
}
for(int i = j; j < k; j++)
{
count += holes.get(i).getStones();
}
return count;
}
/**
* checks if current hole is mancala
* @param position the given Hole to be checked
* @return true/false if the position is a mancala
*/
public boolean isMancala(int position){
if (this.holes.get(position) instanceof Mancala) {
return true;
}
return false;
}
/**
* counts up all the stones in each player's mancala
* @return which player won or null if there's a tie
*/
public Player whoWon()
{
if(holes.get(0).getStones() < holes.get(7).getStones())
{
System.out.println("player b won");
return Player.PLAYERB;
}
else if(holes.get(0).getStones() > holes.get(7).getStones())
{
System.out.println("player a won");
return Player.PLAYERA;
}
//if tie
return null;
}
/**
* checks if the hole was empty before you dropped a stone
* @param position the current stone to be checked
* @return true/false if the position was empty
*/
public boolean wasEmpty(int position){
return (this.holes.get(position).getStones() == 1);
}
/**
*
* @param position the hole postion
* @return if the hole is empty
*/
public boolean isEmpty(int position){
return (this.holes.get(position).getStones() == 0);
}
/**
* adds a change listener to listeners
* @param c the change listener to attach
*/
public void attachListener(ChangeListener c){
listeners.add(c);
}
/**
* notifies a state change to all any board
*/
public void updateBoard(){
for (ChangeListener l : listeners)
{
l.stateChanged(new ChangeEvent(this));
}
}
/**
* gets the amount of stones in a given Hole
* @param position the Hole to be checked
* @return the amount of stones in a given Hole
*/
public int getHoleAmount(int position){
return this.holes.get(position).getStones();
}
/**
* gets the amount of starting stones
* @return the amount of starting stones
*/
public int getAmtStart(){
return nStones;
}
/**
* gets the arraylist of holes
* @return holes the arraylist of holes
*/
public ArrayList<Hole> getData()
{
return this.holes;
}
/**
* gets the current player
* @return the current player
*/
public Player getPlayer()
{
return player;
}
/**
* shifts from one player to another after
* a turn have been taken
*/
public void togglePlayer()
{
System.out.println("toggle");
if(player == Player.PLAYERA)
{
this.player = Player.PLAYERB;
}
else
{
this.player = Player.PLAYERA;
}
}
/**
* gets the other player who is not currently playing
* @return the other player
*/
public Player getOtherPlayer()
{
if(player == Player.PLAYERA)
return Player.PLAYERB;
return Player.PLAYERA;
}
/**
* holds the pastHoles for undo
* @return pastHoles the prev board state
*/
public ArrayList<Hole> getPastData(){
return this.pastHoles;
}
/**
* checks if player on a hole that belonds to it
* @param p the current player
* @param h the current hole
* @return true/false if the whole is the player's
*/
public boolean isPlayerHole(Player p, Hole h)
{
if(p == Player.PLAYERB)
{
if(h.getArrPos() <= 7 && h.getArrPos() > 0)
return true;
}
else if(p == Player.PLAYERA)
{
if(h.getArrPos() > 7 && h.getArrPos() < 14 || h.getArrPos() == 0)
return true;
}
return false;
}
/**
* helps check if a player just went again so that
* undo doesn't toggle
* @return true/false if the player just went again
*/
public boolean getJustRepeated()
{
return justRepeated;
}
}