-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathchessConsole.cpp
More file actions
403 lines (325 loc) · 12.2 KB
/
chessConsole.cpp
File metadata and controls
403 lines (325 loc) · 12.2 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
#include <iostream>
#include <string>
#include <cctype>
using namespace std;
enum PieceType { EMPTY, PAWN, KNIGHT, BISHOP, ROOK, QUEEN, KING };
enum Color { NONE, WHITE, BLACK };
class Piece {
private:
PieceType type;
Color color;
bool hasMoved;
public:
Piece() : type(EMPTY), color(NONE), hasMoved(false) {}
Piece(PieceType t, Color c) : type(t), color(c), hasMoved(false) {}
PieceType getType() const { return type; }
Color getColor() const { return color; }
bool getHasMoved() const { return hasMoved; }
void setMoved() { hasMoved = true; }
bool isEmpty() const { return type == EMPTY; }
bool isWhite() const { return color == WHITE; }
bool isBlack() const { return color == BLACK; }
char getDisplayChar() const {
if (type == EMPTY) return ' ';
char c;
switch (type) {
case PAWN: c = 'P'; break;
case KNIGHT: c = 'N'; break;
case BISHOP: c = 'B'; break;
case ROOK: c = 'R'; break;
case QUEEN: c = 'Q'; break;
case KING: c = 'K'; break;
default: c = ' '; break;
}
return (color == WHITE) ? c : tolower(c);
}
bool canCapture(const Piece& target) const {
return !target.isEmpty() && target.getColor() != this->color;
}
};
class Position {
private:
int row;
int col;
public:
Position(int r, int c) : row(r), col(c) {}
int getRow() const { return row; }
int getCol() const { return col; }
bool isValid() const {
return row >= 0 && row < 8 && col >= 0 && col < 8;
}
static Position fromNotation(const string& notation) {
int col = notation[0] - 'a';
int row = 8 - (notation[1] - '0');
return Position(row, col);
}
string toNotation() const {
char colChar = 'a' + col;
char rowChar = '1' + (7 - row);
return string(1, colChar) + string(1, rowChar);
}
};
class Move {
private:
Position from;
Position to;
bool isCapture;
public:
Move(Position f, Position t, bool capture = false)
: from(f), to(t), isCapture(capture) {}
Position getFrom() const { return from; }
Position getTo() const { return to; }
string toString() const {
return from.toNotation() + " -> " + to.toNotation();
}
};
class Board {
private:
Piece board[8][8];
public:
Board() {
for (int i = 0; i < 8; i++) {
for (int j = 0; j < 8; j++) {
board[i][j] = Piece();
}
}
}
void setupInitialPosition() {
board[0][0] = Piece(ROOK, BLACK);
board[0][1] = Piece(KNIGHT, BLACK);
board[0][2] = Piece(BISHOP, BLACK);
board[0][3] = Piece(QUEEN, BLACK);
board[0][4] = Piece(KING, BLACK);
board[0][5] = Piece(BISHOP, BLACK);
board[0][6] = Piece(KNIGHT, BLACK);
board[0][7] = Piece(ROOK, BLACK);
for (int i = 0; i < 8; i++) {
board[1][i] = Piece(PAWN, BLACK);
}
board[7][0] = Piece(ROOK, WHITE);
board[7][1] = Piece(KNIGHT, WHITE);
board[7][2] = Piece(BISHOP, WHITE);
board[7][3] = Piece(QUEEN, WHITE);
board[7][4] = Piece(KING, WHITE);
board[7][5] = Piece(BISHOP, WHITE);
board[7][6] = Piece(KNIGHT, WHITE);
board[7][7] = Piece(ROOK, WHITE);
for (int i = 0; i < 8; i++) {
board[6][i] = Piece(PAWN, WHITE);
}
}
void display() const {
const string RESET = "\033[0m";
const string WHITE_SQ = "\033[47m";
const string BLACK_SQ = "\033[100m";
const string BLACK_TEXT = "\033[30m";
const string WHITE_TEXT = "\033[97m";
cout << "\n a b c d e f g h\n";
for (int i = 0; i < 8; i++) {
cout << 8 - i << " ";
for (int j = 0; j < 8; j++) {
bool isLightSquare = (i + j) % 2 == 0;
string bgColor = isLightSquare ? WHITE_SQ : BLACK_SQ;
const Piece& piece = board[i][j];
string textColor = piece.isWhite() ? BLACK_TEXT : WHITE_TEXT;
if (piece.isEmpty()) {
textColor = "";
}
cout << bgColor << textColor << " " << piece.getDisplayChar() << " " << RESET;
}
cout << " " << 8 - i << "\n";
}
cout << " a b c d e f g h\n\n";
}
const Piece& getPieceAt(const Position& pos) const {
return board[pos.getRow()][pos.getCol()];
}
Piece& getPieceAt(const Position& pos) {
return board[pos.getRow()][pos.getCol()];
}
bool isPathClear(const Position& from, const Position& to) const {
int fromRow = from.getRow();
int fromCol = from.getCol();
int toRow = to.getRow();
int toCol = to.getCol();
int rowDir = (toRow > fromRow) ? 1 : (toRow < fromRow) ? -1 : 0;
int colDir = (toCol > fromCol) ? 1 : (toCol < fromCol) ? -1 : 0;
int r = fromRow + rowDir;
int c = fromCol + colDir;
while (r != toRow || c != toCol) {
if (!board[r][c].isEmpty()) return false;
r += rowDir;
c += colDir;
}
return true;
}
bool isValidMove(const Position& from, const Position& to, Color currentPlayer) const {
if (!from.isValid() || !to.isValid()) {
return false;
}
const Piece& fromPiece = getPieceAt(from);
const Piece& toPiece = getPieceAt(to);
if (fromPiece.isEmpty() || fromPiece.getColor() != currentPlayer) {
return false;
}
if (toPiece.getColor() == currentPlayer) {
return false;
}
int fromRow = from.getRow();
int fromCol = from.getCol();
int toRow = to.getRow();
int toCol = to.getCol();
if (fromRow == toRow && fromCol == toCol) {
return false;
}
switch (fromPiece.getType()) {
case PAWN:
return isValidPawnMove(from, to, currentPlayer);
case KNIGHT:
return isValidKnightMove(from, to);
case BISHOP:
return isValidBishopMove(from, to);
case ROOK:
return isValidRookMove(from, to);
case QUEEN:
return isValidQueenMove(from, to);
case KING:
return isValidKingMove(from, to);
default:
return false;
}
}
bool isValidPawnMove(const Position& from, const Position& to, Color color) const {
int fromRow = from.getRow();
int fromCol = from.getCol();
int toRow = to.getRow();
int toCol = to.getCol();
int direction = (color == WHITE) ? -1 : 1;
int startRow = (color == WHITE) ? 6 : 1;
const Piece& toPiece = getPieceAt(to);
if (toCol == fromCol && toRow == fromRow + direction && toPiece.isEmpty()) {
return true;
}
if (toCol == fromCol && fromRow == startRow && toRow == fromRow + 2 * direction) {
Position middle(fromRow + direction, fromCol);
if (toPiece.isEmpty() && getPieceAt(middle).isEmpty()) {
return true;
}
}
if (abs(toCol - fromCol) == 1 && toRow == fromRow + direction && !toPiece.isEmpty()) {
return true;
}
return false;
}
bool isValidKnightMove(const Position& from, const Position& to) const {
int rowDiff = abs(to.getRow() - from.getRow());
int colDiff = abs(to.getCol() - from.getCol());
return (rowDiff == 2 && colDiff == 1) || (rowDiff == 1 && colDiff == 2);
}
bool isValidBishopMove(const Position& from, const Position& to) const {
if (abs(to.getRow() - from.getRow()) != abs(to.getCol() - from.getCol()))
return false;
return isPathClear(from, to);
}
bool isValidRookMove(const Position& from, const Position& to) const {
if (to.getRow() != from.getRow() && to.getCol() != from.getCol())
return false;
return isPathClear(from, to);
}
bool isValidQueenMove(const Position& from, const Position& to) const {
return isValidBishopMove(from, to) || isValidRookMove(from, to);
}
bool isValidKingMove(const Position& from, const Position& to) const {
return abs(to.getRow() - from.getRow()) <= 1 &&
abs(to.getCol() - from.getCol()) <= 1;
}
bool makeMove(const Position& from, const Position& to) {
Piece& fromPiece = getPieceAt(from);
Piece& toPiece = getPieceAt(to);
bool isCapture = !toPiece.isEmpty();
toPiece = fromPiece;
fromPiece = Piece();
toPiece.setMoved();
return true;
}
Position findKing(Color kingColor) const {
for (int i = 0; i < 8; i++) {
for (int j = 0; j < 8; j++) {
const Piece& p = board[i][j];
if (p.getType() == KING && p.getColor() == kingColor) {
return Position(i, j);
}
}
}
return Position(-1, -1);
}
bool isKingInCheck(Color kingColor) const {
Position kingPos = findKing(kingColor);
if (!kingPos.isValid()) return false;
Color opponentColor = (kingColor == WHITE) ? BLACK : WHITE;
for (int i = 0; i < 8; i++) {
for (int j = 0; j < 8; j++) {
Position pos(i, j);
if (getPieceAt(pos).getColor() == opponentColor) {
if (isValidMove(pos, kingPos, opponentColor)) {
return true;
}
}
}
}
return false;
}
};
class Game {
private:
Board board;
Color currentPlayer;
int moveCount;
public:
Game() : currentPlayer(WHITE), moveCount(0) {
board.setupInitialPosition();
}
void play() {
cout << "=== Chess ===\n";
cout << "Commands:\n";
cout << " - Move: e2 e4 (from square to square)\n";
cout << " - Quit: type 'quit'\n\n";
while (true) {
board.display();
cout << "Move " << (moveCount + 1) << " - ";
cout << (currentPlayer == WHITE ? "White" : "Black") << "'s turn\n";
cout << "Enter command: ";
string input;
cin >> input;
if (input == "quit") {
cout << "Thanks for playing!\n";
break;
}
string to;
cin >> to;
if (input.length() != 2 || to.length() != 2) {
cout << "Invalid input format. Use format like: e2 e4\n";
continue;
}
Position fromPos = Position::fromNotation(input);
Position toPos = Position::fromNotation(to);
if (board.isValidMove(fromPos, toPos, currentPlayer)) {
board.makeMove(fromPos, toPos);
moveCount++;
Color opponent = (currentPlayer == WHITE) ? BLACK : WHITE;
if (board.isKingInCheck(opponent)) {
cout << "\n*** " << (opponent == WHITE ? "White" : "Black")
<< " king is in CHECK! ***\n\n";
}
currentPlayer = opponent;
} else {
cout << "Invalid move! Try again.\n";
}
}
}
};
int main() {
Game game;
game.play();
return 0;
}