Skip to content

rfals/snail-chess-engine

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

38 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Snail Chess Engine

This is my attempt at making a chess engine, the repository contains Snail Engine which is a basic chess engine developed using NegaMax Alpha Beta Pruning algorithm that you can play against in PyGame. The repository also contains Turtle Engine which is a research work-in-progress notebook of developing an Alpha-Zero-Like Neural Network based engine. Both engines are implemented in Python.


Files

ChessEngine.py: This file contains the logic for the chess game. It includes classes and functions that handle the game board, move generation, validation, and various utility functions.

SnailEngine.py: The SnailEngine file implements the AI for the chess engine. It uses the NegaMax algorithm with Alpha Beta Pruning to search for the best move at each turn. The AI evaluates positions using a combination of heuristics and position scoring.

ChessMain.py: This file brings everything together by rendering a playable chess game in the Pygame library. It provides a graphical user interface where users can interact with the chess engine, make moves, and play against the AI.


How to Run

  1. Clone the repository
  2. Install the requirements using pip install -r requirements.txt
  3. Run the ChessMain.py file using python ChessMain.py
  4. Play against the AI!

Controls

  • Press r to reset the game
  • Press z to undo a move
  • Press e to exit the game

Algorithms

During the development of this project, I implemented multiple algorithms to evaluate the chess positions. The algorithms are listed below in the order of their implementation.

Material Count

This algorithm simply counts the number of pieces on the board and assigns a score to each piece. The score is calculated by multiplying the number of pieces by a constant value. The constant value is positive for white pieces and negative for black pieces. This algorithm is very simple and does not take into account the position of the pieces on the board.

Min Max Algorithm

This algorithm uses the Min Max algorithm to evaluate the positions. It assigns a score to each position by recursively searching the game tree. The algorithm assumes that the opponent will always make the best move and assigns a score to each position based on the best move the opponent can make. The algorithm then chooses the move that leads to the best position for the player. This algorithm is very slow and does not take into account the position of the pieces on the board.

NegaMax Algorithm

This algorithm is similar to the Min Max algorithm but it uses the NegaMax algorithm to evaluate the positions. The NegaMax algorithm is a variation of the Min Max algorithm that simplifies the implementation. The NegaMax algorithm assumes that the opponent will always make the best move and assigns a score to each position based on the best move the opponent can make. The algorithm then chooses the move that leads to the best position for the player. This algorithm is faster than the Min Max algorithm but it does not take into account the position of the pieces on the board.

NegaMax with Alpha Beta Pruning

This algorithm is similar to the NegaMax algorithm but it uses Alpha Beta Pruning to speed up the search. Alpha Beta Pruning is an optimization technique that reduces the number of nodes that are evaluated by the NegaMax algorithm. This algorithm is faster than the NegaMax algorithm but it does not take into account the position of the pieces on the board.

NegaMax with Alpha Beta Pruning and Heuristics

This algorithm is similar to the NegaMax with Alpha Beta Pruning algorithm but it uses heuristics to evaluate the positions. The heuristics are used to assign a score to each position based on the position of the pieces on the board. This algorithm is faster than the NegaMax with Alpha Beta Pruning algorithm and it takes into account the position of the pieces on the board.

Reinforcement Learning with Neural Networks

This algorithm uses a Neural Network to evaluate the positions. The Neural Network is trained using Reinforcement Learning. The Neural Network is trained by playing against itself and learning from the results. This algorithm is faster than the NegaMax with Alpha Beta Pruning and Heuristics algorithm and it takes into account the position of the pieces on the board and is the most accurate algorithm.

Future Improvements

  • Draw by repetition, 3 fold repetition, 50 move rule, and insufficient material
  • Implement Neural Network Engine in PyGame
  • Speed up the search by using a transposition table and iterative deepening
  • Improve code efficiency by using bitboards and NumPy arrays instead of Python lists

Sources

Main Inspiration

Wikipedia articles & Library documentation

AlphaZero & AlphaGo Zero specific articles & papers

Diagrams

Tutorials

Interesting videos

About

This is my attempt at making a chess engine

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published