-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcontroller.cc
More file actions
253 lines (241 loc) · 6.18 KB
/
controller.cc
File metadata and controls
253 lines (241 loc) · 6.18 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
#include "controller.h"
#include <iostream>
#include <string>
#include <sstream>
#include <fstream>
#include "board.h"
using namespace std;
Controller::Controller(): presetBoard {false} {
board = make_shared<Board>();
}
Controller::~Controller() {}
bool Controller::validPlayer(const string &player) {
if (player == "human") return true;
else if (player == "computer1") return true;
else if (player == "computer2") return true;
else if (player == "computer3") return true;
else if (player == "computer4") return true;
else return false;
}
bool Controller::validPiece(const string &piece) {
string s;
s += toupper(piece[0]);
if (s == "K" || s == "Q" || s == "B" || s == "R" || s == "N" || s == "P") {
return true;
}
return false;
}
bool Controller::validPos(const string &pos) {
if (pos.size() != 2) return false;
if (pos[0] >= 'a' && pos[0] <= 'h') {
if (pos[1] >= '1' && pos[1] <= '8') {
return true;
}
return false;
}
return false;
}
Coor Controller::toCoor(const string &pos) {
int x = pos[0] - 'a';
int y = pos[1] - '1';
Coor c {x, y};
return c;
}
void Controller::setup(istream &is, bool showStep) {
presetBoard = true;
board->initBoard();
board->clearBoard();
if (showStep) board->print();
string cmd;
while (is >> cmd) {
if (cmd == "+") {
string piece, pos;
is >> piece >> pos;
if (validPiece(piece) && validPos(pos)) {
Coor c = toCoor(pos);
board->placePiece(piece[0], c);
if (showStep) board->print();
} else {
cout << "Invalid + usage" << endl;
}
} else if (cmd == "-") {
string pos;
is >> pos;
if (validPos(pos)) {
Coor c = toCoor(pos);
board->removePiece(c);
if (showStep) board->print();
} else {
cout << "Invalid - usage, check your coordinates" << endl;
}
} else if (cmd == "=") {
string color;
is >> color;
if (color == "black"){
board->wturn = false;
cout << "Black player will move first" << endl;
} else if (color == "white") {
board->wturn = true;
cout << "White player will move first" << endl;
} else {
cout << "Invalid color" << endl;
}
} else if (cmd == "done") {
if (board->validBoard()) {
cout << "Board setup completed" << endl;
board->saveHistory();
break;
} else {
cout << "Invalid Board, please double check" << endl;
}
} else if (!cin.eof()) {
cout << "Invalid setup command" << endl;
}
}
if (!showStep) board->print();
}
void Controller::printTurn(bool isWhite) {
if (isWhite) cout << "White player's turn" << endl;
else cout << "Black player's turn" << endl;
}
void Controller::game() {
if (!presetBoard) board->initBoard();
board->print();
printTurn(board->wturn);
string cmd;
string start, dest;
while (cin >> cmd) {
if (cmd == "move") {
// check if it is AI
if (board->wturn && board->aiW) {
board->aiW->makeMove();
if (board->needPromotion()) {
board->placePiece('Q', board->AIneedPromotion(0));
}
board->print();
printTurn(board->wturn);
} else if (!board->wturn && board->aiB) {
board->aiB->makeMove();
if (board->needPromotion()) {
board->placePiece('q', board->AIneedPromotion(1));
}
board->print();
printTurn(board->wturn);
} else {
cin >> start >> dest;
if (validPos(start) && validPos(dest)) {
string result = board->makeMove(toCoor(start), toCoor(dest));
if (result == "empty") {
cout << "No valid piece at chosen position" << endl;
continue;
} else if (result == "invalid") {
cout << "Invalid move" << endl;
continue;
}
board->print();
if (board->needPromotion()) {
string choice;
char type;
board->wturn = !board->wturn;
cout << "Promote to rook, knight, bishop or queen?" << endl;
while (cin >> choice) {
if (choice == "rook") {
if (board->wturn) type = 'R';
else type = 'r';
break;
} else if (choice == "knight") {
if (board->wturn) type = 'N';
else type = 'n';
break;
} else if (choice == "bishop") {
if (board->wturn) type = 'B';
else type = 'b';
break;
} else if (choice == "queen") {
if (board->wturn) type = 'Q';
else type = 'q';
break;
} else if (!cin.eof()) {
cout << "Invalid choice, type rook, knight, bishop or queen" << endl;
}
}
board->placePiece(type, toCoor(dest));
board->wturn = !board->wturn;
board->print();
}
printTurn(board->wturn);
} else {
cout << "Incorrect usage of move, check your coordinates again" << endl;
}
}
} else if (cmd == "resign") {
if (!board->resign()) {
cout << "Black wins!" << endl;
return;
} else {
cout << "White wins!" << endl;
return;
}
} else if (cmd == "undo") {
if (board->castling(toCoor(start), toCoor(dest))) {
board->undo(false);
board->undo(false);
board->wturn = !board->wturn;
} else {
board->undo(false);
}
board->print();
} else if (!cin.eof()) {
cout << "Invalid move command" << endl;
}
if (board->isCheckmate()) {
int result = board->resign(); //0 means white resign, 1 means black resign
if (result) {
cout << "Checkmate! White wins!" << endl;
} else {
cout << "Checkmate! Black wins!" << endl;
}
break;
}
if (board->isStalemate()) {
cout << "Stalemate!" << endl;
board->scoreUpdate(0, 0.5);
board->scoreUpdate(1, 0.5);
break;
}
if (board->isCheck()) {
if (board->wturn) {
cout << "White is in check" << endl;
} else {
cout << "Black is in check" << endl;
}
}
}
}
void Controller::play() {
string cmd;
while (cin >> cmd) {
if (cmd == "game") {
string player1, player2;
cin >> player1 >> player2;
if (validPlayer(player1) && validPlayer(player2)) {
board->setPlayer(player1, player2);
game();
presetBoard = false;
}
} else if (cmd == "setup") {
setup(cin, true);
} else if (cmd == "read") {
string filename;
cin >> filename;
ifstream ifs;
ifs.open(filename);
setup(ifs, false);
} else if (!cin.eof()) {
cout << "Invalid game command" << endl;
}
}
cout << "Final Score:" << endl;
cout << "White: " << board->getWscore() << endl;
cout << "Black: " << board->getBscore() << endl;
}