Trying To make the apples not coincide with my snake

Godot Version

Godot 4.3

Question

I am trying to create a snake game with a grid(I know there must be a better way to do this, but I can’t really be bothered to change everything) but the apples seem to be put on the same spaces as the snake even with all my trying not have this happen.

CODE:

extends Node2D
var apple_count:int=0
var apple_add:int=1
var data_array:Array[Array]=[]
var square_size:int=50#Grid Limits x:1,9 y:1,13
var turn_speed:float=0.5
var draw_paths:Thread
var apple_array:Array[Dictionary]=[]
var snake_array:Array[Dictionary]=[{"Type":"h","Position":Vector2(5,6),"Direction":Vector2.UP,"Last Position":Vector2(5,6),"Last Direction":Vector2.UP}]
# Called when the node enters the scene tree for the first time.
func _ready() -> void:
	get_tree().root.process_mode=Node.PROCESS_MODE_ALWAYS
	for x in range(0,450/square_size):
		var y_array:Array[Dictionary]=[]
		for y in range(0,650/square_size):
			y_array.append({"Position":array2coord(x,y),"Occupied":"x"})
		data_array.append(y_array)
	draw_snake()
	place_apple()
	await get_tree().create_timer(turn_speed).timeout
	movement_turns()
func movement_turns() -> void:
	while true:
		for i:int in range(0,snake_array.size()):
			var bit:Dictionary=snake_array[i]
			var new_pos:Vector2=bit["Position"]
			match bit["Direction"]:
				Vector2.UP:
					if Vector2(new_pos+Vector2.UP).y >= 1:
						new_pos+=Vector2.UP
					else:
						restart()
				Vector2.DOWN:
					if Vector2(new_pos+Vector2.UP).y <= 650/square_size:
						new_pos+=Vector2.DOWN
					else:
						restart()
				Vector2.LEFT:
					if Vector2(new_pos+Vector2.LEFT).x >= 1:
						new_pos+=Vector2.LEFT
					else:
						restart()
				Vector2.RIGHT:
					if Vector2(new_pos+Vector2.RIGHT).x <= 450/square_size:
						new_pos+=Vector2.RIGHT
					else:
						restart()
			snake_array[i]["Last Position"]=snake_array[i]["Position"]
			snake_array[i]["Position"]=new_pos
			snake_array[i]["Last Direction"]=snake_array[i]["Direction"]
			if snake_array[i]["Type"]!="h":
				snake_array[i]["Direction"]=snake_array[i-1]["Last Direction"]
		draw_snake()
		draw_apple()
		await get_tree().create_timer(turn_speed).timeout
func _draw() -> void:
	var i=false
	for x in range(356,806,square_size):
		i=!i
		for y in range(0,700,square_size):
			draw_rect(Rect2(x,y,square_size,square_size),Color.SADDLE_BROWN if i else Color.BURLYWOOD)
			i=!i
	for x in range(0,450/square_size):
		for y in range(0,650/square_size):
			if data_array[x][y]["Occupied"]=="b":
				draw_rect(Rect2(data_array[x][y]["Position"],Vector2(square_size,square_size)),Color.GREEN)
			elif data_array[x][y]["Occupied"]=="h":
				draw_rect(Rect2(data_array[x][y]["Position"],Vector2(square_size,square_size)),Color.GREEN)
				draw_circle(data_array[x][y]["Position"]+Vector2(25,25),12.5,Color.BLACK)
			elif data_array[x][y]["Occupied"]=="a":
				draw_circle(data_array[x][y]["Position"]+Vector2(25,25),25,Color.RED)
func draw_snake() -> void:
	for i in range(0,snake_array.size()):
		var bit:Dictionary=snake_array[i]
		if data_array[bit["Position"].x-1][bit["Position"].y-1]["Occupied"]=="a":
			place_apple(bit["Position"])
			add_snake_bit()
		elif data_array[bit["Position"].x-1][bit["Position"].y-1]["Occupied"]=="b":
			restart()
		data_array[bit["Last Position"].x-1][bit["Last Position"].y-1]["Occupied"]="x"
		data_array[bit["Position"].x-1][bit["Position"].y-1]["Occupied"]=bit["Type"]
		queue_redraw()
func restart()->void:
	get_tree().root.process_mode=Node.PROCESS_MODE_DISABLED
	await get_tree().process_frame
	get_tree().change_scene_to_packed(preload("res://node_2d.tscn"))
func array2coord(x:int,y:int) -> Vector2:
	return Vector2(356+(x*square_size),y*square_size)
func _unhandled_input(_event: InputEvent) -> void:
	if Input.is_action_just_pressed("Up"):
		snake_array[0]["Last Direction"]=snake_array[0]["Direction"]
		snake_array[0]["Direction"]=Vector2.UP
	elif Input.is_action_just_pressed("Down"):
		snake_array[0]["Last Direction"]=snake_array[0]["Direction"]
		snake_array[0]["Direction"]=Vector2.DOWN
	elif Input.is_action_just_pressed("Left"):
		snake_array[0]["Last Direction"]=snake_array[0]["Direction"]
		snake_array[0]["Direction"]=Vector2.LEFT
	elif Input.is_action_just_pressed("Right"):
		snake_array[0]["Last Direction"]=snake_array[0]["Direction"]
		snake_array[0]["Direction"]=Vector2.RIGHT
func place_apple(pos:Vector2=Vector2(0,0)) -> void:
	if apple_array.is_empty():
		var reroll:bool=true
		while reroll:
			var new_pos:Vector2=Vector2(randi_range(1,9),randi_range(1,13))
			if data_array[(new_pos.x)-1][(new_pos.y)-1]["Occupied"]!="b" or data_array[(new_pos.x)-1][(new_pos.y)-1]["Occupied"]!="a" or data_array[(new_pos.x)-1][(new_pos.y)-1]["Occupied"]!="h":
				apple_array.append({"Position":new_pos})
				data_array[(apple_array[0]["Position"].x)-1][(apple_array[0]["Position"].y)-1]["Occupied"]="a"
				reroll=false
	else:
		if apple_count<15:
			turn_speed-=0.002
		else:
			turn_speed-=0.0005
		if apple_count%apple_add==0:
			var reroll:bool=true
			while reroll:
				var new_pos:Vector2=Vector2(randi_range(1,9),randi_range(1,13))
				if data_array[(new_pos.x)-1][(new_pos.y)-1]["Occupied"]!="b" or data_array[(new_pos.x)-1][(new_pos.y)-1]["Occupied"]!="a" or data_array[(new_pos.x)-1][(new_pos.y)-1]["Occupied"]!="h":
					apple_array.append({"Position":new_pos})
					reroll=false
		apple_count+=1
		for current_index in range(0,apple_array.size()):
			var apple:Dictionary=apple_array[current_index]
			if pos==apple["Position"]:
				apple_array.pop_at(current_index)
				var reroll:bool=true
				while reroll:
					var new_pos:Vector2=Vector2(randi_range(1,9),randi_range(1,13))
					if data_array[(new_pos.x)-1][(new_pos.y)-1]["Occupied"]!="b" or data_array[(new_pos.x)-1][(new_pos.y)-1]["Occupied"]!="a" or data_array[(new_pos.x)-1][(new_pos.y)-1]["Occupied"]!="h":
						apple_array.insert(current_index,{"Position":new_pos})
						data_array[(apple_array[current_index]["Position"].x)-1][(apple_array[current_index]["Position"].y)-1]["Occupied"]="a"
						reroll=false
			else:
				continue
func add_snake_bit()->void:
	snake_array.append({"Type":"b","Position":snake_array[snake_array.size()-1]["Last Position"],"Direction":snake_array[snake_array.size()-1]["Last Direction"],"Last Position":snake_array[snake_array.size()-1]["Last Position"],"Last Direction":snake_array[snake_array.size()-1]["Last Direction"]})
func draw_apple()->void:
	for apple:Dictionary in apple_array:
		data_array[apple["Position"].x-1][apple["Position"].y-1]["Occupied"]="a"

I guess it might be the "or"s in this line:

try changing them to “and”, so the condition is only true if its not occupied by “b” AND not occupied by “a” AND not occupied by “h”.

Also, for future debugging it might be helpful to improve the codes readability, so errors can be spottet more easily. Maybe ask chatGPT to make the code more readable

Thanks!!! and I’ll take your advice!!!