My chess games code is not working properly! (New to GameDev)

Godot Version

4.5.1

Question

Hi guys I recently decided to pick up game development as a hobby and for my first project I decided to make a chess game! I am not new to coding but I only do coding as a hobby and I am not really good at it either. Could someone please help me understand whats wrong with my code? I want all possible moves to be highlighted but the code is not removing pieces that are occupied with my own pieces.

Whats happening:

What should happen is that the pawns dont even count.

Node structure:

Script:

extends Node2D

## Loads all the pieces
var black_pawn = preload("res://Pieces/black/black_pawn.tscn")
var black_knight = preload("res://Pieces/black/black_knight.tscn")
var black_bishop = preload("res://Pieces/black/black_bishop.tscn")
var black_rook = preload("res://Pieces/black/black_rook.tscn")
var black_queen =preload("res://Pieces/black/black_queen.tscn")
var black_king = preload("res://Pieces/black/black_king.tscn")


var white_pawn = preload("res://Pieces/white/white_pawn.tscn")
var white_knight = preload("res://Pieces/white/white_knight.tscn")
var white_bishop = preload("res://Pieces/white/white_bishop.tscn")
var white_rook = preload("res://Pieces/white/white_rook.tscn")
var white_queen = preload("res://Pieces/white/white_queen.tscn")
var white_king = preload("res://Pieces/white/white_king.tscn")

signal select_button

## Dictionary for moves
@onready var chessboard = $Chessboard

## Defines what side of piece
var White_Pieces = [White_Pawn , White_Bishop , White_Knight , White_Rook , White_Queen , White_King]
## Pieces
var White_Pawn
var White_Bishop
var White_Knight
var White_Rook
var White_Queen
var White_King

## Dictionary
var board = {
	Vector2(0,-2): [White_Pawn , "white pawn"],
	Vector2(1,-2): [White_Pawn , "white pawn"],
	Vector2(2,-2): [White_Pawn , "white pawn"],
	Vector2(3,-2): [White_Pawn , "white pawn"],
	Vector2(4,-2): [White_Pawn , "white pawn"],
	Vector2(5,-2): [White_Pawn , "white pawn"],
	Vector2(6,-2): [White_Pawn , "white pawn"],
	Vector2(7,-2): [White_Pawn , "white pawn"],
	Vector2(2,-1): [White_Bishop , "white bishop"],
	Vector2(5,-1): [White_Bishop , "white bishop"],
	Vector2(1,-1): [White_Knight, "white knight"],
	Vector2(6,-1): [White_Knight, "white knight"],
	Vector2(0,-1): [White_Rook, "white rook"],
	Vector2(7,-1): [White_Rook, "white rook"],
	Vector2(3,-1): [White_Queen, "white queen"],
	Vector2(4,-1): [White_King, "white king"],
}


enum Playing{ White, Black }

##Loads All the pieces
func _ready() -> void:
	white_pawns()
	white_bishops()
	white_knights()
	white_rooks()
	white_queens()
	pass


func on_board(pos):
	var tiletaken: int = chessboard.get_cell_source_id(pos)
	if tiletaken == -1:
		print("tile not occupied")
		return null
	else :
		print("tile occupied")
		return true
	

func is_tile_occupied(pos):
	var tile_occupied = board.get(pos)
	if tile_occupied == null:
		return null
	else:
		return tile_occupied


## Loading White Pawns
func white_pawns():
	for i in range(8):
		var initiate_whitepawn = white_pawn.instantiate()
		var pixel_pos = Vector2i(i, -2)
		var piece_pos = chessboard.map_to_local(pixel_pos)
		initiate_whitepawn.position = Vector2i(piece_pos)
		add_child(initiate_whitepawn)
			


## Loading White Bishop
func white_bishops():
	for i in range(2):
		var a = 3  * (i)
		var initiate_whitebishop = white_bishop.instantiate()
		var pos = Vector2i(a + 2, -1)
		var piece_pos = chessboard.map_to_local(pos)
		initiate_whitebishop.position = Vector2i(piece_pos)
		add_child(initiate_whitebishop)


## Loading White Knighta
func white_knights():
	for i in range(2):
		var a = 5  * (i)
		var initiate_whiteknight = white_knight.instantiate()
		var pos = Vector2i(a + 1, -1)
		var piece_pos = chessboard.map_to_local(pos)
		initiate_whiteknight.position = Vector2i(piece_pos)
		add_child(initiate_whiteknight)


## Loading White Rooks
func white_rooks():
	for i in range(2):
		var a = 7  * (i)
		var initiate_whiterook = white_rook.instantiate()
		var pos = Vector2i(a , -1)
		var piece_pos = chessboard.map_to_local(pos)
		initiate_whiterook.position = Vector2i(piece_pos)
		add_child(initiate_whiterook)


## Loading White Queen
func white_queens():
	var initiate_whitequeen = white_queen.instantiate()
	var pos = Vector2i(3 , -1)
	var piece_pos = chessboard.map_to_local(pos)
	initiate_whitequeen.position = Vector2i(piece_pos)
	add_child(initiate_whitequeen)

## Handling Inputs
func _unhandled_input(event: InputEvent) -> void:
	if event.is_action_pressed("left_click"):
		assert(event is InputEventMouse)
		var mouse_position_viewport: Vector2 = event.position
		var mouse_position_tilemap_local: Vector2 = chessboard.get_global_transform_with_canvas().affine_inverse() * mouse_position_viewport
		var mouse_selection : Vector2 = chessboard.local_to_map(mouse_position_tilemap_local)
		print(mouse_selection)
		var piece_clicked = board.get(mouse_selection)
		print(piece_clicked)
		var onboard = on_board(mouse_selection)
		print(onboard)
		handle_possible(mouse_selection)
		
func handle_possible(tile_position):
	var possible_moves = get_bishop_moves(tile_position)
	var a = possible_moves.size()
	highlight_possible_moves(possible_moves , a)
	print(possible_moves)





func highlight_possible_moves(possible_moves , a):
	for i in range(a):
		var is_piece_enemy = possible_moves [i]
		chessboard.set_cell(is_piece_enemy , 1 , Vector2(0,0), 0   )
	pass
	



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 on_board(pos):
			if is_tile_occupied(pos):
				var what_side = what_side_is_piece(pos)
				if what_side == "White_Piece":
					print("white piece in line of sight")
				else:
					if check_for_piece(pos):
							print("black piece in line of sight")
							moves.append(pos) # can capture
							break
			moves.append(pos)
			pos += dir
	return moves

func check_for_piece(pos ):
	var piece_enemy = board.get(pos)
	var is_piece_enemy = board [piece_enemy][0]


func what_side_is_piece(piece):
	var White_Piece
	var Black_Piece
	var side = White_Pieces.get(piece)
	if side == true:
		return "White_Piece"
	else:
		return "Black_Piece"
		

Thank you in advance !

The loop in get_bishop_moves() should break once it encounters an occupied square.

First off, great code organization. Is the occupied white tile being recognized as occupied (using the print("white piece in line of sight"))? If it is, then the loop might just be ignoring this call because you did not tell it to break that direction when a piece is blocking it. Add a break after that print and see if it works.

Something like this I assume ? sadly it doesn’t seem to work thank you regardless!

for dir in directions:
	var pos = start + dir
	while on_board(pos):
		if is_tile_occupied(pos):
			var what_side = what_side_is_piece(pos)
			if what_side == "White_Piece":
				print("white piece in line of sight")
				break
			else:
				if check_for_piece(pos):
						print("black piece in line of sight")
						moves.append(pos) # can capture
						break
						
		moves.append(pos)
		pos += dir
return moves

Like this ? again it doesnt seem to work , I think the problem might be elsewhere I will try to find it but thank you so much regardless!

Is it printing the “white piece in line of sight” debug? Also, check_for_piece does not have a return value yet, and so it will always be false.

No it doesnt seem to this is the current debug , thank you I somehow missed this.

(2.0, -1.0)
[, “white bishop”]
tile occupied
true
tile not occupied
tile occupied
tile occupied
tile occupied
tile occupied
tile occupied
tile not occupied
tile not occupied
tile occupied
tile occupied
tile not occupied
[(3, -2), (4, -3), (5, -4), (6, -5), (7, -6), (1, -2), (0, -3)]

Well the logic should speak for itself. Simply - if a tile is occupied then break:

if is_tile_occupied(pos):
	# other stuff
	break

Or make it a part of while condition:

while on_board(pos) and not is_tile_occupied(pos):
	# stuff

Seems like that was the problem thank you !