-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
571 lines (547 loc) · 22.5 KB
/
main.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
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
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
#include <fstream>
#include <iostream>
#include <istream>
#include <string>
#include <vector>
#include <algorithm>
#include <chrono>
#include <ctime>
#include <sstream>
template <typename T>
struct vec2 {
T x;
T y;
};
template <typename T>
std::ostream& operator << (std::ostream &o, const vec2<T>& p) {
return o << "x: " << p.x << ", y: " << p.y << std::endl;
}
template <typename T>
inline bool operator != (const vec2<T>& p1, const vec2<T>& p2) {
return (p1.x!=p2.x||p1.y!=p2.y);
}
struct BoardPiece {
int type;
int color;
int moveCount = 0;
int lastTurnMoved = 0;
vec2<int> pos;
BoardPiece(int t, int c) {
type = t;
color = c;
}
};
std::ostream& operator<< (std::ostream &o, const BoardPiece& p) {
return o << "Type: " << p.type << ", Color: " << p.color << " LastMoved: " << p.lastTurnMoved << std::endl;
}
struct {
int color = 1;
bool quit = false;
bool inGame = false;
bool isWhite = true;
int turnCount = 0;
bool inCheck = false;
int check = 0;
char promote = ' ';
std::vector<std::string> moveList;
std::vector<std::vector<BoardPiece>> board;
} chess;
void reverse( char *start, char *end )
{
while( start < end )
{
char c = *start;
*start++ = *end;
*end-- = c;
}
}
char *reverse_char( char *start )
{
char *end = start;
while( (end[1] & 0xC0) == 0x80 ) end++;
reverse( start, end );
return( end+1 );
}
void reverse_string( char *string )
{
char *end = string;
while( *end ) end = reverse_char( end );
reverse( string, end-1 );
}
void clearScreen() {
system("clear");
}
void printBoard() {
std::string board = "\n";
// Unicode values for chess pieces, Black pieces then White pieces in order: King, Queen, Bishop, Knight, Rook
// the chessboard fills top to bottom so then the position of 0, 0 would be the top left increasing as it goes
std::string pieces[] = { "\u2654", "\u2655", "\u2657", "\u2658", "\u2656", "\u2659","\u265A", "\u265B", "\u265D", "\u265E", "\u265C", "\u265F"};
for (int i = 0; i<8;i++) {
board+= " +---+---+---+---+---+---+---+---+\n";
board+= std::to_string(8-i) + " |";
for (int j = 0; j<8;j++) {
if (chess.board[i][j].type == 0) {
board+=/*(chess.board[i][j].color?" \u2B1C|":" \u2B1B|")*/" |";
} else board+= " " + pieces[chess.board[i][j].type+(chess.board[i][j].color*6)-1]/*chess.board[i][j].type+(chess.board[i][j].color*6)-1*/+ " |";
}
board+="\n";
}
board+=" +---+---+---+---+---+---+---+---+\n";
board+=" A B C D E F G H \n";
std::vector<char> cstr(board.c_str(), board.c_str() + board.size() + 1);
if (!chess.color)reverse_string(cstr.data());
std::cout << cstr.data();
};
char getInput(std::string question, std::vector<char> valid) {
char c;
do {
std::cout << question << std::endl;
std::cout << "Type a letter: ";
std::cin >> c;
c = toupper(c);
} while (std::find(valid.cbegin(), valid.cend(), c) == valid.cend());
return c;
}
bool itemsInPath(vec2<int> c, vec2<int> d) {
if (c.x == d.x) {
for (int i = c.y + 1; i < d.y; i++) {
if (chess.board[c.x][i].type != 0) {
return true;
}
}
} else if (c.y == d.y) {
for (int i = c.x + 1; i < d.x; i++) {
if (chess.board[i][c.y].type != 0) {
return true;
}
}
} else {
int dx = d.x < c.x ? -1 : 1;
int dy = d.y < c.y ? -1 : 1;
for (int i = 1; i < abs(d.x - c.x); i++) {
if (chess.board[c.x + (i * dx)][c.y + (i * dy)].type != 0) {
return true;
}
}
}
return false;
}
BoardPiece itemInDirection(vec2<int> c, vec2<int> d) {
if (abs(d.x) + abs(d.y) == 2) {
for (int i = 1; i < 8; i++) {
int newX = c.x + (i * d.x);
int newY = c.y + (i * d.y);
if (newX < 0 || newX >= 8 || newY < 0 || newY >= 8) break;
if (chess.board[newX][newY].type != 0) return chess.board[newX][newY];
}
} else if (d.x == 0) {
for (int i = c.y + d.y; (d.y == -1 ? i >= 0 : i < 8); (d.y == -1 ? i-- : i++)) {
if (c.x < 0 || c.x >= 8 || i < 0 || i >= 8) break;
if (chess.board[c.x][i].type != 0) {
return chess.board[c.x][i];
}
}
} else if (d.y == 0) {
for (int i = c.x + d.x; (d.x == -1 ? i >= 0 : i < 8); (d.x == -1 ? i-- : i++)) {
if (i < 0 || i >= 8 || c.y < 0 || c.y >= 8) break;
if (chess.board[i][c.y].type != 0) {
return chess.board[i][c.y];
}
}
}
return BoardPiece(0, 0);
}
void updatePos() {
for (int i = 0; i < 8; i++) {
for (int j = 0; j < 8; j++) {
chess.board[i][j].pos = {i, j};
}
}
}
bool isKingInCheck(bool color) {
vec2<int> kingPos;
std::vector<vec2<int>> knt;
for (int i = 0; i < 8; i++) {
for (int j = 0; j < 8; j++) {
if (chess.board[i][j].type == 1 && chess.board[i][j].color == color) {
kingPos = {i, j};
} else if (chess.board[i][j].type == 4 && chess.board[i][j].color != color) {
knt.push_back({i,j});
}
}
}
for (int i : {1, 0, -1}) {
for (int j : {1, 0, -1}) {
if (abs(i)+abs(j)!=0) {
BoardPiece item = itemInDirection(kingPos, {i, j});
updatePos();
// KNIGHT
// checks L
for (vec2<int> k : knt) if (abs(k.x-kingPos.x)*abs(k.y-kingPos.y)==2) return true;
if (item.type!=0 && item.color!=color) {
int dx = (item.pos.x == kingPos.x ? 0 : item.pos.x < kingPos.x ? -1 : 1);
int dy = (item.pos.y == kingPos.y ? 0 : item.pos.y < kingPos.y ? -1 : 1);
// QUEEN CHECK
// if detected then yea
if (item.type==2) return true;
// BISHOP
// checks if diagonal
if (item.type==3 && !(abs(item.pos.y-kingPos.y)/abs(item.pos.x-kingPos.x)!=1)) return true;
// ROOK
// checks if horizontal
if (item.type==5 && !(abs(dx)+abs(dy)!=1)) return true;
// PAWN
// checks if the king is on the correct side and if it isnt in front of the pawn
if (item.type==6 && (((item.pos.x-kingPos.x>0&&chess.color)||(item.pos.x-kingPos.x<0&&!chess.color)) && abs(kingPos.y-item.pos.y)==1)) return true;
}
}
}
}
return false;
}
bool movePiece(BoardPiece* c, BoardPiece* t, bool color, bool enPassant = false, BoardPiece* ep = nullptr, bool test = false) {
BoardPiece cT = *c;
BoardPiece tT = *t;
BoardPiece epT = BoardPiece(0, 0);
if (ep != nullptr) {
epT = *ep;
if (enPassant) *ep = BoardPiece(0, 0);
}
*t = BoardPiece(0, 0);
std::swap(*c, *t);
updatePos();
// Promotion code
if (t->type == 6 && t->lastTurnMoved != 0 && ((!t->color && t->pos.x == 7) || (t->color && t->pos.x == 0))) {
if (chess.promote == ' ' && !test) chess.promote = getInput("What would you like to promote to? (Q)ueen, (R)ook, (K)night, (B)ishop", {'Q', 'R', 'K', 'B'});
if (chess.promote == 'Q' || test) t->type = 2; // Queen
else if (chess.promote == 'R') t->type = 5; // Rook
else if (chess.promote == 'K') t->type = 4; // Knight
else if (chess.promote == 'B') t->type = 3; // Bishop
}
// Check for king in check after swapping positions
if (isKingInCheck(color)) {
std::swap(*c, *t);
*t = tT;
*c = cT;
if (enPassant && ep != nullptr) *ep = epT;
return false;
} else if (test) {
std::swap(*c, *t);
*t = tT;
*c = cT;
if (enPassant && ep != nullptr) *ep = epT;
return true;
}
if (chess.promote != ' ') chess.moveList.push_back(std::string(&chess.promote));
t->moveCount++;
t->lastTurnMoved = chess.turnCount;
return true;
}
vec2<bool> isValidMove(vec2<int> currentPos, vec2<int> targetedPos, bool color) {
BoardPiece* current = &chess.board[currentPos.x][currentPos.y];
BoardPiece* target = &chess.board[targetedPos.x][targetedPos.y];
if (current->type != 0 && color == current->color && currentPos != targetedPos && currentPos.x < 8 && currentPos.x > -1 && currentPos.y < 8 && currentPos.y > -1 && targetedPos.x < 8 && targetedPos.x > -1 && targetedPos.y < 8 && targetedPos.y > -1) {
switch (current->type) {
// KING
case 1: {
// King can only move one square at a time so yea
if (abs(targetedPos.x-currentPos.x) > 1 || abs(targetedPos.y-currentPos.y) > 1) return {false, false};
if (!itemsInPath(currentPos, targetedPos) && (target->color!=color || target->type==0)) {
return {true, false};
}
return {false, false};
break;
}
// QUEEN
case 2: {
int dx = (targetedPos.x == currentPos.x ? 0 : targetedPos.x < currentPos.x ? -1 : 1);
int dy = (targetedPos.y == currentPos.y ? 0 : targetedPos.y < currentPos.y ? -1 : 1);
if (abs(dx)+abs(dy)!=1 && abs(targetedPos.y-currentPos.y)/abs(targetedPos.x-currentPos.x)!=1) return {false, false};
if (!itemsInPath(currentPos, targetedPos) && (target->color!=color || target->type==0)) {
return {true, false};
}
return {false, false};
break;
}
// BISHOP
case 3: {
int dx = (targetedPos.x == currentPos.x ? 0 : targetedPos.x < currentPos.x ? -1 : 1);
int dy = (targetedPos.y == currentPos.y ? 0 : targetedPos.y < currentPos.y ? -1 : 1);
if (abs(targetedPos.y-currentPos.y) == 0 || abs(targetedPos.x-currentPos.x)==0) return {false, false};
if (abs(targetedPos.y-currentPos.y)/abs(targetedPos.x-currentPos.x)!=1) return {false, false};
if (!itemsInPath(currentPos, targetedPos) && (target->color!=color || target->type==0)) {
return {true, false};
}
return {false, false};
break;
}
// KNIGHT
case 4: {
if (abs(targetedPos.x-currentPos.x)*abs(targetedPos.y-currentPos.y)!=2 || (target->color==color && target->type!=0)) return {false, false};
return {true, false};
}
// ROOK
case 5: {
int dx = (targetedPos.x == currentPos.x ? 0 : targetedPos.x < currentPos.x ? -1 : 1);
int dy = (targetedPos.y == currentPos.y ? 0 : targetedPos.y < currentPos.y ? -1 : 1);
if (abs(dx)+abs(dy)==2) return {false, false};
//strange needed to be normal
if (itemsInPath(currentPos, targetedPos) && (target->color!=color || target->type==0)) {
return {true, false};
}
return {false, false};
break;
}
// PAWN
case 6: {
//check if moving backwards
if ((targetedPos.x-currentPos.x>0&&color)||(targetedPos.x-currentPos.x<0&&!color)) return {false, false};
//checks if x(y) movement is equal to one or 2 if hasnt moved
int movement = abs(targetedPos.x-currentPos.x);
if (movement > 2||(current->moveCount!=0&&movement==2)) return {false, false};
//non en passant/capture check
if (targetedPos.y == currentPos.y) {
if (target->type != 0 || itemsInPath(currentPos, targetedPos)) return {false, false};
return {true, false};
//en passant/capture code
} else {
//moving too far left and right
if (abs(targetedPos.y-currentPos.y) > 1) return {false, false};
int dy = targetedPos.y-currentPos.y;
int dx = targetedPos.x-currentPos.x;
//en passant checks if there is a pawn next to us, if it has only moved once, and if the current position is in the 5th or 6th row, also if it has moved in the last turn, adn also if it is a different color
if (chess.board[currentPos.x][currentPos.y+dy].type==6 && chess.board[currentPos.x][currentPos.y+dy].moveCount==1 && (currentPos.x == 3 || currentPos.x == 4) && chess.board[currentPos.x][currentPos.y+dy].lastTurnMoved+1==chess.turnCount && chess.board[currentPos.x][currentPos.y+dy].color != color && target->type==0) {
return {true, true};
}
if (target->type !=0 && target->color != color && targetedPos.x != currentPos.x) {
return {true, false};
}
return {false, false};
}
break;
}
}
return {true, false};
} else return {false, false};
}
// loop through all the possible pieces and all the possible moves on the board to see if there is a valid move that does not put the king in check
// calls the movepiece on test mode which doesnt modify any of the values
bool hasValidMoves(bool color) {
for (int i = 0; i < 8; i++) {
for (int j = 0; j < 8; j++) {
if (chess.board[i][j].type != 0 && chess.board[i][j].color == color) {
//gets all the pieces of the color
for (int k = 0; k < 8; k++) {
for (int l = 0; l < 8; l++) {
//problem with is valid move
vec2<bool> v = isValidMove({i, j}, {k, l}, color);
if (v.x) {
//tests if the current move can keep the color's king safe
if (movePiece(&chess.board[i][j], &chess.board[k][l], color, true, &chess.board[i][j+(l-j)], true)) {chess.promote = ' ';return true;}
}
}
}
}
}
}
return false;
}
void parseMoves(std::string m = "") {
std::string move;
bool isValid = false;
do {
if (m=="") {
if (chess.inCheck && chess.check == chess.color) std::cout << "You are in check!" << std::endl;
std::cout << "Enter a valid move (examples of notation: E4-E5 or g3-f5): ";
//std::cin.ignore();
std::getline(std::cin >> std::ws, move);
for (char & c: move) c = toupper(c);
} else {
move=m;
}
//
// IMPORTANT X AND Y ARE SWAPPED DUE TO HOW THE ROWS WORK!!!!
// DOWN = MORE X
// RIGHT = MORE Y
//
if (move.size() != 5) continue;
if (move.at(2) != '-' || move.size() != 5) continue;
vec2<int> currentPos = {abs(atoi(&move.at(1))-8), int(move.at(0))-65 };
vec2<int> targetedPos = {abs(atoi(&move.at(4))-8), int(move.at(3))-65 };
vec2<bool> r = isValidMove(currentPos, targetedPos, chess.color);
if (r.x) {
isValid = movePiece(&chess.board[currentPos.x][currentPos.y], &chess.board[targetedPos.x][targetedPos.y], chess.color, r.y, &chess.board[currentPos.x][currentPos.y+(targetedPos.y-currentPos.y)]);
}
} while (!isValid);
// first checks if the opposite king is in check then checks if the other side has valid moves
//
chess.moveList.push_back(move);
if (isKingInCheck(!chess.color)) {
chess.inCheck = true;
chess.check = !chess.color;
if (!hasValidMoves(!chess.color)) {
printBoard();
std::cout << (chess.color==1?"White":"Black") << " won the game!" << std::endl;
chess.quit=true;
return;
}
} else {
chess.inCheck = false;
if (!hasValidMoves(!chess.color)) {
printBoard();
std::cout << "Stalemate! Nobody won!" << std::endl;
chess.quit=true;
return;
}
}
return;
}
void initBoard(bool isWhite) {
chess.board = {{}, {}, {}, {}, {}, {}, {}, {}};
chess.color = 1;
std::vector<BoardPiece> base = {BoardPiece(5, 1), BoardPiece(4, 1), BoardPiece(3, 1), BoardPiece(2, 1), BoardPiece(1, 1), BoardPiece(3, 1), BoardPiece(4, 1), BoardPiece(5, 1)};
std::vector<BoardPiece> pawns = {BoardPiece(6, 1),BoardPiece(6, 1), BoardPiece(6, 1), BoardPiece(6, 1), BoardPiece(6, 1), BoardPiece(6, 1), BoardPiece(6, 1), BoardPiece(6, 1)};
chess.isWhite = isWhite;
for (BoardPiece p : base) {
p.color = 0;
chess.board.at(0).push_back(p);
}
for (BoardPiece p : pawns) {
p.color = 0;
chess.board.at(1).push_back(p);
}
isWhite = !isWhite;
for (int i = 2; i < 6; i++) {
for (int j = 0; j < 8; j++) {
chess.board.at(i).push_back(BoardPiece(0, isWhite));
isWhite = !isWhite;
}
isWhite = !isWhite;
}
for (BoardPiece p : pawns) {
p.color = 1;
chess.board.at(6).push_back(p);
}
for (BoardPiece p : base) {
p.color = 1;
chess.board.at(7).push_back(p);
}
}
int main() {
std::cout << "\
██████╗██╗ ██╗███████╗███████╗███████╗\n\
██╔════╝██║ ██║██╔════╝██╔════╝██╔════╝\n\
██║ ███████║█████╗ ███████╗███████╗\n\
██║ ██╔══██║██╔══╝ ╚════██║╚════██║\n\
╚██████╗██║ ██║███████╗███████║███████║\n\
╚═════╝╚═╝ ╚═╝╚══════╝╚══════╝╚══════╝" << std::endl;
initBoard(true);
printBoard();
while (!chess.quit) {
if (chess.inGame) std::cout << "It is " << (chess.color?"White's":"Black's") << " turn!" << std::endl;
if (chess.inCheck && chess.check == chess.color) std::cout << "You are in check!" << std::endl;
char r = getInput(std::string("What would you like to do? ") + (!chess.inGame?"(N)ew Game":"(F)orfeit Game") + " (M)ove (U)ndo (S)ave (L)oad (Q)uit", {'N', 'F', 'M', 'U', 'S', 'L', 'Q'});
if (r == 'N') {
if (chess.inGame) {
std::cout << "You are already in a game! Enter F to quit!" << std::endl;
continue;
}
else {
char c = getInput("Do you want to be (W)hite or (B)lack?", {'W', 'B'});
clearScreen();
initBoard((c == 'W'));
printBoard();
chess.inGame = true;
}
} else if (r == 'F') {
if (!chess.inGame) {
std::cout << "You are not in a game! Enter N to start a new one!" << std::endl;
continue;
}
else {
std::cout << (chess.color==1?"White":"Black") << " has forfeited the game!" << std::endl;
std::cout << (chess.color!=1?"White":"Black") << " won the game!" << std::endl;
chess.inGame = false;
}
} else if (r == 'M') {
if (!chess.inGame) {
std::cout << "You are not in a game! Enter N to start a new one!" << std::endl;
continue;
} else {
parseMoves();
clearScreen();
chess.color = !chess.color;
chess.turnCount++;
printBoard();
}
} else if (r == 'S') {
if (!chess.inGame) {
std::cout << "You are not in a game! Enter N to start a new one!" << std::endl;
continue;
} else {
std::cout << "The file will be saved in this directory.\nEnter a name for the file: ";
//std::cin.ignore();
std::string fn;
std::getline(std::cin >> std::ws, fn);
char t[20];
time_t now = time(NULL);
strftime(t, 20, "%Y-%m-%d %H:%M:%S", localtime(&now));
if (fn.size() <= 0) fn = t;
std::ofstream f;
f.open(fn + ".txt");
f << t << "\n";
f << chess.isWhite << "\n";
for (int i = 0; i < chess.moveList.size(); i++) {
std::cout << chess.moveList[i] << std::endl;
f << chess.moveList[i] << "\n";
}
f.close();
std::cout << "Saved current game to '" << fn << ".txt' successfully!" << std::endl;
}
} else if (r == 'L') {
if (chess.inGame) {
std::cout << "You are already in a game! Enter Q to quit!" << std::endl;
continue;
} else {
std::cout << "The game will be loaded from a file.\nEnter the path of the file: ";
//std::cin.ignore();
std::string fn, g;
std::getline(std::cin >> std::ws, fn);
std::ifstream f;
f.open(fn, f.in);
std::ostringstream t;
if (!f.rdbuf()->is_open()) {
std::cout << "The file does not exist!" << std::endl;
continue;
}
t << f.rdbuf();
g = t.str();
size_t p = 0;
std::string tk;
std::vector<std::string> mv;
while ((p = g.find("\n")) != std::string::npos) {
mv.push_back(g.substr(0, p));
g.erase(0, p + std::string("\n").length());
}
mv.erase(mv.begin());
chess.isWhite = mv[0] == "1";
mv.erase(mv.begin());
std::cout << "Loading the game..." << std::endl;
initBoard(chess.isWhite);
chess.inGame=true;
for (std::string s : mv) {
if (s.size() == 1) {
chess.promote = *s.c_str();
continue;
};
parseMoves(s);
clearScreen();
chess.color = !chess.color;
chess.turnCount++;
printBoard();
}
}
} else break;
};
return 0;
}