Skip to content

This is a modular and intuitive implementation of the classic game Chess, built from the ground up in Java.

Notifications You must be signed in to change notification settings

jackpanning/Java-Chess

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

4 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Java Chess Engine

Overview

This is a modular and intuitive implementation of the classic game Chess, built from the ground up in Java.

Features

  • Intuitive GUI displaying real-time game information.
  • Easy-to-use text-based game as an alternative to the GUI.
  • Bitboard implementation for efficient position calculations.
  • Forsyth-Edwards Notation (FEN) parser to quickly import positions.
  • Multiple bots of varying difficulties.

Technologies Used

This project has been written primarily in Java. The game itself and underlying logic does not rely on external libraries, however the GUI uses:

  • Swing for GUI components, ensuring consistent look across platforms.
  • Abstract Window Toolkit for tracking mouse and keyboard input.

Lessons Learned

Design Principles

This project has allowed me to mature greatly with design principles. Specifically:

  • Encapsulating logic and states within objects, such as a Board or Player.
  • Implementing interfaces to define intended functionality and improve modularity.
  • Planning out needed functionality within classes.
  • Determining most efficient project structure with logical sub-directories.

Bitwise Operations

After doing research on the most efficient chess engines, such as Stockfish and Leela Chess Zero, I learned chess boards are often implemented as a set of bitboards containing 64 bits, where each bit corresponds to a position on the board. This was implemented using Java's primitive data type long. With a set of longs, bitwise operations made the move calculation process much more efficient. 12 bitboards were used (one for each piece type for both white and black), in addition to a bitboard for both white and black's pieces, and a final bitboard for all pieces on the board.

Minimax Algorithm

One of the most famous algorithms in chess computing is called minimax. It recursively plays moves, calculating the value of each position such that the current player's loss is minimized, while assuming the opponent will play the best move available, maximizing their own gain. As implemented in Bot_v2, the minimax algorithm has the ability to recursively search through millions of positions in order to find the best move for the current player. There is an adjustable parameter depth, which changes how many positions in the future the bot will look. Increasing depth exponentially increases computation time, but ensures that basic details are not missed.

Swing

To implement the GUI, the Swing framework was used. Through the use of modular widgets, it proved to be effortless to create, modify, and swap individual widgets without affecting the overall structure of the program. Within the GUI class, encapsulation was used to extend functionality of basic Swing classes, like JPanel, especially when designing the game information panel. This allowed the GUI and GameLogic classes to quickly interact, meaning the information panel could dynamically update given the state of chessboard.

Future Work

In the future, I plan on implementing a new bot, created by scraping grandmaster games and using gradient descent techniques to optimize an evaluation function. Furthermore, the implementation of alpha-beta pruning could further speed up the minimax algorithm. This would allow for much deeper searches and an improved game from the bot. Additionally, future work can be done on the GUI, possibly creating player "profiles" to store data about bots and players. This would create a more compelling experience for users, encouraging them to come back.

Acknowledgements

The assets for the chess pieces were sourced online. I do not claim ownership over these images.

About

This is a modular and intuitive implementation of the classic game Chess, built from the ground up in Java.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages