-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBoard.cpp
More file actions
390 lines (370 loc) · 9.79 KB
/
Board.cpp
File metadata and controls
390 lines (370 loc) · 9.79 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
#include "Board.h"
#include <iostream>
using namespace std;
Board::Board(int nHoles, int nInitialBeansPerHole): grid0(nullptr), grid1(nullptr)
{
if (nHoles < 1) //treat negative hole number as 1
{
nHoles = 1;
}
if (nInitialBeansPerHole < 0) //treat negative beans per hole as 0
{
nInitialBeansPerHole = 0;
}
m_holes_side = nHoles;
m_total_holes = m_holes_side * NSIDES;
num_beans_north = nInitialBeansPerHole * m_holes_side;
num_beans_south = nInitialBeansPerHole * m_holes_side;
//////////////////////////////////////////////////////
//initialize the grid
//////////////////////////////////////////////////////
grid0 = new int[m_holes_side + 1]; //account for the pot on each side
grid1 = new int[m_holes_side + 1]; //account for the pot on each side
//fill grid with beans
//////////////////////////////////////
//North row
//////////////////////////////////////
for (int i = 1; i <= m_holes_side; i++)
{
grid0[i] = nInitialBeansPerHole; //fill north row with beans
}
grid0[0] = 0; //intialise north pot as empty
//////////////////////////////////////
//South row
//////////////////////////////////////
for (int i = 1; i <= m_holes_side; i++)
{
grid1[i] = nInitialBeansPerHole; //fill south row with beans
}
grid1[0] = 0; //intialise south pot as empty
}
Board::Board(const Board& copy)
{
m_holes_side = copy.m_holes_side;
m_total_holes = copy.m_total_holes;
num_beans_north = copy.num_beans_north;
num_beans_south = copy.num_beans_south;
//////////////////////////////////////////////////////
//initialize the grid
//////////////////////////////////////////////////////
grid0 = new int[m_holes_side + 1]; //account for the pot on each side
grid1 = new int[m_holes_side + 1]; //account for the pot on each side
//fill grid with beans
//////////////////////////////////////
//North row
//////////////////////////////////////
for (int i = 1; i <= m_holes_side; i++)
{
grid0[i] = copy.grid0[i]; //fill north row with beans
}
grid0[0] = copy.grid0[0]; //intialise north pot as empty
//////////////////////////////////////
//South row
//////////////////////////////////////
for (int i = 1; i <= m_holes_side; i++)
{
grid1[i] = copy.grid1[i]; //fill south row with beans
}
grid1[0] = copy.grid1[0]; //intialise south pot as empty
}
//Board& Board::operator=(const Board& rhs) //assignment operator
//{
// if (this != &rhs)
// {
// /////////////////////////////////////////////
// //Copy all the private members
// /////////////////////////////////////////////
// m_holes_side = rhs.m_holes_side;
// m_total_holes = rhs.m_total_holes;
// num_beans_north = rhs.num_beans_north;
// num_beans_south = rhs.num_beans_south;
//
// //////////////////////////////////////////////////////
// //initialize the grid
// //////////////////////////////////////////////////////
// grid0 = new int[m_holes_side + 1]; //account for the pot on each side
// grid1 = new int[m_holes_side + 1];
// ////////////////////////////////////////////////////
// //fill the grid with same number of beans from rhs//
// ////////////////////////////////////////////////////
// for (int i = 0; i <= rhs.holes(); i++)
// {
// grid0[i] = rhs.grid0[i];
// grid1[i] = rhs.grid1[i];
// }
// }
// return *this;
//}
Board& Board::operator=(const Board& rhs)
{
if (this != &rhs)
{
delete[]grid1;
delete[]grid0;
m_holes_side = rhs.m_holes_side;
m_total_holes = rhs.m_total_holes;
num_beans_north = rhs.num_beans_north;
num_beans_south = rhs.num_beans_south;
////////////////////////////////////
grid0 = new int[m_holes_side + 1];
grid1 = new int[m_holes_side + 1];
////////////////////////////////////
for (int i = 1; i <= m_holes_side; i++)
{
grid0[i] = rhs.grid0[i]; //fill north row with beans
}
grid0[0] = rhs.grid0[0]; //intialise north pot as empty
for (int i = 1; i <= m_holes_side; i++)
{
grid1[i] = rhs.grid1[i]; //fill south row with beans
}
grid1[0] = rhs.grid1[0]; //intialise south pot as empty
}
return (*this);
}
Board::~Board()
{
delete [] grid0;
delete [] grid1;
//for (int i = 0; i < 2; i++)
//{
// delete[] grid[i]; //delete the 2 arrays of integers first
//}
//delete []grid; //delete the array of 2 pointers
}
int Board::holes() const
{
return(m_holes_side);
}
int Board::beans(Side s, int hole) const
{
if (s == NORTH)
{
if (hole >= 0 && hole <= m_holes_side )
{
return(grid0[hole]);
}
else
{
return(-1);
}
}
else
{
if (hole >= 0 && hole <= m_holes_side)
{
return(grid1[hole]);
}
else
{
return(-1);
}
}
}
int Board::beansInPlay(Side s) const
{
if (s == NORTH)
{
return(num_beans_north - grid0[0]);
}
else
{
return(num_beans_south - grid1[0]);
}
}
int Board::totalBeans() const
{
return(num_beans_north + num_beans_south);
}
bool Board::sow(Side s, int hole, Side& endSide, int& endHole)
{
if (!(hole >= 1 && hole <= m_holes_side))
{
return(false);
}
if (s == SOUTH)
{
if (grid1[hole] == 0)
{
return(false);
}
}
if (s == NORTH)
{
if (grid0[hole] == 0)
{
return(false);
}
}
/////////////////////////////////////////////////////////////////
//hole number enter is valid
////////////////////////////////////////////////////////////////
if (s == NORTH)
{
int beanstosow = grid0[hole];
grid0[hole] = 0; //remove all the beans form the particular side and row
int pos_nextsow = hole;
Side currentside = NORTH;
while (beanstosow != 0)
{
if (currentside == NORTH && pos_nextsow == 0) //when hit north's pot and next move should be first cell of south
{
currentside = SOUTH;
pos_nextsow = 1;
}
else if (currentside == NORTH && pos_nextsow != 0) //anywhere between the last cell and first cell of north, next pos-1
{
currentside = NORTH;
pos_nextsow -= 1;
}
else if (currentside == SOUTH && pos_nextsow == m_holes_side) //when at south and hits last cell of south before south's pot, got to last cell of north(anticlockwise)
{
currentside = NORTH;
pos_nextsow = m_holes_side;
}
else if (currentside == SOUTH && pos_nextsow != m_holes_side) //when at south but did not hit south's pot, stay at currentside and nextpos+1
{
currentside = SOUTH;
pos_nextsow += 1;
}
////////////////////////////////////////////////////////
if (currentside == NORTH) //when sow in north
{
grid0[pos_nextsow] += 1;
endHole = pos_nextsow;
beanstosow--;
}
else //when sow is in south(different side)
{
grid1[pos_nextsow] += 1;
endHole = pos_nextsow;
beanstosow--;
//account for change in the number of north beans and south beans
num_beans_north--;
num_beans_south++;
}
}
endSide = currentside;
return(true);
}
else
{
int beanstosow = grid1[hole];
grid1[hole] = 0;
int pos_nextsow = hole;
Side currentside = SOUTH;
bool hitSouthPot = false; //boolean to indicate if south pot is hit
while (beanstosow != 0)
{
if (currentside == SOUTH && pos_nextsow == m_holes_side) //when at south and hit the last cell of south
{
currentside = SOUTH;
pos_nextsow = 0; //next pos is south pot
hitSouthPot = true; //set boolean to true
}
else if (currentside == SOUTH && hitSouthPot == true) //when at south and at south's pot
{
currentside = NORTH; //change the side
pos_nextsow = m_holes_side; //set the next to north's last cell
hitSouthPot = false; //set boolean to false
}
else if (currentside == SOUTH && hitSouthPot == false && pos_nextsow!=m_holes_side) //from the first to one before the last south cell
{
currentside = SOUTH; //remain same side
pos_nextsow += 1; //increment position by one
}
else if (currentside == NORTH && pos_nextsow == 1) //when at north and positon is 1
{
currentside = SOUTH; //change side, skipping the north pot
pos_nextsow = 1; //go to the first cell of south
}
else if (currentside == NORTH && pos_nextsow != 1 && pos_nextsow != 0) //from the last to the first north cell, leaving out the pot
{
currentside = NORTH; //remain same side
pos_nextsow -= 1;
}
////////////////////////////////////////////////////////
if (currentside == SOUTH) //when sow is in south
{
grid1[pos_nextsow] += 1;
endHole = pos_nextsow;
beanstosow--;
}
else //when sow is in north(different side)
{
grid0[pos_nextsow] += 1;
endHole = pos_nextsow;
num_beans_north++;
num_beans_south--;
beanstosow--;
}
}
endSide = currentside;
return(true);
}
}
bool Board::moveToPot(Side s, int hole, Side potOwner)
{
if (hole>=1 && hole <= m_holes_side)
{
int temp;
if (s == NORTH)
{
temp = grid0[hole];
grid0[hole] = 0;
}
else
{
temp = grid1[hole];
grid1[hole] = 0;
}
if (potOwner == NORTH)
{
grid0[0] += temp;
if (s == SOUTH) //account for changing number of beans (during capture)
{
num_beans_north += temp;
num_beans_south -= temp;
}
}
else
{
grid1[0] += temp;
if (s == NORTH) //account for changing number of beans (during capture)
{
num_beans_south += temp;
num_beans_north -= temp;
}
}
return(true);
}
else
{
return(false);
}
}
bool Board::setBeans(Side s, int hole, int beans)
{
if (beans < 0)
{
return(false);
}
if (!(hole >= 0 && hole <= m_holes_side))
{
return(false);
}
////////////////////////////////////////////////
if (s == NORTH)
{
num_beans_north -= grid0[hole];
grid0[hole] = beans;
num_beans_north += beans;
}
else
{
num_beans_south -= grid1[hole];
grid1[hole] = beans;
num_beans_south += beans;
}
return(true);
}