-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGame.cpp
More file actions
165 lines (154 loc) · 4.66 KB
/
Copy pathGame.cpp
File metadata and controls
165 lines (154 loc) · 4.66 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
#include"Game.h"
#include"Player.h"
#include"Board.h"
using namespace std;
Game::Game(const Board& b, Player* south, Player* north) :m_board(b), m_south(south), m_north(north), turn(SOUTH) {
}
void Game::display() const {
cout << "\t" + m_north->name() << endl;
cout << " ";
for (int i = 1; i <= m_board.holes(); i++) {
cout << " " + to_string(m_board.beans(NORTH, i)) + " ";
}
cout << endl;
cout << " " + to_string(m_board.beans(NORTH, POT)) + " ";
for (int i = 1; i <= m_board.holes(); i++) {
cout << " ";
}
cout << " " + to_string(m_board.beans(SOUTH, POT)) + " ";
cout << endl;
cout << " ";
for (int i = 1; i <= m_board.holes(); i++) {
cout << " " + to_string(m_board.beans(SOUTH, i)) + " ";
}
cout << endl;
cout << "\t" + 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) { //game is not over
over = false;
hasWinner = false;
return;
}
over = true;
if (m_board.beans(NORTH, POT) > m_board.beans(SOUTH, POT)) { //north won
hasWinner = true;
winner = NORTH;
}
else if (m_board.beans(SOUTH, POT) > m_board.beans(NORTH, POT)) { //south won
hasWinner = true;
winner = SOUTH;
}
else { // its a draw
hasWinner = false;
}
return;
}
bool Game::move() {
int endHole;
Side endSide;
//vector<int> isZero; //creates a vector that stores the possible endholes that would result in a capture
if (turn == SOUTH) {
int hole = m_south->chooseMove(m_board, turn);
if (!m_south->isInteractive()) { //indicate what moves the computer player makes
cout << m_south->name() + " chooses hole " + to_string(hole) << endl;
}
//for (int i = 1; i <= m_board.holes(); i++) { //adds the endholes to the vector
// if (m_board.beans(turn, i) == 0) {
// isZero.push_back(i);\
// };
//}
if (m_board.sow(turn, hole, endSide, endHole)) {
if (endHole == POT) {
//note that sow will always skip the opponent's pot so endhole = 0 implies endhole is on the same side as the player
display();
if (m_board.beansInPlay(SOUTH) > 0) {
cout << m_south->name() + " gets another turn." << endl;
return move(); //player gets another turn
}
}
else if (endSide == turn) {
//for (int i = 0; i < isZero.size(); i++) {
if (m_board.beans(turn, endHole) == 1 && m_board.beans(opponent(turn), endHole) > 0) { //capture
m_board.moveToPot(opponent(turn), endHole, turn);
m_board.moveToPot(turn, endHole, turn);
}
//}
}
}
}
else { //turn = north
int hole = m_north->chooseMove(m_board, turn);
if (!m_north->isInteractive()) {
cout << m_north->name() + " chooses hole " + to_string(hole) << endl;
}
//for (int i = 1; i <= m_board.holes(); i++) {
// if (m_board.beans(turn, i) == 0) {
// isZero.push_back(i);
// };
//}
if (m_board.sow(turn, hole, endSide, endHole)) {
if (endHole == POT) {
display();
if (m_board.beansInPlay(NORTH) > 0) {
cout << m_north->name() + " gets another turn." << endl;
return move(); //player gets another turn
}
}
else if (endSide == turn) {
//for (int i = 0; i < isZero.size(); i++) {
if (m_board.beans(turn, endHole) == 1 && m_board.beans(opponent(turn), endHole) > 0) { //capture
m_board.moveToPot(opponent(turn), endHole, turn);
m_board.moveToPot(turn, endHole, turn);
}
//}
}
}
}
turn = opponent(turn);
if (m_board.beansInPlay(NORTH) == 0) { //game is over
display();
cout << "Sweeping remaining beans into " + m_south->name() + "'s pot." << endl;
for (int i = 1; i <= m_board.holes(); i++) { //loop through the south holes
m_board.moveToPot(SOUTH, i, SOUTH);
}
return false;
}
else if (m_board.beansInPlay(SOUTH) == 0) {
display();
cout << "Sweeping remaining beans into " + m_north->name() + "'s pot." << endl;
for (int i = 1; i <= m_board.holes(); i++) { //loop through north's holes
m_board.moveToPot(NORTH, i, NORTH);
}
return false;
}
return true;
}
void Game::play() {
bool over, hasWinner;
Side winner;
display();
do {
move();
display();
if (!m_south->isInteractive() && !m_north->isInteractive()) {
cout << "Press ENTER to continue.";
cin.ignore(10000, '\n');
}
status(over, hasWinner, winner);
} while (!over);
if (hasWinner) {
if (winner == SOUTH) {
cout << "The winner is " + m_south->name() << endl;
}
else {
cout << "The winner is " + m_north->name() << endl;
}
}
else {
cout << "It's a tie." << endl;
}
}
int Game::beans(Side s, int hole) const {
return m_board.beans(s, hole);
}