#include <iostream>
#include <vector>
#include <string>
using namespace std;
// Enum for piece types and colors
enum PieceType { EMPTY, PAWN, ROOK, KNIGHT, BISHOP, QUEEN, KING };
enum PieceColor { NONE, WHITE, BLACK };
// Struct for a chess piece
struct Piece {
PieceType type;
PieceColor color;
};
// Class for the chess board
class ChessBoard {
public:
ChessBoard();
void printBoard();
bool movePiece(int startX, int startY, int endX, int endY);
bool isCheckmate(PieceColor color);
private:
vector<vector<Piece>> board;
bool isValidMove(int startX, int startY, int endX, int endY);
bool isCheck(PieceColor color);
};
// Constructor to initialize the chess board
ChessBoard::ChessBoard() {
board.resize(8, vector<Piece>(8, { EMPTY, NONE }));
// Place pawns
for (int i = 0; i < 8; ++i) {
board[1][i] = { PAWN, WHITE };
board[6][i] = { PAWN, BLACK };
}
// Place rooks
board[0][0] = board[0][7] = { ROOK, WHITE };
board[7][0] = board[7][7] = { ROOK, BLACK };
// Place knights
board[0][1] = board[0][6] = { KNIGHT, WHITE };
board[7][1] = board[7][6] = { KNIGHT, BLACK };
// Place bishops
board[0][2] = board[0][5] = { BISHOP, WHITE };
board[7][2] = board[7][5] = { BISHOP, BLACK };
// Place queens
board[0][3] = { QUEEN, WHITE };
board[7][3] = { QUEEN, BLACK };
// Place kings
board[0][4] = { KING, WHITE };
board[7][4] = { KING, BLACK };
}
// Function to print the chess board
void ChessBoard::printBoard() {
for (int i = 7; i >= 0; --i) {
for (int j = 0; j < 8; ++j) {
char pieceChar = '.';
switch (board[i][j].type) {
case PAWN: pieceChar = (board[i][j].color == WHITE) ? 'P' : 'p'; break;
case ROOK: pieceChar = (board[i][j].color == WHITE) ? 'R' : 'r'; break;
case KNIGHT: pieceChar = (board[i][j].color == WHITE) ? 'N' : 'n'; break;
case BISHOP: pieceChar = (board[i][j].color == WHITE) ? 'B' : 'b'; break;
case QUEEN: pieceChar = (board[i][j].color == WHITE) ? 'Q' : 'q'; break;
case KING: pieceChar = (board[i][j].color == WHITE) ? 'K' : 'k'; break;
default: break;
}
cout << pieceChar << " ";
}
cout << endl;
}
}
// Function to move a piece from one position to another
bool ChessBoard::movePiece(int startX, int startY, int endX, int endY) {
if (isValidMove(startX, startY, endX, endY)) {
board[endX][endY] = board[startX][startY];
board[startX][startY] = { EMPTY, NONE };
return true;
}
return false;
}
// Function to check if a move is valid (simplified)
bool ChessBoard::isValidMove(int startX, int startY, int endX, int endY) {
// For simplicity, only checks if the destination is empty or has an opponent's piece
if (startX < 0 || startX >= 8 || startY < 0 || startY >= 8 || endX < 0 || endX >= 8 || endY < 0 || endY >= 8)
return false;
Piece startPiece = board[startX][startY];
Piece endPiece = board[endX][endY];
if (startPiece.type == EMPTY || startPiece.color == NONE)
return false;
if (endPiece.type != EMPTY && startPiece.color == endPiece.color)
return false;
// For simplicity, assume all moves are valid (you can add specific rules for each piece)
return true;
}
// Function to check if the current player is in checkmate (simplified)
bool ChessBoard::isCheckmate(PieceColor color) {
// Simplified: Assume checkmate if the king of the given color is in check
return isCheck(color);
}
// Function to check if the current player is in check (simplified)
bool ChessBoard::isCheck(PieceColor color) {
// Simplified: Assume the king is in check if an opponent's piece can move to the king's position
return false; // Replace with actual check logic
}
// Main function
int main() {
ChessBoard game;
game.printBoard();
// Example moves
game.movePiece(1, 4, 3, 4); // White pawn moves forward
game.movePiece(6, 4, 4, 4); // Black pawn moves forward
game.printBoard();
return 0;
}