DevLog #2: Building a Chess Engine - Piece Movement Functionality

DevLog #2: Building a Chess Engine - Piece Movement Functionality

Avatar of Seriton7
| 0

In this stage of the project, I focused on implementing the functionality to move chess pieces on the board according to the rules of chess. Previously, I developed a graphical user interface (GUI), and now it’s time to add basic logic to the engine for handling moves.


Stage Objectives:

1. Enable moving chess pieces according to chess rules.

2. Prepare the foundational logic to expand the engine into a more advanced chess system in the future.


Features:

1. Highlighting Selected Square

Users can click on any square on the board. If a piece is present on the square, it gets highlighted, making it easier to track.

This functionality is managed by the draw_board function. It checks the coordinates of the square and changes its color to yellow (SELECTED) if it matches the selected square:

if selected and selected == (col, row):

   color = SELECTED

2. Piece Movement Handling

All moves comply with chess rules. Below is the logic for each piece:

- Pawn: Moves one square forward (or two if on its starting square) and captures diagonally.
- Rook: Moves vertically or horizontally without jumping over other pieces.
- Bishop: Moves diagonally under similar restrictions as the rook.
- Queen: Combines the movement of the rook and bishop.
- Knight: Moves in an "L" shape and can jump over other pieces.
- King: Moves one square in any direction.

Each piece has its unique logic implemented in the is_valid_move function.

3. Executing Piece Movement

When a move is valid, the move_piece function transfers the piece to the new square and updates the board array:

board[ey][ex] = board[sy][sx]

board[sy][sx] = ""

4. Turn-Based Play

Moves alternate between white and black players, enforced by the turn variable. This prevents a player from moving the opponent's pieces:

if turn == "white" and not piece.isupper():

    return False

if turn == "black" and not piece.islower():

    return False


Outcome:

Since the engine code is getting longer, I can no longer post it here, so I created a GitHub account where all updates will be published. Link -  https://github.com/Sakludo


The code currently enables:

1. Moving pieces according to chess rules.

2. Graphically highlighting the selected piece.

3. Switching turns between white and black players.

Below are visualizations of the current project state:

Graphically highlighting the selected piece.
Moving pieces according to chess rules.

Summary and Next Steps:

1. Move Validation Enhancements:

The is_valid_move function still requires optimization to handle advanced rules, such as check, pawn promotion, and castling.

2. Interactive Features:

I aim to add visual indicators showing valid moves for the selected piece.

3. AI Development:

The next step will be implementing an algorithm to allow the computer to make moves, such as the Minimax algorithm.

4. Fix Errors:

The program may contain errors that I didn't notice.

This stage of development has resulted in a basic chess engine that enables interaction with the board. It’s an important step towards building a fully functional chess engine.


Stay tuned for the next devlog entries to see how the project evolves!