-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathChessGame.cpp
More file actions
547 lines (476 loc) · 14.8 KB
/
Copy pathChessGame.cpp
File metadata and controls
547 lines (476 loc) · 14.8 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
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
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
#include "ChessGame.h"
int main()
{
static char board[8][8] = {
{'r', 'n', 'b', 'q', 'k', 'b', 'n', 'r'},
{'p', 'p', 'p', 'p', 'p', 'p', 'p', 'p'},
{' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '},
{' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '},
{' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '},
{' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '},
{'P', 'P', 'P', 'P', 'P', 'P', 'P', 'P'},
{'R', 'N', 'B', 'Q', 'K', 'B', 'N', 'R'},
};
static char beatenPiecesBlack[16];
int lastInsertBlack = -1;
static char beatenPiecesWhite[16];
int lastInsertWhite = -1;
Color color = Color::Undefined;
askLoadGame(board, beatenPiecesBlack, beatenPiecesWhite, &color);
if (color == Color::Undefined) {
color = selectColor();
}
string errMessage = "";
string winner = "";
bool patt = false;
bool running = true;
while (running) {
displayBoard(board, 8, 8, color, errMessage);
displayStats(beatenPiecesBlack, beatenPiecesWhite);
errMessage = "";
bool exit = false;
Move move = promptForMove(8, &exit);
if (exit) {
askSaveGame(board, beatenPiecesBlack, beatenPiecesWhite, color);
return 0;
}
if (!isMoveValid(board, color, move, &errMessage)) {
continue;
}
if (isBeat(board, move) && isValidBeat(board, move, color, &errMessage)) {
string colorAsString = color == Color::Black ? "Black" : "White";
char piece = board[move.to.y - 1][move.to.x - 1];
if (piece == 'k' || piece == 'K') {
winner = colorAsString;
break;
}
if (color == Color::Black) {
beatenPiecesWhite[lastInsertWhite + 1] = piece;
lastInsertWhite++;
}
else {
beatenPiecesBlack[lastInsertBlack + 1] = piece;
lastInsertBlack++;
}
if (lastInsertBlack >= 14 && lastInsertWhite >= 14) {
patt = true;
break;
}
}
if (errMessage == "") {
doMove(board, move);
checkConvertPawn(board, move, color);
color = color == Color::Black ? Color::White : Color::Black;
}
}
system("cls");
if (patt) {
cout << "Patt!\n";
}
else {
cout << winner << " has won!\n";
}
}
void displayStats(char beatenPiecesBlack[16], char beatenPiecesWhite[16]) {
cout << "Beaten pieces: (Black) " << beatenPiecesBlack << ", (White) " << beatenPiecesWhite << endl;
cout << endl;
}
void displayBoard(char board[][8], int sizeX, int sizeY, Color currentColor, string message) {
system("cls");
string colorAsString = currentColor == Color::Black ? "Black" : "White";
cout << "\nIt is " << colorAsString << "'s turn\n";
cout << endl;
if (message.length() > 0) {
cout << message << endl;
cout << endl;
}
cout << " ";
for (int x = 0; x < sizeX; x++) {
cout << " " << x + 1 << " ";
}
cout << endl;
for (int y = 0; y < sizeY; y++) {
cout << "\33[0m " << y + 1 << " ";
for (int x = 0; x < sizeX; x++) {
if (y % 2 == 0) {
if (x % 2 == 0) {
cout << "\33[30m\33[47m";
}
else {
cout << "\33[97m\33[40m";
}
}
else {
if (x % 2 == 0) {
cout << "\33[97m\33[40m";
}
else {
cout << "\33[30m\33[47m";
}
}
cout << " " << board[y][x] << " ";
}
cout << endl;
}
cout << "\33[0m\n";
}
Color selectColor() {
Color color;
bool colorSelected = false;
while (!colorSelected) {
color = promptForColor();
if (color != Color::White && color != Color::Black) {
system("cls");
continue;
}
colorSelected = true;
}
system("cls");
return color;
}
Color promptForColor() {
Color color;
int tempNumber;
cout << "Which color do you want to play? (Black 1, White 2): ";
cin >> tempNumber;
color = (Color)tempNumber;
return color;
}
char promptForPiece() {
char piece;
string input;
cout << "Which piece do you want your pawn to become? (q,r,b,n)";
cin >> input;
piece = input[0];
return piece;
}
Move promptForMove(int maxCoord, bool *exit) {
Vector2D fromPos;
Vector2D toPos;
bool moveValid = false;
while (!moveValid) {
string input;
cout << "What is your next move? fromX,fromY,toX,toY or exit: ";
cin >> input;
if (input == "exit") {
*exit = true;
return {};
}
fromPos.x = stoi(input.substr(0, input.find(",", 0)));
input = input.substr(input.find(",", 0) + 1, input.length());
fromPos.y = stoi(input.substr(0, input.find(",", 0)));
input = input.substr(input.find(",", 0) + 1, input.length());
toPos.x = stoi(input.substr(0, input.find(",", 0)));
input = input.substr(input.find(",", 0) + 1, input.length());
toPos.y = stoi(input.substr(0, input.find(",", 0)));
input = input.substr(input.find(",", 0) + 1, input.length());
if (fromPos.x < 1 || fromPos.x > maxCoord || fromPos.y < 1 || fromPos.y > maxCoord) {
system("cls");
cout << "From value must be in the range (1," << maxCoord << ")\n";
continue;
}
if (toPos.x < 1 || toPos.x > maxCoord || toPos.y < 1 || toPos.y > maxCoord) {
system("cls");
cout << "To value must be in the range (1," << maxCoord << ")\n";
continue;
}
// TODO: Add checks
moveValid = true;
}
return { fromPos, toPos };
}
void doMove(char board[][8], Move move) {
char piece = board[move.from.y - 1][move.from.x - 1];
board[move.from.y - 1][move.from.x - 1] = ' ';
board[move.to.y - 1][move.to.x - 1] = piece;
}
void checkConvertPawn(char board[][8], Move move, Color color) {
if (color == Color::Black && move.to.y == 8) {
convertPawn(board, move.to, color);
}
else if (color == Color::White && move.to.y == 1) {
convertPawn(board, move.to, color);
}
}
void convertPawn(char board[][8], Vector2D pos, Color color) {
if (pos.y != 1 && pos.y != 8) return;
system("cls");
char piece;
bool pieceSelected = false;
while (!pieceSelected) {
piece = promptForPiece();
if (piece != 'q' && piece != 'r' && piece != 'b' && piece != 'n') {
system("cls");
continue;
}
pieceSelected = true;
}
system("cls");
piece = color == Color::Black ? piece : toupper(piece);
board[pos.y - 1][pos.x - 1] = piece;
}
bool ownsPiece(char board[][8], Vector2D pos, Color color) {
if (islower(board[pos.y - 1][pos.x - 1]) && color == Color::Black) {
return true;
}
else if (isupper(board[pos.y - 1][pos.x - 1]) && color == Color::White) {
return true;
}
else {
return false;
}
}
bool isMoveValid(char board[][8], Color color, Move move, string *errMessage) {
if (board[move.from.y - 1][move.from.x - 1] == ' ') {
*errMessage = "There's no piece at that location.";
return false;
}
if (!ownsPiece(board, move.from, color)) {
*errMessage = "You don't own that piece.";
return false;
}
if (move.from.x == move.to.x && move.from.y == move.to.y) {
*errMessage = "Come on, I can't see the difference there.";
return false;
}
switch (tolower(board[move.from.y - 1][move.from.x - 1])) {
case 'p':
if (isPawnMove(move, color)) {
if (isPieceOnSquare(board, move.to)) {
*errMessage = "Target pos is already occupied";
return false;
}
} else if (!(isDiagonalPawn(move, color) && isPieceOnSquare(board, move.to) && !ownsPiece(board, move.to, color))) {
// TODO: ugly if statement
*errMessage = "Move is not permitted";
return false;
}
break;
case 'r':
if (!isStraight(move)) {
*errMessage = "Move is not straight";
return false;
}
if (isRookPathBlocked(board, move)) {
*errMessage = "Move is blocked";
return false;
}
break;
case 'n':
if (!isLShape(move)) {
*errMessage = "Move is not L shape";
return false;
}
break;
case 'b':
if (!isDiagonal(move)) {
*errMessage = "Move is not diagonal";
return false;
}
if (isBishopPathBlocked(board, move)) {
*errMessage = "Move is blocked";
return false;
}
break;
case 'q':
if (!(isDiagonal(move) || isStraight(move))) {
*errMessage = "Move is not diagonal or straight";
return false;
}
if (isQueenPathBlocked(board, move)) {
*errMessage = "Move is blocked";
return false;
}
break;
case 'k':
if (abs(move.to.x - move.from.x) > 1 || abs(move.to.y - move.from.y) > 1) {
*errMessage = "Move is to far";
return false;
}
if (isKingNearby(board, move, color)) {
*errMessage = "Opponents king is nearby";
return false;
}
break;
default:
*errMessage = "Not a valid piece type.";
return false;
}
return true;
}
bool isKingNearby(char board[][8], Move move, Color color) {
Vector2D nearbySquares[8] = {
{ move.to.x - 1, move.to.y },
{ move.to.x + 1, move.to.y },
{ move.to.x, move.to.y - 1 },
{ move.to.x, move.to.y + 1 },
{ move.to.x - 1, move.to.y - 1 },
{ move.to.x - 1, move.to.y + 1 },
{ move.to.x + 1, move.to.y - 1 },
{ move.to.x + 1, move.to.y + 1 }
};
char kingPiece = color == Color::Black ? 'K' : 'k';
for (int i = 0; i < 8; i++) {
if (nearbySquares[i].x > -1 && nearbySquares[i].x < 8 && nearbySquares[i].y > -1 && nearbySquares[i].y < 8) {
cout << board[nearbySquares[i].y - 1][nearbySquares[i].x - 1];
if (board[nearbySquares[i].y - 1][nearbySquares[i].x - 1] == kingPiece) {
return true;
}
}
}
return false;
}
bool isBeat(char board[][8], Move move) {
if (board[move.to.y - 1][move.to.x - 1] != ' ') {
return true;
}
return false;
}
bool isValidBeat(char board[][8], Move move, Color color, string *errMessage) {
if (ownsPiece(board, move.to, color)) {
*errMessage = "You can't beat your own piece";
return false;
}
else {
return true;
}
}
bool isPieceOnSquare(char board[][8], Vector2D square) {
if (board[square.y - 1][square.x - 1] != ' ') {
return true;
}
return false;
}
bool isStraight(Move move) {
if (move.from.x == move.to.x) {
return true;
}
if (move.from.y == move.to.y) {
return true;
}
return false;
}
bool isDiagonal(Move move) {
if (abs(move.to.x - move.from.x) == abs(move.to.y - move.from.y)){
return true;
}
return false;
}
bool isDiagonalPawn(Move move, Color color) {
if (abs(move.from.x - move.to.x) != 1) {
return false;
}
if (color == Color::Black && move.from.y - move.to.y == -1) {
return true;
}
if (color == Color::White && move.from.y - move.to.y == 1) {
return true;
}
return false;
}
bool isLShape(Move move) {
if (abs(move.from.x - move.to.x) == 2 && abs(move.from.y - move.to.y) == 1) {
return true;
}
if (abs(move.from.x - move.to.x) == 1 && abs(move.from.y - move.to.y) == 2) {
return true;
}
return false;
}
bool isPawnMove(Move move, Color color) {
if (move.from.x != move.to.x) {
return false;
}
if (color == Color::Black && move.from.y == 2 && (move.from.y - move.to.y == -1 || move.from.y - move.to.y == -2)) {
return true;
}
if (color == Color::White && move.from.y == 7 && (move.from.y - move.to.y == 1 || move.from.y - move.to.y == 2)) {
return true;
}
if (color == Color::Black && move.from.y - move.to.y == -1) {
return true;
}
if (color == Color::White && move.from.y - move.to.y == 1) {
return true;
}
return false;
}
Direction2D getDirection2D(Move move) {
int dirX = move.to.x > move.from.x ? 1 : -1;
int dirY = move.to.y > move.from.y ? 1 : -1;
return { dirX, dirY };
}
Direction getDirection(Move move) {
if (move.to.x > move.from.x && move.to.y == move.from.y) {
return Direction::Right;
}
else if (move.to.x < move.from.x && move.to.y == move.from.y) {
return Direction::Left;
}
else if (move.to.y > move.from.y && move.to.x == move.from.x) {
return Direction::Down;
}
else if (move.to.y < move.from.y && move.to.x == move.from.x) {
return Direction::Up;
}
else {
return Direction::Undefined;
}
}
bool isRookPathBlocked(char board[][8], Move move) {
switch (getDirection(move))
{
case Direction::Right:
for (int i = 1; i < (move.to.x - move.from.x); i++) {
cout << i;
if (isPieceOnSquare(board, { move.from.x + i, move.from.y })) {
return true;
}
}
break;
case Direction::Left:
for (int i = 1; i < (move.from.x - move.to.x); i++) {
cout << i;
if (isPieceOnSquare(board, { move.from.x - i, move.from.y })) {
return true;
}
}
break;
case Direction::Down:
for (int i = 1; i < (move.to.y - move.from.y); i++) {
cout << i;
if (isPieceOnSquare(board, { move.from.x, move.from.y + i })) {
return true;
}
}
break;
case Direction::Up:
for (int i = 1; i < (move.from.y - move.to.y); i++) {
cout << i;
if (isPieceOnSquare(board, { move.from.x, move.from.y - i })) {
return true;
}
}
break;
default:
break;
}
return false;
}
bool isBishopPathBlocked(char board[][8], Move move) {
Direction2D dir2d = getDirection2D(move);
for (int i = 1; i < abs(move.to.x - move.from.x); i++) {
if (isPieceOnSquare(board, { move.from.x + i * dir2d.x, move.from.y + i * dir2d.y })) {
return true;
}
}
return false;
}
bool isQueenPathBlocked(char board[][8], Move move) {
if (isStraight(move)) {
return isRookPathBlocked(board, move);
}
else {
return isBishopPathBlocked(board, move);
}
}