In may 2024 I set out to improve my knowledge of python. Like many of my peers one of my hobbies is chess but I am nor very good. I decided to kill two birds with one stone by creating a chess game/chess bot using Python and the PyGame library.
- The first step was understanding how PyGame creates game spaces. Then in this game space how can I make a chessboard. Both of these ended up being quite simple. First I imported PyGame as p. Second in my main I generate a PyGame object using p.init and create a screen. Then in my main I start by running just the drawBoard function bellow. This function is passes the screen object (where to draw to) and squareColors a variable that allows me to add customizability later. It then creates a checker board the side of DIMENSION (default 8) where each SQ_SIZE is the ratio of the game window width to the dimension.

2. Okay so now I have a chessboard but no pieces. I began by writing code that imported an image of each piece and began placing them on the board. Upon further research I found the Chess library. This library simplified creating all of the game functionality and would allow me to work on developing the bot. Luckily my work importing images was still useful as I preferred the piece style I chose and my images scaled better. The most important object in the chess library is the board object. The board object stores the location of the pieces using FEN notation. I pass my board object to the draw pieces function before placing the piece images at their corresponding square.



3. Now I have generated a chess board but my pieces won’t move. I briefly mention it above but they key to understanding chess movement on a computer is FEN(Forsyth Edwards Notation). FEN uses default ASCII characters to describe board spaces. an example of FEN for the default board space is
rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq – 0 1
the black pieces are described by lower case letters and the white by uppercase. Each row is separated by a / after the board is described the letters w and b describe which pieces turn it is to move. The following two sections define castling availability the KQkq defines which castles are still legal. KQkq means that none of the kings or rooks have moved. If it were instead KQk this would mean that the black queenside rook has moved. The follow – then defines the availability of the castle using the same notation. The – means that there are pieces in the way of all castling moves. If it were instead a K it would mean that white can castle on the king side. lastly the 0 1 define the number of moves taken. 0 1 means black has taken 0 moves and it is white’s 1st move.
Okay so now we understand FEN how do we use it to actually move pieces on our board? Again the chess library’s board object is incredibly useful. It is able to handle pretty much all FEN updates we just need to read the users inputs at tell it the correct updates.
In my main I create a state machine with 3 states Start Screen, Game Running, and End Screen. In the Game Running I clear the the screen, draw the current board state, check that the FEN is valid, check game mode, check player turn, get player click / CPU move, and update board state.






4. Okay now I have a playable game but who am I playing against?
To be Continued…