-
Notifications
You must be signed in to change notification settings - Fork 0
/
Game.cc
206 lines (194 loc) · 5.71 KB
/
Game.cc
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
#include "Game.h"
// startGame() begins the game
void Game::startGame()
{
printMenu();
vector<string> words;
for (; getWords(words);)
{
// game
if (words.size() == 3 && words[0] == "game" &&
VALIDPLAYERS.count(words[1]) && VALIDPLAYERS.count(words[2]))
{
// binding players
if (words[1] == "human")
p1 = make_shared<Human>(b, true);
else
p1 = make_shared<Computer>(b, true, words[1][8] - '0');
if (words[2] == "human")
p2 = make_shared<Human>(b, false);
else
p2 = make_shared<Computer>(b, false, words[2][8] - '0');
// If it the board has been set up
// don't initialize it, and no castling
// for players on it
if (!run()) // reach EOF
break;
b = Board(1); // init board for next round
}
// setup
else if (words.size() == 1 && words[0] == "setup")
{
b = Board(); // empty the board
// if (isGraphic)
// b.setGraphic(true);
if (!setup()) // reach EOF
break;
cout << "Setup completed." << endl;
b.setCustomized(true);
}
// bad input
else
badInput();
printMenu();
}
// EOF
printScores();
}
// run() runs game on current board
bool Game::run()
{
p1->bindOppo(p2);
p2->bindOppo(p1);
saveBoard(); // save the initialized board to history
for (;;)
{
render.printBoard(b);
cout << "Next Player to move is: Player"
<< (b.isBoardWhiteTurn() ? 1 : 2) << endl;
shared_ptr<Player> cur = b.isBoardWhiteTurn() ? p1 : p2;
int &oppoScore = b.isBoardWhiteTurn() ? BScore : WScore;
int &myScore = b.isBoardWhiteTurn() ? WScore : BScore;
int res = cur->move();
if (res < 0 || res > 3) // continue
{
b.setWhiteTurn(!b.isBoardWhiteTurn());
saveBoard();
}
else if (res == 0) // EOF
return false;
else if (res == 1)
{
cout << "Player" << (b.isBoardWhiteTurn() ? 2 : 1)
<< " wins!" << endl;
oppoScore += 2;
return true;
}
else if (res == 2)
{
cout << "Draw !" << endl;
++myScore;
++oppoScore;
return true;
}
else
{
if (history.size() > 2)
{
b = history[history.size() - 3];
history.pop_back();
history.pop_back();
cout << "Regret!" << endl;
}
else
{
cout << "You can go back no further!" << endl;
}
}
}
}
// setup() sets up board
bool Game::setup()
{
b.clear();
vector<string> words;
for (; getWords(words);)
{
if (!words.size())
continue;
else if (!VALIDSETUPOPS.count(words[0]))
badInput();
if (words[0] == "done")
{
if (words.size() == 1)
if (validBoard())
return true;
else
cout << "This is not a valid setup!" << endl;
else
badInput();
}
else if (words[0] == "+")
{
if (words.size() == 3 && words[1].size() == 1 &&
(WHITEPIECESET.count(words[1][0]) ||
BLACKPIECESET.count(words[1][0])) &&
validCoord(words[2]))
{
b.piece(words[2][1] - '0',
words[2][0] - 'a' + 1) = words[1][0];
render.printBoard(b);
}
else
badInput();
}
else if (words[0] == "-")
{
if (words.size() == 2 && validCoord(words[1]))
{
b.piece(words[1][0] - 'a' + 1, words[1][1] - '0') = '\0';
render.printBoard(b);
}
else
badInput();
}
else if (words[0] == "=")
{
if (words.size() == 2 &&
(words[1] == "white" || words[1] == "black"))
{
b.setWhiteTurn(words[1] == "white");
cout << "Next Player to move is: Player"
<< (b.isBoardWhiteTurn() ? 1 : 2) << endl;
}
else
badInput();
}
}
return false;
}
// printScores() prints players' final game scores by colour
void Game::printScores()
{
cout << "Final Score:" << endl
<< "White: "
<< to_string(WScore / 2) + (WScore % 2 ? ".5" : "") << endl
<< "Black: "
<< to_string(BScore / 2) + (BScore % 2 ? ".5" : "") << endl;
}
// validBoard() checks validity of chess board
bool Game::validBoard()
{
// Creates two "virtual players" to check validity
shared_ptr<Player> p1 = make_shared<Computer>(b, true);
shared_ptr<Player> p2 = make_shared<Computer>(b, false);
p1->bindOppo(p2);
p2->bindOppo(p1);
return p1->eval() && p2->eval() && !p1->inCheck() && !p2->inCheck();
}
// printMenu() prints the menu of the game
void Game::printMenu()
{
cout << "************************* CHESS *************************"
<< endl
<< "Press Ctrl + D to quit and show scores" << endl
<< "Type game [human/computer[1-4]] [human/computer[1-4]] to play"
<< endl
<< "Type setup to create a board" << endl;
}
// saveBoard() saves the chess Board for possible future undos.
void Game::saveBoard()
{
history.push_back(b);
}
Game::Game(bool isGraphic) : render{isGraphic} {}