-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathchess-online-demo.cpp
362 lines (302 loc) · 10.6 KB
/
chess-online-demo.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
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
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
#include <iostream>
#include <array>
#include <stdexcept>
#include <sstream>
namespace chess {
enum class castle {
long_castle,
short_castle
};
enum class color {
white,
black
};
class chess {
protected:
std::array<std::array<char, 8>, 8> square;
public:
bool validate(color color, int x1, int y1, int x2, int y2);
bool validate(color color, castle castle_type);
bool move(color color, int x1, int y1, int x2, int y2, bool force = false);
bool move(color color, std::string query);
bool move(color color);
chess();
void reset();
void show();
std::array<std::array<char, 8>, 8> state();
void play();
};
}
using namespace std;
namespace chess {
// This function checks if castling is possible
bool chess::validate(color color, castle castle_type) {
int rank, rook_pos;
char king, rook;
// Set the color of king and rook
if (color == color::white) {
rank = 0;
king = 'K';
rook = 'R';
} else {
rank = 7;
king = 'k';
rook = 'r';
}
// Check if king is in correct place
if (square[rank][4] != king) {
return false;
}
// Set the value of rook_pos variable, containing rook's current position
if (castle_type == castle::long_castle) {
rook_pos = 0;
} else {
rook_pos = 7;
}
// Check if rook is in correct place
if (square[rank][rook_pos] == rook) {
// Check if every squares between king and rook is empty, return false if not
for (int i = 5; i < rook_pos; i++) {
if (square[rank][i] != ' ') {
return false;
}
}
}
// All tests passed, so castling is legal
return true;
}
// This function checks if move is valid (except castling)
bool chess::validate(color color, int x1, int y1, int x2, int y2) {
// Declare some needed variables
bool valid = false, correct_piece, capture_own_piece;
// Check if square is in range
if ((x1 >= 0 && x1 < 8) && (y1 >= 0 && y1 < 8) && (x2 >= 0 && x2 < 8) && (y2 >= 0 && y2 < 8)) {
// Check if the square from where piece will be moved is not empty
if (square[y1][x1] != ' ') {
// Prepare for checking if the player is moving his own piece
if (color == color::white) {
correct_piece = square[y1][x1] != tolower(square[y1][x1]);
} else {
correct_piece = square[y1][x1] != toupper(square[y1][x1]);
}
// Check if the player is moving his own piece
if (correct_piece) {
// Check if two squares, from where piece will move and where piece will placed, are not same
if (y1 != y2 || x1 != x2) {
// Prepare for checking if player is capturing his own piece
if (color == color::white) {
capture_own_piece = square[y2][x2] != tolower(square[y2][x2]);
} else {
capture_own_piece = square[y2][x2] != toupper(square[y2][x2]);
}
// Check if player is capturing his own piece
if (!capture_own_piece) {
valid = true;
}
}
}
}
}
return valid;
}
// This function works with the board directly
bool chess::move(color color, int x1, int y1, int x2, int y2, bool force) {
// Do an essensial test, check if move is forced, if yes, check if square is out of range
// Program will fail if skipped this test, and square is out of range
if (force) {
for (int value : {x1, y1, x2, y2}) {
if (value > 7 || value < 0) {
throw invalid_argument("Square is out of range!"); // Throw exception
}
}
}
// Check if move is either forced or valid
if (force || validate(color, x1, y1, x2, y2)) {
square[y2][x2] = square[y1][x1];
square[y1][x1] = ' ';
} else {
return false;
}
// Moved successfully, inform it
return true;
}
// This function take the move as std::string and moves
bool chess::move(color color, string query) {
// Prepare for checking if move is castling
int rank;
if (color == color::white) {
rank = 0;
} else {
rank = 7;
}
// Check if move is castling
if (query == "O-O" || query == "o-o" || query == "0-0") { // Check if move is short castling
if (!validate(color, castle::short_castle)) {
return false;
}
move(color, 4, rank, 6, rank, true);
move(color, 7, rank, 5, rank, true);
return true;
} else if (query == "O-O-O" || query == "o-o-o" || query == "0-0-0") { // Check if move is long castling
if (!validate(color, castle::long_castle)) {
return false;
}
move(color, 4, rank, 2, rank, true);
move(color, 0, rank, 3, rank, true);
return true;
}
// Declare two strings and a stringstream to extract square information
stringstream squares(query);
string src, dest;
// Extract square information from string
squares >> src >> dest;
int x1 = tolower(src[0])-'a';
int y1 = src[1]-'1';
int x2 = tolower(dest[0])-'a';
int y2 = dest[1]-'1';
// Call move function with all informations, and return the value it returned
return move(color, x1, y1, x2, y2);
}
// This function takes input input from user with standard input and moves
bool chess::move(color color) {
// Declare string where move query will be stored
string query = "";
int i = 0;
// Ask for move until move is valid
do {
// Show that move was invalid
if (i && query != "") {
cout << "Invaild move!" << endl;
}
i++;
// Show prompt
if (color == color::white) {
cout << "white >>> ";
} else {
cout << "black >>> ";
}
// Get move
getline(cin, query);
// Return false if user entered exit
if (query == "exit") {
return false;
}
} while (!move(color, query));
return true;
}
// Constructor
chess::chess() {
reset();
}
// This function will reset the board
void chess::reset() {
// Place white piece, except pawns
square[0][0] = square[0][7] = 'R';
square[0][1] = square[0][6] = 'N';
square[0][2] = square[0][5] = 'B';
square[0][3] = 'Q';
square[0][4] = 'K';
// Place black piece, except pawns
square[7][0] = square[7][7] = 'r';
square[7][1] = square[7][6] = 'n';
square[7][2] = square[7][5] = 'b';
square[7][3] = 'q';
square[7][4] = 'k';
// Place pawns of both colors
char pawn;
for (int y : {1, 6}) {
// Change pawn color, when y = 1, pawn is while, black otherwise
if (y == 1) {
pawn = 'P';
} else {
pawn = 'p';
}
for (int x = 0; x < 8; x++) {
square[y][x] = pawn;
}
}
// Clear all other squares
for (int x = 2; x < 6; x++) {
for (int y = 0; y < 8; y++) {
square[x][y] = ' ';
}
}
}
// This function show the state of board, directly to terminal
void chess::show() {
cout << " +---+---+---+---+---+---+---+---+" << endl;
for (int x = 7; x >= 0; x--) {
cout << x+1 << " | ";
for (int y = 0; y < 8; y++) {
cout << square[x][y] << " | ";
}
cout << endl << " +---+---+---+---+---+---+---+---+" << endl;
}
cout << " a b c d e f g h " << endl;
}
// This function will return the array containing board state
std::array<std::array<char, 8>, 8> chess::state() {
return square;
}
// This function will ask for move infinitely, until user types exit on prompt
void chess::play() {
// Declare variable containing if move should be asked again
bool again = true;
// Ask moves, until user exits
while (again) {
// Ask for move to white, and store return value to again
again = move(color::white);
// Check if white moved, if yes, ask for move
if (again) {
// Show the board
show();
// Black must move, otherwise he will lose his chance
while (!move(color::black)) {
cout << "You must move, otherwise you'll lose your chance" << endl;
}
// Show the board
show();
}
}
}
}
int main() {
// Declare and set necessary variables
bool play = true;
string command = "";
chess::chess chess;
// Show the board
chess.show();
// Prompt user what to do, until he commands to exit
while (play) {
// Show the prompt
cout << ">>> " << flush;
// Get command
getline(cin, command);
// Check and do what to do
if (command == "move") {
if (chess.move(chess::color::white)) {
chess.show();
while (!chess.move(chess::color::black)) {
cout << "You must move, otherwise you'll lose your chance" << endl;
}
chess.show();
}
} else if (command == "play") {
chess.show();
chess.play();
} else if (command == "reset") {
chess.reset();
chess.show();
} else if (command == "exit") {
play = false;
} else if (command == "show") {
chess.show();
} else if (command == "") {
continue;
} else {
cout << "Unknown command" << endl;
}
}
return 0;
}