DevLog #3: Building a Chess Engine - Implementing Castling and Notation
Recently, I made several important changes to my chess engine project, which enhance both the realism of the game and its functionality. The most significant modifications include adding support for castling (both kingside and queenside) and implementing chess notation on the board.
Stage Objectives:
1. Adding the ability to castle the king according to chess rules.
2. Adding chess notation on the board for better visualization.
3. Fixing the queen's movement bug.
Features:
1. Adding Castling (Kingside and Queenside)
Introducing castling into my chess engine was essential to make the game a more complete simulation of real chess rules. Castling is a move where the king and a rook switch positions, provided certain conditions are met.
Code snippet for castling handling:
if piece.lower() == "k" and abs(ex - sx) == 2:
if turn == "white" and sy == 7:
if ex == 6:
board[ey][ex] = board[sy][sx]
board[ey][ex - 1] = board[7][7]
board[7][7] = ""
board[sy][sx] = ""
elif ex == 2:
board[ey][ex] = board[sy][sx]
board[ey][ex + 1] = board[7][0]
board[7][0] = ""
board[sy][sx] = ""
elif turn == "black" and sy == 0:
if ex == 6:
board[ey][ex] = board[sy][sx]
board[ey][ex - 1] = board[0][7]
board[0][7] = ""
board[sy][sx] = ""
elif ex == 2:
board[ey][ex] = board[sy][sx]
board[ey][ex + 1] = board[0][0]
board[0][0] = ""
board[sy][sx] = ""
I implemented the castling functionality, including both kingside and queenside versions, with checks for the king and rook positions, empty squares between them, and the fact that neither of these pieces has been moved earlier. After castling, the castling rights are automatically updated.
Updating castling rights:
if piece == "K":
castling_rights["white"]["king"] = False
castling_rights["white"]["queen"] = False
elif piece == "k":
castling_rights["black"]["king"] = False
castling_rights["black"]["queen"] = False
With these changes, castling is now fully functional in the game.
2. Adding Notation on the Board
Another significant change was the addition of chess notation. This allows players to easily track positions on the board, which significantly improves interaction with the game.
Code snippet responsible for the notation:
if col == 0:
text = font.render(str(8 - row), True, text_color)
SCREEN.blit(text, (col * TILE_SIZE + 5, row * TILE_SIZE + 5))
if row == 7:
text = font.render(chr(97 + col), True, text_color)
SCREEN.blit(text, (col * TILE_SIZE + TILE_SIZE - 25, row * TILE_SIZE + TILE_SIZE - 30))
This piece of code adds row numbers (from 1 to 8) and column letters (from a to h) to the board, making it easier for players to read and navigate. The color of the letters and numbers was chosen to blend well with the board, ensuring they are readable without standing out too much.
Probably, in the future, the color and adjustments will be changed.
Outcome:
The result of my work can be found on my GitHub - https://github.com/Sakludo
Summary and Next Steps:
1. Pawn promotion to any piece:
In order to complete the chessboard functionality and introduce AI, implementing pawn promotion is essential.
2. The ability to check the king and deliver checkmate:
Currently, the king does not have the functionality for check or checkmate implemented; it can move into the threat of any piece, which I would like to change in the next update.
3. En passant capture:
Another important step in the development of my chess engine is the en passant capture.
In this update, I focused on adding the ability to perform both short and long castling, as well as implementing chess notation. The next update will focus on completing all the functions according to the rules of chess, which will enable the implementation of artificial intelligence. I aim to develop it into a strong chess engine.
Stay tuned for the next devlog entries to see how the project evolves!