Skip to content

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.

Image

1. The Board (aBoard)

The aBoard object is responsible for initializing the game board.

  • Initialization: When the game starts (in Step event), aBoard loops through an 8x8 grid.
  • Tile Creation: For each grid position, it creates a BoardPiece instance.
    • It calculates the algebraic position (e.g., "e4") and assigns it to the BoardPiece.
  • Piece Spawning: It checks a global map called global.boardLineUp to see if a piece should exist at that position.
    • If yes, it creates an aPiece instance.
    • It sets the piece's type (e.g., "Rook") and side (e.g., "player").
    • Wiring: It links the tile and the piece:
      • BoardPiece.contain = aPiece
      • aPiece.currentTile = BoardPiece

2. The Pieces (aPiece)

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.

3. The Tiles (BoardPiece)

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, y of the selected piece to the new tile.
      • Capture: Destroys any enemy piece currently on the target tile.
      • Update Links: Updates contain and currentTile references for both the old and new tiles.
      • Update State: Sets has_moved = true on the piece.
      • Global Map: Updates global.boardLineUp to reflect the new board state.

4. The Rules (ChessLogic Script)

The core rules are defined in the ChessLogic script, primarily in the is_move_legal function.

Image Image
  • 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==0 or dy==0) and calls chess_path_is_clear to ensure no obstacles.
      • Bishop: Checks for diagonals (abs(dx) == abs(dy)) and calls chess_path_is_clear.
      • Queen: Combines Rook and Bishop logic.
      • Knight: Checks for "L" shape (2x1 or 1x2). 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.
  • chess_path_is_clear: A helper function that iterates through all tiles between start and end to ensure they are empty.

Clone this wiki locally