Chess Game

Difficulty: MEDIUMCategory: LLDFrequency: 1Asked On 2025-04-26

Problem Statement

Design and implement a Chess game using object-oriented principles. The focus is on modular design, extensibility, and enforcing game rules, not just low-level implementation.


✅ Functional Requirements


1.Game Setup
•8x8 chessboard with 64 squares.
•Two players: White and Black.
•Standard chess pieces: King, Queen, Rook, Bishop, Knight, Pawn.
2.Piece Movements
•Each piece moves according to standard chess rules.
•Handle special moves:
•Castling
•En Passant
•Pawn Promotion
3.Game Rules & State
•Check/checkmate detection.
•Turn-based moves.
•Valid move verification.
•Draw conditions (stalemate, threefold repetition, 50-move rule, insufficient material).
4.User Interaction (Optional)
•Input moves (e.g., “e2 to e4”)
•Undo/Redo moves.
•Display board state.


🔐 Constraints & Assumptions


•Focus on object-oriented design and modular architecture.
•Ignore AI or multiplayer network handling for basic LLD.
•Board coordinates: (row, column) or algebraic notation.
•Game should enforce valid moves and turn order.


🧱 Key Components & Classes


1.Piece (Abstract Base Class)
1 2class Piece { 3protected: 4 Color color; 5 Position position; 6public: 7 virtual vector<Position> getValidMoves(Board& board) = 0; 8 Color getColor(); 9 Position getPosition(); 10};
1.Derived Piece Classes
1 2class King : public Piece { ... }; 3class Queen : public Piece { ... }; 4class Rook : public Piece { ... }; 5class Bishop : public Piece { ... }; 6class Knight : public Piece { ... }; 7class Pawn : public Piece { ... };
1.Board Class
1 2class Board { 3private: 4 vector<vector<Piece*>> grid; // 8x8 5public: 6 void placePiece(Piece* piece, Position pos); 7 bool movePiece(Position from, Position to); 8 bool isCheck(Color color); 9 bool isCheckmate(Color color); 10};
1.Game Class
1 2class Game { 3private: 4 Player white; 5 Player black; 6 Board board; 7 Color turn; 8public: 9 void start(); 10 bool makeMove(Position from, Position to); 11 void switchTurn(); 12};
1.Player Class
1 2class Player { 3private: 4 Color color; 5public: 6 Color getColor(); 7};


🔄 Example Workflow


1.Initialize game → set up board with pieces.
2.White player moves pawn e2 → e4 → validate move → update board.
3.Black player moves pawn e7 → e5 → validate → update board.
4.Continue turns until checkmate, stalemate, or draw.


🧪 Expected Deliverables


•Class diagram showing Piece hierarchy, Board, Game, and Player.
•Implementation of getValidMoves for at least basic pieces (Pawn, Rook, Knight, Bishop).
•Test cases:
•Move validation (valid/invalid moves)
•Turn enforcement
•Check/checkmate detection
•Special moves like castling and pawn promotion


💡 Discussion Points


•Trade-offs between object-oriented design vs procedural board representation.
•Extensibility for AI, networked multiplayer, or GUI.
•Handling game state history for undo/redo.
•Efficient move validation (e.g., for check detection).


Solution

Comments