-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGameManager.cpp
More file actions
162 lines (122 loc) · 4.12 KB
/
Copy pathGameManager.cpp
File metadata and controls
162 lines (122 loc) · 4.12 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
//
// Created by sahoo on 23-06-2026.
//
#include "GameManager.h"
#include <iostream>
#include "MoveValidator.h"
#include "ChessBoard.h"
#include "KingValidator.h"
#include "PawnValidator.h"
#include "Utils.h"
GameManager::GameManager() {
gameIsRunning = true;
}
// runs the game
void GameManager::run() {
board.printBoard();
// game loop
while (gameIsRunning) {
displayTurn();
bool moveCompleted = processTurn();
if (moveCompleted && gameIsRunning) {
// clears terminal
system("cls");
// next player (opponent) after the move
bool isNextPlayerWhite = !board.isWhiteTurn();
// notify if the opponent's king is in check
if (KingValidator::isKingInCheck(board, isNextPlayerWhite)) {
std::cout << std::endl << (isNextPlayerWhite ? "White" : "Black") << " king is in check!" << std::endl;
}
board.printBoard();
board.switchTurn();
}
}
std::cout << "Game exited!!" << std::endl;
}
// processes input validation, move validation, moving pieces
// returns true if the current player's move is successfully completed
bool GameManager::processTurn() {
std::string startPosInput;
int startPos;
// input loop for valid input format and valid piece selection
while (true) {
startPosInput = getInput("Enter piece position ('x' to exit): ");
// exits the game
if (startPosInput == "x") {
gameIsRunning = false;
return false;
}
// validates user input format
if (!MoveValidator::isValidPosition(startPosInput)) {
continue;
}
startPos = Utils::coordinateToPosition(startPosInput);
if (!board.isValidPiece(board.isWhiteTurn(), startPos)) {
std::cout << "Invalid piece selected!" << std::endl;
continue;
}
break;
}
Piece selectedPiece = board.getPieceAt(startPos);
std::cout << "Piece selected: " << pieceToCharMap[selectedPiece] << std::endl;
std::string endPosInput;
int endPos;
// second input loop for validating input format and legal move
while (true) {
endPosInput = getInput("Enter piece position ('x' to exit or 'b' to unselect): ");
// exits the game
if (endPosInput == "x") {
gameIsRunning = false;
return false;
}
// lets player select another piece
if (endPosInput == "b") {
std::cout << "Piece unselected!" << std::endl;
return false;
}
// validates user input format
if (!MoveValidator::isValidPosition(endPosInput)) {
continue;
}
endPos = Utils::coordinateToPosition(endPosInput);
// checks if move is legal
if (!MoveValidator::isValidMove(board, startPos, endPos)) {
continue;
}
break;
}
std::cout << "Moving piece from "<< startPos << " to " << endPos << std::endl;
Piece pieceMoved = board.getPieceAt(startPos);
bool isCastling = (pieceMoved == whiteKing || pieceMoved == blackKing) &&
(std::abs((startPos & 7) - (endPos & 7)) == 2);
if (isCastling) {
board.executeCastle(startPos, endPos, board.isWhiteTurn());
} else {
// moves the piece
board.makeMove(startPos, endPos);
}
// handles pawn promotion after move
if (pieceMoved == whitePawn || pieceMoved == blackPawn) {
int rank = endPos >> 3;
// white pawn reaches 8th rank or black pawn reaches 1st rank
if (rank == 7 || rank == 0) {
PawnValidator::promotePawn(board, endPos, (pieceMoved == whitePawn));
}
}
return true;
}
// takes input from player
std::string GameManager::getInput(const std::string &prompt) {
std::string input;
std::cout << prompt;
std::cin >> input;
return input;
}
// displays the current player's turn
void GameManager::displayTurn() {
if (board.isWhiteTurn()) {
std::cout << "White's Turn" << std::endl;
} else {
std::cout << "Black's Turn" << std::endl;
}
}