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 |
#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; } |
Explanation:
- Piece Structure:
- The
Piece
struct represents a chess piece with atype
(e.g., pawn, rook, etc.) and acolor
(white or black).
- The
- ChessBoard Class:
- The
ChessBoard
class manages the game board, checks move validity, and provides functions for printing the board and making moves.
- The
- Board Initialization:
- The constructor initializes the board with the standard chess setup, placing pawns, rooks, knights, bishops, queens, and kings in their respective starting positions.
- Print Board:
- The
printBoard
function displays the current state of the chess board in the console, using characters to represent pieces (e.g., ‘P’ for white pawn, ‘r’ for black rook).
- The
- Move Piece:
- The
movePiece
function handles moving a piece from one position to another. It first checks if the move is valid and then updates the board.
- The
- Move Validation:
- The
isValidMove
function is a simplified version that only checks if the destination is within bounds and does not contain a piece of the same color. You can expand this to include specific rules for each type of piece.
- The
- Check and Checkmate:
- The
isCheck
andisCheckmate
functions are placeholders for determining if a player is in check or checkmate. These functions would need to be implemented with actual game logic.
- The
- Main Function:
- The
main
function initializes the game, prints the board, and demonstrates a couple of moves (moving pawns forward).
- The
Possible Enhancements:
- Complete Move Validation: Implement complete move validation for each piece (e.g., pawns moving diagonally to capture, rooks moving in straight lines, etc.).
- Check and Checkmate Logic: Implement the logic to check if a player’s king is in check and if the game has reached checkmate.
- User Interaction: Allow players to input their moves and handle turns between white and black.
- Graphical Interface: Integrate a graphical interface for a more interactive experience.