Repository navigation
Expand file tree
/
Copy pathState.java
More file actions
203 lines (170 loc) · 7.75 KB
/
Copy pathState.java
File metadata and controls
203 lines (170 loc) · 7.75 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
import java.util.ArrayList;
/**
*
* @author Suyash Misra
*/
public class State {
Pit[] pitA;
Pit[] pitB;
Stones mancalaA;
Stones mancalaB;
char Player;
void setPlayer(char player){
Player = player;
}
char getPlayer(){
return Player;
}
void getEmptied(){
if(Player == 'A'){ //empty B pits
for(int i = 0; i<6; i++){
if(pitB[i].getAmount()!=0){
mancalaB.addStones(pitB[i].takeAll());
}
}
}
else{ //empty A pits
for(int i = 0; i<6; i++){
if(pitA[i].getAmount()!=0){
mancalaA.addStones(pitA[i].takeAll());
}
}
}
}
public State(Pit[] pA, Pit[] pB, Stones manA, Stones manB, char plyr) {
pitA = new Pit[6];
pitB = new Pit[6];
for (int i = 0; i < 6; i++)
pitA[i] = new Pit(pA[i].getAmount());
for (int i = 0; i < 6; i++)
pitB[i] = new Pit(pB[i].getAmount());
mancalaA = new Stones();
mancalaB = new Stones();
mancalaA.setStones(manA.getAmount());
mancalaB.setStones(manB.getAmount());
Player = plyr;
}
public State(State obj) {
pitA = new Pit[6];
pitB = new Pit[6];
for (int i = 0; i < 6; i++)
pitA[i] = new Pit(obj.pitA[i].getAmount());
for (int i = 0; i < 6; i++)
pitB[i] = new Pit(obj.pitB[i].getAmount());
mancalaA = new Stones();
mancalaB = new Stones();
mancalaA.setStones(obj.mancalaA.getAmount());
mancalaB.setStones(obj.mancalaB.getAmount());
}
ArrayList<Integer> availableMoves(){
ArrayList<Integer> moves = new ArrayList<>();
if(Player == 'A'){
for(int i = 0; i < 6; i++){
if(pitA[i].getAmount()!=0){
moves.add(i);
}
}
}
else{
for(int i = 0; i < 6; i++){
if(pitB[i].getAmount()!=0){
moves.add(i);
}
}
}
return moves;
}
ArrayList<State> getChildren(){
ArrayList<State> children = new ArrayList<>();
for (int move : this.availableMoves()) {
State child = new State(this);
child.Turn(Player, move);
child.setPlayer(child.getPlayer() == 'A'?'B':'A');
children.add(child);
}
return children;
}
void Turn(char Player, int pitNumber){
int stones;
if(Player=='A'){ //player A will pick from anyone of a[] pits
stones=pitA[pitNumber].takeAll(); //pick up all stones in the specified pit
int i=pitNumber+1; //setting i to the next pit for depositing stones
boolean flag=false; //flag used to check whether to change sides while depositing
while(stones-->0){
if(i==6 && !flag){ //checking if i is indicating the mancala A, assuming 0 based pitNumber
mancalaA.addStones(1);
i=0; //setting i to 0 to start depositing in pit b[]
flag=true;
}
else if(flag){ //depositing on opponent pits
if(i==6){ //if i reaches the opponent mancala
i=0; //i is reset to 0 to point to starting of A's pit
flag = false;
if(stones==0 && pitA[i].getAmount()==0){ //if last stone in hand and i points an empty pit
mancalaA.addStones(pitB[5-i].takeAll()+1);//take the stone and opponent pit's stones and deposit in player mancala
}
else{
pitA[i].addStone();
// flag=false; //indicating player depositing on own side
i++;
}
}
else{
pitB[i].addStone();
i++;
}
}
else{ //if i indicates player's pits
if(stones==0 && pitA[i].getAmount()==0){ //if last stone in hand and i points an empty pit
mancalaA.addStones(pitB[5-i].takeAll()+1);//take the stone and opponent pit's stones and deposit in player mancala
}
else{
pitA[i].addStone();
i++;
}
}
}
}
else{ //player B will pick from any one of b[] pits
stones=pitB[pitNumber].takeAll(); //pick up all stones in the specified pit
int i=pitNumber+1; //setting i to the next pit for depositing stones
boolean flag=false; //flag used to check whether to change sides while depositing
while(stones-->0){
if(i==6 && !flag){ //checking if i is indicating the mancala B, assuming 0 based pitNumber
mancalaB.addStones(1);
i=0; //setting i to 0 to start depositing in pit a[]
flag=true;
}
else if(flag){ //depositing on opponent pits
if(i==6){ //if i reaches the opponent mancala
i=0; //mancala is skipped and i is reset to 0 to point to starting of b pit
flag = false;
if(stones==0 && pitB[i].getAmount()==0){ //if last stone in hand and i points an empty pit
mancalaB.addStones(pitA[5-i].takeAll()+1);
//take the stone and opponent pit's stones and deposit in player mancala
}
else{
pitB[i].addStone();
i++;
// flag=false; //indicating player depositing on own side
}
}
else{
pitA[i].addStone();
i++;
}
}
else{ //if i indicates player's pits
if(stones==0 && pitB[i].getAmount()==0){ //if last stone in hand and i points an empty pit
mancalaB.addStones(pitA[5-i].takeAll()+1);
//take the stone and opponent pit's stones and deposit in player mancala
}
else{
pitB[i].addStone();
i++;
}
}
}
}
}
}