-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBoard.cpp
166 lines (132 loc) · 3.96 KB
/
Board.cpp
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
#include "Board.h"
Board::Board(int nHoles, int nInitialBeansPerHole)
{
if (nHoles < 1) //Fixing erroneous inputs
nHoles = 1;
if (nInitialBeansPerHole < 0)
nInitialBeansPerHole = 0;
//First make the pots
m_holes[NORTH].push_back(0);
m_holes[SOUTH].push_back(0);
for (int i = 0; i < nHoles; i++) //Then fill in the holes
{
m_holes[NORTH].push_back(nInitialBeansPerHole);
m_holes[SOUTH].push_back(nInitialBeansPerHole);
}
}
int Board::holes() const
{
return m_holes[NORTH].size() - 1; //Subtracting 1 to not count pot
}
int Board::beans(Side s, int hole) const
{
if (hole > holes() || hole < 0) //Catching invalid hole indices
return -1;
return m_holes[s][hole];
}
int Board::beansInPlay(Side s) const
{
int count = 0;
for (int i = 1; i <= holes(); i++) //summing beans on side s
{
count += m_holes[s][i];
}
return count;
}
int Board::totalBeans() const
{
int count = 0;
for (int i = 0; i <= holes(); i++) //summing all beans
{
count += m_holes[NORTH][i];
count += m_holes[SOUTH][i];
}
return count;
}
bool Board::sow(Side s, int hole, Side& endSide, int& endHole)
{ //going to put everything into one vector in counter-clockwise direction for ease of iteration
if ((hole == 0 || beans(s, hole) == 0) || beans(s, hole) == -1) //Catching erroneous input
return false;
vector<int*> m_vec;
int starting_index = -1;
//Adding all the holes to m_vec, starting with South 1 and ending with North Pot going counter-clockwise
for (int i = 1; i <= holes(); i++)
{
if (s == SOUTH && i == hole) //If the hole we are adding is the hole we are sowing from, make a note of the the corresonding index in m_vec
{
m_vec.push_back(&m_holes[SOUTH][i]);
starting_index = i - 1;
}
else
m_vec.push_back(&m_holes[SOUTH][i]);
}
m_vec.push_back(&m_holes[SOUTH][POT]);
if (starting_index == -1) //If our starting point isn't on south side, we use this to help us place the starting index correctly while we add North side holes
{
starting_index = holes();
}
for (int i = holes(); i >= 0; i--)
{
if (s == NORTH && i == hole) //If the hole we are adding is the hole we are sowing from, make a note of the the corresonding index in m_vec
{
m_vec.push_back(&m_holes[NORTH][i]);
starting_index += holes() + 1 - i;
}
else
m_vec.push_back(&m_holes[NORTH][i]);
}
//Now that our data is organized how we like, proceed to sow
int beansToSow = m_holes[s][hole]; //Picking up our beans from the starting point
m_holes[s][hole] = 0;
starting_index++; // Making starting index point to the hole AFTER where we just picked up our beans
int previous_index = 0;
int* opposing_pot = &m_holes[opponent(s)][POT];
while (beansToSow > 0)
{
if (opposing_pot == m_vec[starting_index])
{
starting_index = (starting_index + 1) % m_vec.size();
continue;
} //skips loop if opposing pot is about to be placed into
*m_vec[starting_index] += 1;
beansToSow--;
previous_index = starting_index;
starting_index = (starting_index + 1) % m_vec.size(); // this is what allows us to keep going circularly around
}
//We have sowed, now time to find where our ending point was in our original array of vectors (m_holes) so we can set endSide and endHole
int* endpt = m_vec[previous_index];
for (int i = 0; i <= holes(); i++)
{
if (endpt == &m_holes[SOUTH][i])
{
endSide = SOUTH;
endHole = i;
}
}
for (int i = 0; i <= holes(); i++)
{
if (endpt == &m_holes[NORTH][i])
{
endSide = NORTH;
endHole = i;
}
}
return true;
}
bool Board::moveToPot(Side s, int hole, Side potOwner)
{
if ((beans(s, hole) == -1) || hole == 0) // catching invalid hole indices
return false;
m_holes[potOwner][POT] += m_holes[s][hole]; //Adding beans to pot then clearing hole(s, hole)
m_holes[s][hole] = 0;
return true;
}
bool Board::setBeans(Side s, int hole, int beans)
{
if (hole > holes() || hole < 0) //Catching invalid hole indices
return false;
if (beans < 0) // Catching negative beans
return false;
m_holes[s][hole] = beans;
return true;
}