Skip to content

Nidhi-dwivedi/Chess-Game

Repository files navigation

Java Chess Game

A simple chess game implementation in Java with both Human vs Human and Human vs Computer game modes.

Features

  • Human vs Human gameplay
  • Human vs Computer gameplay (computer plays as either White or Black)
  • Standard chess rules implementation
  • Check detection
  • Pawn promotion
  • Visual board representation
  • Turn management

How to Run

  1. Make sure you have Java installed on your system
  2. Compile all Java files:
    javac *.java
    
  3. Run the game:
    java ChessGame
    

Game Controls

  • Click on a piece to select it (it will be highlighted in green)
  • Click on a valid destination square to move the piece
  • The status bar shows whose turn it is and if a king is in check
  • In Human vs Computer mode, the computer will automatically make its move

Project Structure

Core Game Files

  • ChessGame.java - Main class that initializes the game window and handles game mode selection
  • Board.java - Manages the chess board, piece movement, and game state
  • Piece.java - Abstract base class for all chess pieces
  • GameModeDialog.java - Dialog for selecting game mode (Human vs Human or Human vs Computer)

Piece Classes

  • Pawn.java - Implements pawn movement rules including first move and capture
  • Rook.java - Implements rook movement (horizontal and vertical)
  • Knight.java - Implements knight's L-shaped movement
  • Bishop.java - Implements bishop's diagonal movement
  • Queen.java - Implements queen's combined rook and bishop movement
  • King.java - Implements king's movement and check detection

Computer Player

  • ComputerPlayer.java - Implements basic AI for computer player with random valid moves

File Descriptions

ChessGame.java

The main entry point of the application. Creates the game window and initializes the game mode selection dialog.

Board.java

Core game logic class that:

  • Manages the 8x8 chess board
  • Handles piece movement and validation
  • Implements turn management
  • Manages check detection
  • Handles pawn promotion
  • Controls the game's visual representation

Piece.java

Abstract base class that defines:

  • Common properties for all pieces (color, position)
  • Abstract methods for movement validation
  • Helper methods for checking valid positions and piece interactions

GameModeDialog.java

Creates a dialog window that allows players to:

  • Choose between Human vs Human and Human vs Computer modes
  • Select the computer's color (White or Black)

Pawn.java

Implements pawn-specific rules:

  • Forward movement (one or two squares on first move)
  • Diagonal capture
  • Pawn promotion when reaching the opposite end

Rook.java

Implements rook movement:

  • Horizontal and vertical movement
  • No jumping over other pieces

Knight.java

Implements knight movement:

  • L-shaped movement (2 squares in one direction, 1 square perpendicular)
  • Can jump over other pieces

Bishop.java

Implements bishop movement:

  • Diagonal movement
  • No jumping over other pieces

Queen.java

Implements queen movement:

  • Combines rook and bishop movement
  • Most powerful piece on the board

King.java

Implements king movement:

  • One square in any direction
  • Check detection
  • Cannot move into check

ComputerPlayer.java

Implements basic AI:

  • Generates valid moves for computer's pieces
  • Randomly selects from valid moves
  • Ensures moves don't leave the king in check

Future Improvements

  1. Add difficulty levels for computer player
  2. Implement checkmate detection
  3. Add move history
  4. Add save/load game functionality
  5. Improve computer player's strategy
  6. Add sound effects
  7. Add a restart button
  8. Implement castling and en passant moves

About

Java Based Project

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages