Chess code
#include
#include
using namespace std;
enum Color { WHITE, BLACK };
enum Type { NONE, PAWN, KNIGHT, BISHOP, ROOK, QUEEN, KING };
struct Piece {
Type type;
Color color;
};
class ChessGame {
public:
ChessGame();
void playGame();
private:
Piece board[8][8];
Color currentPlayer;
bool isGameOver;
void initBoard();
void printBoard() const;
bool handleMove(string move);
bool isValidMove(int srcRow, int srcCol, int destRow, int destCol) const;
bool isKingInCheck(Color color) const;
bool isStalemate(Color color) const;
bool isCheckmate(Color color) const;
void movePiece(int srcRow, int srcCol, int destRow, int destCol);
};
ChessGame::ChessGame() {
initBoard();
currentPlayer = WHITE;
isGameOver = false;
}
void ChessGame::playGame() {
cout << "Welcome to chess!" << endl;
while (!isGameOver) {
printBoard();
if (isKingInCheck(currentPlayer)) {
if (isCheckmate(currentPlayer)) {
cout << "Checkmate! " << (currentPlayer == WHITE ? "Black" : "White") << " wins!" << endl;
isGameOver = true;
break;
}
else {
cout << "Check!" << endl;
}
}
else if (isStalemate(currentPlayer)) {
cout << "Stalemate!" << endl;
isGameOver = true;
break;
}
string move;
do {
cout << (currentPlayer == WHITE ? "White" : "Black") << "'s turn: ";
cin >> move;
} while (!handleMove(move));
currentPlayer = currentPlayer == WHITE ? BLACK : WHITE;
}
}
void ChessGame::initBoard() {
// Set up pawns
for (int col = 0; col < 8; col++) {
board[1][col] = { PAWN, BLACK };
board[6][col] = { PAWN, WHITE };
}
// Set up other pieces
board[0][0] = { ROOK, BLACK };
board[0][1] = { KNIGHT, BLACK };
board[0][2] = { BISHOP, BLACK };
board[0][3] = { QUEEN, BLACK };
board[0][4] = { KING, BLACK };
board[0][5] = { BISHOP, BLACK };
board[0][6] = { KNIGHT, BLACK };
board[0][7] = { ROOK, BLACK };
board[7][0] = { ROOK, WHITE };
board[7][1] = { KNIGHT, WHITE };
board[7][2] = { BISHOP, WHITE };
board[7][3] = { QUEEN, WHITE };
board[7][4] = { KING, WHITE };
board[7][5] = { BISHOP, WHITE };
board[7][6] = { KNIGHT, WHITE };
board[7][7] = { ROOK, WHITE };
// Set all other spaces to NONE
void ChessGame::printBoard() const {
cout << endl << " ";
for (int col = 0; col < 8; col++) {
cout << col << " ";
}
cout << endl;
for (int row = 0; row < 8; row++) {
cout << row << " ";
for (int col = 0; col < 8; col++) {
char symbol;
switch (board[row][col].type) {
case NONE: symbol = ' '; break;
case PAWN: symbol = 'P'; break;
case KNIGHT: symbol = 'N'; break;
case BISHOP: symbol = 'B'; break;
case ROOK: symbol = 'R'; break;
case QUEEN: symbol = 'Q'; break;
case KING: symbol = 'K'; break;
}
if (board[row][col].color == BLACK) {
symbol = tolower(symbol);
}
cout << symbol << " ";
}
cout << row << endl;
}
cout << " ";
for (int col = 0; col < 8; col++) {
cout << col << " ";
}
cout << endl << endl;
}
bool ChessGame::handleMove(string move) {
if (move.length() != 4) {
cout << "Invalid move. Format: 'a1b2'" << endl;
return false;
}
int srcRow = move[1] - '0';
int srcCol = move[0] - 'a';
int destRow = move[3] - '0';
int destCol = move[2] - 'a';
if (srcRow < 0 || srcRow > 7 || srcCol < 0 || srcCol > 7 ||
destRow < 0 || destRow > 7 || destCol < 0 || destCol > 7) {
cout << "Invalid move. Out of bounds." << endl;
return false;
}
if (board[srcRow][srcCol].type == NONE || board[srcRow][srcCol].color != currentPlayer) {
cout << "Invalid move. No piece at source location." << endl;
return false;
}
if (!isValidMove(srcRow, srcCol, destRow, destCol)) {
cout << "Invalid move. Illegal move for this piece." << endl;
return false;
}
movePiece(srcRow, srcCol, destRow, destCol);
return true;
}
bool ChessGame::isValidMove(int srcRow, int srcCol, int destRow, int destCol) const {
Piece srcPiece = board[srcRow][srcCol];
Piece destPiece = board[destRow][destCol];
// Check if destination is occupied by friendly piece
if (destPiece.type != NONE && destPiece.color == srcPiece.color) {
return false;
}
// Check if move is legal for this piece
switch (srcPiece.type) {
case PAWN:
if (srcPiece.color == WHITE) {
if (srcCol == destCol && destRow == srcRow - 1 && destPiece.type == NONE) {
return true;
}
else if (srcCol == destCol && destRow == srcRow - 2 && srcRow == 6 && destPiece.type == NONE && board[srcRow-
bool ChessGame::isInCheck(Color color) const {
int kingRow = -1, kingCol = -1;
for (int row = 0; row < 8; row++) {
for (int col = 0; col < 8; col++) {
Piece piece = board[row][col];
if (piece.type == KING && piece.color == color) {
kingRow = row;
kingCol = col;
break;
}
}
}
if (kingRow == -1 || kingCol == -1) {
return false; // King not found
}
// Check if any enemy piece can attack the king
for (int row = 0; row < 8; row++) {
for (int col = 0; col < 8; col++) {
Piece piece = board[row][col];
if (piece.type != NONE && piece.color != color && isValidMove(row, col, kingRow, kingCol)) {
return true;
}
}
}
return false;
}
bool ChessGame::isInCheck(Color color) const {
int kingRow = -1, kingCol = -1;
for (int row = 0; row < 8; row++) {
for (int col = 0; col < 8; col++) {
Piece piece = board[row][col];
if (piece.type == KING && piece.color == color) {
kingRow = row;
kingCol = col;
break;
}
}
}
if (kingRow == -1 || kingCol == -1) {
return false; // King not found
}
// Check if any enemy piece can attack the king
for (int row = 0; row < 8; row++) {
for (int col = 0; col < 8; col++) {
Piece piece = board[row][col];
if (piece.type != NONE && piece.color != color && isValidMove(row, col, kingRow, kingCol)) {
return true;
}
}
}
return false;
}
int main() {
ChessGame game;
while (!game.isOver()) {
game.printBoard();
cout << "It is " << (game.getCurrentPlayer() == WHITE ? "White" : "Black") << "'s turn." << endl;
string move;
do {
cout << "Enter your move: ";
cin >> move;
} while (!game.handleMove(move));
if (game.isInCheck(game.getCurrentPlayer())) {
cout << "Check!" << endl;
}
game.nextTurn();
}
game.printBoard();
if (game.isCheckmate()) {
cout << "Checkmate! " << (game.getCurrentPlayer() == WHITE ? "Black" : "White") << " wins." << endl;
}
else if (game.isStalemate()) {
cout << "Stalemate. Game is a draw." << endl;
}
return 0;
}
Comments
Post a Comment