-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGame.cpp
More file actions
295 lines (278 loc) · 8.06 KB
/
Game.cpp
File metadata and controls
295 lines (278 loc) · 8.06 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
#include "Game.h"
#include <iostream>
#include <string>
#include <vector>
using namespace std;
Game::Game(const Board& b, Player* south, Player* north): m_board(b)
{
m_south = south;
m_north = north;
}
void Game::display() const
{
/////////////////////////////////////////
//Create and fill in values for northrow
/////////////////////////////////////////
vector<int> northrow(m_board.holes());
for (int i = 0; i < m_board.holes(); i++)
{
northrow[i] = m_board.beans(NORTH, i+1);
}
///////////////////////////////////
//create and fill in values for pot
///////////////////////////////////
vector<int> potrow(2);
potrow[0] = m_board.beans(NORTH,0);
potrow[1] = m_board.beans(SOUTH,0);
/////////////////////////////////////////
//Create and fill in values for southrow
/////////////////////////////////////////
vector<int> southrow(m_board.holes());
for (int i = 0; i < m_board.holes(); i++)
{
southrow[i] = m_board.beans(SOUTH, i+1);
}
//////////////////
//Display of grid
//////////////////
for (int i = 0; i < m_board.holes(); i++)
{
cout << " ";
}
cout << m_north->name() << endl;
cout << " ";
for (size_t i = 0; i < m_board.holes(); i++)
{
cout << " ";
cout << northrow[i];
cout << " ";
}
cout << endl;
cout << potrow[0];
cout << " ";
for (size_t i = 1; i < m_board.holes(); i++)
{
cout << " ";
}
cout << " ";
cout << potrow[1] << endl;
cout << " ";
for (size_t i = 0; i < m_board.holes(); i++)
{
cout << " ";
cout << southrow[i];
cout << " ";
}
cout << endl;
for (int i = 0; i < m_board.holes(); i++)
{
cout << " ";
}
cout << m_south->name() << endl;
}
void Game::status(bool& over, bool& hasWinner, Side& winner) const
{
if (m_board.beansInPlay(NORTH) == 0 || m_board.beansInPlay(SOUTH) == 0) //if either side has 0 beans in play
{
over = true; //game is over
//find the total number of beans for each side
int northpot = m_board.beans(NORTH, 0) + m_board.beansInPlay(NORTH);
int southpot = m_board.beans(SOUTH, 0) + m_board.beansInPlay(SOUTH);
if (northpot > southpot) //if north has more beans than south, winner is north
{
hasWinner = true;
winner = NORTH;
}
else if (northpot < southpot) //if south has more beans than north, winner is south
{
hasWinner = true;
winner = SOUTH;
}
else //if it is a draw
{
hasWinner = false; //there is no winner, make no changes to winner
}
return;
}
else //if there are still beans in play
{
over = false; //game not over
return;
}
}
bool Game::move()
{
status(over, hasWinner, winner); //call status function to check whether game is over
if (over)
{
return(false); //if game over, no move possible, return false
}
else
{
if (m_turn == SOUTH)
{
int holetosole = m_south->chooseMove(m_board, SOUTH); //call the player choose move function
if (!(m_south->isInteractive())) //if not human player
{
cout << m_south->name() << " chooses hole " << holetosole << "." << endl; //cout the statement which hole the south player chooses
}
m_board.sow(SOUTH, holetosole, endSide, endHole); //sow the board
if (endSide == SOUTH && endHole == 0 && m_board.beansInPlay(SOUTH) != 0) //if lands in the south's pot(its own pot) and turn still available
{
display(); //display the grid
cout << m_south->name() << " gets another turn." << endl;
return(move()); //recursively call the function again
}
else if (endSide == SOUTH && m_board.beans(SOUTH, endHole) == 1 && m_board.beans(NORTH, endHole) != 0) //if it is a capture move(lands in a hole on the same side initially empty), and hole on opposite side is not empty
{
m_board.moveToPot(NORTH, endHole, SOUTH);
m_board.moveToPot(SOUTH, endHole, SOUTH);
}
m_turn = NORTH; //change of turn, after end of each move
}
else if (m_turn == NORTH) //if turn is north
{
int holetosole = m_north->chooseMove(m_board, NORTH);
if (!(m_north->isInteractive())) //if not human player
{
cout << m_north->name() << " chooses hole " << holetosole << "." << endl; //cout the statement which hole the north player chooses
}
m_board.sow(NORTH, holetosole, endSide, endHole);
if (endSide == NORTH && endHole == 0 && m_board.beansInPlay(NORTH) != 0) //if repeat move
{
display();
cout << m_north->name() << " gets another turn." << endl;
return(move());
}
else if (endSide == NORTH && m_board.beans(NORTH, endHole) == 1 && m_board.beans(SOUTH, endHole) != 0) //if capture move
{
m_board.moveToPot(SOUTH, endHole, NORTH);
m_board.moveToPot(NORTH, endHole, NORTH);
}
m_turn = SOUTH; //change of turn after move
}
status(over, hasWinner, winner); //call the status function
if (over == true) //if game over
{
display(); //display grid
if (m_board.beansInPlay(NORTH) == 0) //case of north, south player still has beans
{
for (int i = 1; i <= m_board.holes(); i++)
{
if (m_board.beans(SOUTH, i) != 0)
{
m_board.moveToPot(SOUTH, i, SOUTH); //put remaining beans into south's pot
}
}
cout << "Sweeping remaining beans into " << m_south->name() << "'s pot." << endl;
display();
}
else //case of south, north player still has beans
{
for (int i = 1; i <= m_board.holes(); i++)
{
if (m_board.beans(NORTH, i) != 0)
{
m_board.moveToPot(NORTH, i, NORTH); //put remaining beans into north's pot
}
}
cout << "Sweeping remaining beans into " << m_north->name() << "'s pot." << endl;
display();
}
}
status(over, hasWinner, winner); //call status function to change the winner
return(true);
}
}
void Game::play()
{
if (!(m_south->isInteractive()) && !(m_north->isInteractive())) //both players ar bots
{
display(); //display grid
if (move() == false) //if move returns false, means first move cannot be made, south side has no beans in play
{
for (int i = 1; i <= m_board.holes(); i++)
{
m_board.moveToPot(NORTH, i, NORTH); //move all existing north beans into pot
}
cout << "Sweeping remaining beans into " << m_north->name() << "'s pot." << endl;
display();
}
while (over != true) //while game is not over
{
display();
move();
cout << "Press ENTER to continue";
cin.ignore(9600, '\n');
}
if (over == true) //if game is over
{
string victor;
//check winner is south or north
if (winner == SOUTH && hasWinner == true)
{
victor = m_south->name(); //assign south winner's name to victor
cout << "The winner is " << victor << "." << endl;
}
else if (winner == NORTH && hasWinner == true)
{
victor = m_north->name(); //assign north winner's name to victor
cout << "The winner is " << victor << "." << endl;
}
else
{
cout << "It is a draw, there is no winner." << endl;
}
}
}
else //one human and one computer
{
display(); //display grid
if (move() == false) //if no move possible
{
for (int i = 1; i <= m_board.holes(); i++)
{
m_board.moveToPot(NORTH, i, NORTH);
}
cout << "Sweeping remaining beans into " << m_north->name() << "'s pot." << endl;
display();
}
while (over != true) //if game not over
{
display();
move();
}
if (over == true)
{
string victor;
//check winner is south or north
if (winner == SOUTH && hasWinner == true)
{
victor = m_south->name(); //assign south winner's name to victor
cout << "The winner is " << victor << "." << endl;
}
else if (winner == NORTH && hasWinner == true)
{
victor = m_north->name(); //assign north winner's name to victor
cout << "The winner is " << victor << "." << endl;
}
else
{
cout << "It is a draw, there is no winner." << endl;
}
}
}
return;
}
int Game::beans(Side s, int hole) const
{
if (hole >= 0 && hole <= m_board.holes()) //if the hole entered is valid
{
int numbeans = m_board.beans(s, hole);
return(numbeans);
}
else
{
return(-1);
}
}