How can I show possible moves a chess board?

Godot Version

Godot 4.5.1

Question

Hi guys I am new to game development and godot but I know a bit of coding so I decided to try and make a chess game as a way to start easy. I reached the part where I have to code the chess pieces themselves, and Im struggling on how I can make the pieces know where they can go (eg. bishop in a diagonal) my chess board is set as a tile set and the pieces are individual scenes with sprite2d to show them. Thank you in advance!

A good approach is to separate the rules from the pieces. Instead of making each piece figure out the board on its own, let your board script handle movement logic.

For example, give each piece a type (“bishop”, “rook”, etc.) and its current grid position. When the player selects a piece, ask the board to calculate valid moves based on the piece type. For a bishop, you just check tiles diagonally until you hit another piece or the edge.

Since you’re using a TileMap, you can use local_to_map() and map_to_local() to convert between tile coords and world coords.

Simple bishop example (GDScript):

func get_bishop_moves(start: Vector2i) -> Array[Vector2i]:
    var moves: Array[Vector2i] = []
    var directions = [
        Vector2i(1, 1),
        Vector2i(1, -1),
        Vector2i(-1, 1),
        Vector2i(-1, -1)
    ]

    for dir in directions:
        var pos = start + dir
        while is_on_board(pos):
            if is_tile_occupied(pos):
                if is_enemy_piece(pos):
                    moves.append(pos) # can capture
                break
            moves.append(pos)
            pos += dir
    return moves

Where is_on_board(), is_tile_occupied(), and is_enemy_piece() are simple helper functions in your board script.

This keeps the logic clean and makes debugging way easier. Good luck!

1 Like

thank you!