Skip to content

Arrakkis/ZofiaChessBot

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

37 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Zofia Chess Bot

Zofia Chess Bot

This is a project created and maintained currently by a single person, focused on creating an autonomous chess-playing bot that can play a full game of chess, evaluate positions, and attempts to generate optimal moves from the current position.

It is currently a work in progress, with only the board representation completed. Current areas WIP are: Legal move generation (pins specifically), optimal move choice logic.

Chess Pieces

Chess pieces are individual objects of class Piece, which contains a subclass for each chess piece in the game. These objects store their own mailbox address for quick reference, their legal moves, team, value, and other values relevant to the piece's position.

Movement is calculated by adding a radial value to the current mailbox value, which, conveniently, lets us index the board array directly in constant time, quickly accessing the tile being considered.

Chessboard

For pieces with collision detection (Rook, Bishop, Queen), once a piece is detected, it stores the final capture move, and moves on to another direction until all are accounted for.

Board representation

Zofia represents a chessboard using a type of mailbox system. Chess tiles are stored in an array, starting with the bottom left tile at A1 and moving leftward to the edge, where it then moves up and wraps around, moving left to right again.

Chessboard

In order to quickly detect chess piece moves that would bring them out of the playing field, the sides of the chess board have "sentinel" tiles on the sides that signal out of bounds. Thus the array is actually significantly larger, and the first mailbox of the actual board begins at index 21.

Chessboard

This allows even knight moves to be easily detected, even at the top and bottom where movement of 2 would not lead to a wraparound in the board representation. The board itself stores a dictionary tree structure to describe both teams and their pieces, and can also store the legal moves.

A starting board can be generated by calling in sequence:

chessBoard = board()
chessBoard.makeBoard()
# You can use one of the following functions to setup your desired board layout
# This creates the basic starting layout
chessBoard.populate()
# This creates a board layout from a FEN, input FEN as a string, example included
chessBoard.boardFromFEN("rnbq1rk1/pp3ppp/2p1pn2/3p4/2PP4/2P1PN2/P3BPPP/R1BQ1RK1 b - - 4 8")
# You can also freely add pieces to the board at any stage (Replaces whatever piece is currently there)
chessBoard.addPiece('Q','white','D4')
# Use this to clear a tile
chessBoard.clearTile('D4')
# Or this to clear the whole board
chessBoard.clearBoard()
# The chess board MUST BE INITIALIZED in order to be used, this clears illegal piece moves, and
# prepares the board for Zofia to begin move consideration
chessBoard.initialize()

TIP: Each piece is named using its first letter, except for the knight, who is given the traditional "N" as a name.

You may display the board at any point, even before initialization as such:

chessBoard.display()

This display function uses Colorama to color the pieces and the board for easy visualization.

It should look a little bit like this:

Chessboard

Pin Handling

Pins can be challenging to handle quickly and efficiently. Zofia's current implementation has a few steps:

Firstly, move generation for all pieces is completed, including the addition of a special series of arrays for all pieces with directional vectors (Rooks, Bishops, and Queens). It stores all pieces along the line of sight, even those behind the first visible piece.

Second, any piece that has an array with an enemy king behind an enemy piece is considered pinned.

Third, the corresponding vector of the piece that is pinning is inverted, and Zofia checks to see if the pinned piece has that movement vector available as well.

Fourth, if it is available, trace those steps across the board along that vector, and add those moves to the legal moves list. Otherwise, the pin is absolute, and the pinned piece has no legal moves.

This technique has a few advantages, but could be improved with "flagging" at the move generation stage. It is however, very appropriate and fast for the board representation we are using.

Move Pondering

Zofia has been designed to use a reinforcement learning neural network to guide its decisions. Given any board state, Zofia will go through all legal moves available to her and generate the board after that move has been played, and, from the new board state; will give a percentage-based chance of victory. From this, new legal moves are generated, and the process repeats to the next depth layer. This way, promising moves are quickly evaluated, but superior move combinations can also be considered. Consecutive depth analysis yields a dynamic evaluation which can account for the time assigned to a particular move ponder.

The board position input is formatted with the rows being the position of pieces on the board, and the column being the corresponding piece type for each team.

About

A reinforcement learning chess bot

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages