Skip to content

deep-n-patel/Chess-Game

Repository files navigation

Chess Game Project

Overview

This program is a text-based Chess game written in Java. It allows a human player to play chess against the computer using the terminal. The project demonstrates object-oriented design, game state management, move validation, and basic artificial intelligence techniques.

The game supports two AI difficulties:

  • EasyAI – makes simple, mostly random legal moves.
  • HarderAI – uses a shallow minimax algorithm to choose stronger moves based on material evaluation.

The game ends when a king is captured.

High-Level Architecture

The program is split into several logical components:

  1. Game Control & Flow – manages turns, input, and win conditions
  2. Board Representation – stores the chess board and pieces
  3. Pieces & Move Validation – enforces chess rules
  4. AI Players – decide moves for the computer
  5. Display – prints the board to the terminal

How to Run

  1. Compile all .java files
  2. Run A3Main.java
  3. Follow the on-screen prompts to enter moves

Moves are entered using board coordinates (row and column numbers).

Main Entry Point

A3Main.java

This is the starting point of the program. It:

  • Creates the game controller
  • Selects the display type (text-based)
  • Starts the game loop

Running this file launches the chess game in the terminal.

Game Control

ChessController.java

Controls the overall game flow. Responsibilities include:

  • Alternating turns between the human player and the AI
  • Requesting moves from players
  • Passing moves to the game logic
  • Detecting when the game is over

ChessGameLogic.java

Handles all game rules, including:

  • Validating moves
  • Moving pieces on the board
  • Capturing pieces
  • Pawn promotion
  • Ending the game when a king is captured

This class is the authority on what is legal in the game.

Board Representation

Board.java

Represents the chess board as an 8×8 grid of Spot objects.

Spot.java

Each Spot represents a single square on the board and contains:

  • A row and column
  • A reference to the Piece occupying it (or null if empty)

Pieces and Movement

Piece.java

An abstract base class shared by all chess pieces. It defines:

  • Which player the piece belongs to (whosePiece)
  • The method isValidMove(...), which each piece must implement

Concrete Piece Classes

Each chess piece has its own class that enforces its movement rules:

  • Pawn.java
  • Rook.java
  • Knight.java
  • Bishop.java
  • Queen.java
  • King.java

Move validation is handled inside each piece, following object-oriented principles.

Move Representation

Move.java

Encapsulates a chess move, including:

  • Starting row and column
  • Destination row and column
  • Flags for resignation and pawn promotion
  • The piece involved in the move

This allows moves to be passed cleanly between the controller, logic, and AI.

Players

ChessPlayer.java

An interface implemented by all player types (human or AI). It defines:

  • makeMove(...) – returns the next move for that player

Human Player

The human player provides input through the terminal.

EasyAI.java

A basic computer opponent that:

  • Generates all legal moves
  • Chooses one with minimal strategy

This AI is useful for testing and learning the game.

HarderAI.java

A stronger AI that uses a simple minimax algorithm:

  • Searches a few moves ahead
  • Assumes the human plays optimally
  • Evaluates board positions using material values

This AI provides a more challenging opponent while remaining efficient.

Artificial Intelligence Details

Minimax (HarderAI)

The minimax algorithm:

  1. Generates all legal moves for the AI
  2. Simulates each move on a copied board
  3. Recursively evaluates human replies
  4. Assigns scores based on material advantage
  5. Selects the move with the highest score

Piece values used for evaluation:

  • Pawn: 100
  • Knight: 320
  • Bishop: 330
  • Rook: 500
  • Queen: 900
  • King: very large value (game-ending)

The search depth is intentionally shallow to keep the game responsive.

Display

GameDisplay.java

An interface for displaying the board.

TextGameDisplay.java

A terminal-based implementation that:

  • Prints the chess board in ASCII format
  • Updates after every move

Design Highlights

  • Object-oriented design with clear separation of responsibilities
  • Encapsulated move validation per piece
  • Simple but effective AI using minimax
  • Clean board cloning for AI simulations
  • Text-based interface for simplicity and portability

About

Simple text-based chess game written in Java

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages