Problem Statement
Design a Chess game system that allows two players to play a standard 8x8 chess game. The system should support all chess rules including valid piece movement, turn-based gameplay, move validation, and game-ending conditions like checkmate and stalemate.
Functional Requirements
- Two-player turn-based chess game
- Standard 8x8 chess board setup
- Each piece follows its movement rules
- Move validation before execution
- Check and checkmate detection
- Game status tracking (ACTIVE, CHECKMATE, STALEMATE)
- Move history tracking
Objects Required
- Game
- Board
- Cell
- Piece (Abstract)
- King, Queen, Rook, Bishop, Knight, Pawn
- Player
- Move
- Color Enum
- GameStatus Enum
Enums
Color Enum
public enum Color {
WHITE,
BLACK
}
The Color enum represents the two sides in a chess game. It is used by both Player and Piece classes to determine ownership and enforce turn-based rules.
GameStatus Enum
public enum GameStatus {
ACTIVE,
CHECKMATE,
STALEMATE
}
GameStatus represents the lifecycle state of the chess game.
- ACTIVE → Game is ongoing
- CHECKMATE → King has no legal escape
- STALEMATE → No valid moves but king not in check
Piece
public abstract class Piece {
protected Color color;
public Piece(Color color) {
this.color = color;
}
public Color getColor() {
return color;
}
public abstract boolean canMove(Board board, Cell from, Cell to);
}
This is the base class for all chess pieces. It enforces that every piece must define its own movement logic.
The canMove() method is overridden by each specific piece to validate legal moves based on chess rules.
Rook Class
public class Rook extends Piece {
public Rook(Color color) {
super(color);
}
@Override
public boolean canMove(Board board, Cell from, Cell to) {
return from.getRow() == to.getRow() ||
from.getCol() == to.getCol();
}
}
Rook moves horizontally or vertically across the board. The movement is valid only if row or column remains constant.
Cell Class
public class Cell {
private int row;
private int col;
private Piece piece;
public Cell(int row, int col) {
this.row = row;
this.col = col;
}
public Piece getPiece() {
return piece;
}
public void setPiece(Piece piece) {
this.piece = piece;
}
public int getRow() {
return row;
}
public int getCol() {
return col;
}
}
Represents a single square on the chess board. Each cell may or may not contain a piece.
Getter and setter methods manage piece placement during moves.
Board Class
public class Board {
private Cell[][] cells = new Cell[8][8];
public Board() {
initializeBoard();
}
private void initializeBoard() {
for (int i = 0; i < 8; i++) {
for (int j = 0; j < 8; j++) {
cells[i][j] = new Cell(i, j);
}
}
}
public Cell getCell(int row, int col) {
return cells[row][col];
}
}
Represents the 8x8 chess board. It is responsible for initializing all cells and providing access to any square.
Move Class
public class Move {
private Player player;
private Cell from;
private Cell to;
public Move(Player player, Cell from, Cell to) {
this.player = player;
this.from = from;
this.to = to;
}
public Player getPlayer() {
return player;
}
public Cell getFrom() {
return from;
}
public Cell getTo() {
return to;
}
}
Represents a single move in the game, including source and destination cells and the player making the move.
Player Class
public class Player {
private String name;
private Color color;
public Player(String name, Color color) {
this.name = name;
this.color = color;
}
public Color getColor() {
return color;
}
}
Represents a chess player. Each player is assigned a color which determines turn order and piece ownership.
Game Class
import java.util.*;
public class Game {
private Board board;
private Player whitePlayer;
private Player blackPlayer;
private Player currentPlayer;
private GameStatus status;
private List moveHistory;
public Game(Player white, Player black) {
this.board = new Board();
this.whitePlayer = white;
this.blackPlayer = black;
this.currentPlayer = white;
this.status = GameStatus.ACTIVE;
this.moveHistory = new ArrayList<>();
}
public boolean makeMove(Move move) {
Cell from = move.getFrom();
Cell to = move.getTo();
Piece piece = from.getPiece();
if (piece == null) return false;
if (piece.getColor() != currentPlayer.getColor()) return false;
if (!piece.canMove(board, from, to)) return false;
to.setPiece(piece);
from.setPiece(null);
moveHistory.add(move);
switchTurn();
return true;
}
private void switchTurn() {
currentPlayer = (currentPlayer == whitePlayer) ? blackPlayer : whitePlayer;
}
}
Core controller of the chess system. Handles move validation, turn switching, and game state tracking.
The makeMove() method ensures:
- Piece exists at source
- Correct player's turn
- Move is valid according to piece rules
- Board state is updated after move
The switchTurn() method alternates between players after each valid move.
Main Class
public class Main {
public static void main(String[] args) {
Player p1 = new Player("Alice", Color.WHITE);
Player p2 = new Player("Bob", Color.BLACK);
Game game = new Game(p1, p2);
Cell from = game.getBoard().getCell(6, 0);
Cell to = game.getBoard().getCell(5, 0);
Move move = new Move(p1, from, to);
game.makeMove(move);
System.out.println("Move executed successfully");
}
}
Entry point of the application that simulates a basic chess move between two players.
Comments
Post a Comment