-
Notifications
You must be signed in to change notification settings - Fork 1
Chess Logic
DuncanSzabaga edited this page Nov 23, 2025
·
1 revision
This document explains the core logic of the chess implementation, focusing on how the board, pieces, and movement rules are wired together.
The aBoard object is responsible for initializing the game board.
-
Initialization: When the game starts (in
Stepevent),aBoardloops through an 8x8 grid. -
Tile Creation: For each grid position, it creates a
BoardPieceinstance.- It calculates the algebraic position (e.g., "e4") and assigns it to the
BoardPiece.
- It calculates the algebraic position (e.g., "e4") and assigns it to the
-
Piece Spawning: It checks a global map called
global.boardLineUpto see if a piece should exist at that position.- If yes, it creates an
aPieceinstance. - It sets the piece's
type(e.g., "Rook") andside(e.g., "player"). -
Wiring: It links the tile and the piece:
-
BoardPiece.contain=aPiece -
aPiece.currentTile=BoardPiece
-
- If yes, it creates an
The aPiece object is the visual representation of a chess piece.
-
Data: It holds simple data:
-
side: "player" or "cpu" -
piece_type: "King", "Queen", "Rook", "Bishop", "Knight", or "Pawn" -
has_moved: Boolean flag (important for castling and pawn double-moves).
-
- Logic: It does not contain the movement logic itself. It is primarily a data container and visual object.
The BoardPiece object handles user interaction (clicks).
-
Selection: When clicked (Left Mouse Button):
- If nothing is selected, it selects the piece on itself (if it belongs to the player).
- It calls
chess_mark_legal_moves()to highlight valid destinations.
-
Movement: If a piece is already selected and the user clicks a different
BoardPiece:- It checks validity using
is_move_legal(). - If legal:
-
Move: Updates the
x, yof the selected piece to the new tile. - Capture: Destroys any enemy piece currently on the target tile.
-
Update Links: Updates
containandcurrentTilereferences for both the old and new tiles. -
Update State: Sets
has_moved = trueon the piece. -
Global Map: Updates
global.boardLineUpto reflect the new board state.
-
Move: Updates the
- It checks validity using
The core rules are defined in the ChessLogic script, primarily in the is_move_legal function.
-
is_move_legal(_piece, _fromTile, _toTile):- Calculates the difference in file (
dx) and rank (dy) between the start and end tiles. -
Switch Statement: Checks logic based on
_piece.piece_type:-
Rook: Checks for straight lines (
dx==0ordy==0) and callschess_path_is_clearto ensure no obstacles. -
Bishop: Checks for diagonals (
abs(dx) == abs(dy)) and callschess_path_is_clear. - Queen: Combines Rook and Bishop logic.
-
Knight: Checks for "L" shape (
2x1or1x2). Knights jump, so they don't need path clearing. - Pawn: Checks direction (up for player, down for cpu), single steps, double steps (if not moved), and diagonal captures.
- King: Checks for single steps or Castling logic.
-
Rook: Checks for straight lines (
- Calculates the difference in file (
-
chess_path_is_clear: A helper function that iterates through all tiles between start and end to ensure they are empty.
Wiki
User Stories
Design
Requirements
Architecture
Considerations & Issues
Documentation
- How to Add a Card to the Game
- How the Music Controller & Script work
- Card Paging & Search System (CardManager)
- How to Spawn Cards in a Room
- How the Chessboard works
- How to Add a New Board Color
- How to use Alert System
- Chess Logic
- Cheat Mode
- Timer
- How the new card system works
- How the NEW Chessboard works