extends Node @export var circle_scene: PackedScene @export var cross_scene: PackedScene var player: int var moves: int var winner: int var temp_marker var player_panel_pos: Vector2i var grid_data: Array var grid_pos: Vector2i var board_size: int var cell_size: int var row_sum: int var col_sum: int var diag1_sum: int var diag2_sum: int # Called when the node enters the scene tree for the first time. func _ready(): board_size = $Board.texture.get_width() #get size of cell cell_size = board_size/3 #coordonates of right panel player_panel_pos = $PlayerPanel.get_position() new_game() # Called every frame. 'delta' is the elapsed time since the previous frame. func _process(delta): pass func _input(event): if event is InputEventMouseButton: if event.button_index == MOUSE_BUTTON_LEFT and event.pressed: #check if mouse on board if event.position.x < board_size: #convert mouse poz to grid location grid_pos = Vector2i(event.position / cell_size) if grid_data[grid_pos.y][grid_pos.x] == 0: moves+=1 grid_data[grid_pos.y][grid_pos.x] = player #place player's marker create_marker(player,grid_pos * cell_size+Vector2i(cell_size/2,cell_size/2)) if check_win() != 0: get_tree().paused=true $GameOverMenu.show() if winner == -1: $GameOverMenu.get_node("ResultLabel").text = "Jucător 1 a câștigat!" elif winner == 1: $GameOverMenu.get_node("ResultLabel").text = "Jucător 2 a câștigat!" #check if tie elif moves==9: get_tree().paused=true $GameOverMenu.show() $GameOverMenu.get_node("ResultLabel").text = "Egalitate!" player *= -1 #update player marker temp_marker.queue_free() create_marker(player,player_panel_pos+Vector2i(cell_size/2,cell_size/2),true) print(grid_data) func new_game(): player=1 winner=0 moves=0 row_sum=0 col_sum=0 diag1_sum=0 diag2_sum=0 #clear existing markers get_tree().call_group("x-uri","queue_free") get_tree().call_group("zerouri","queue_free") grid_data = [ [0,0,0], [0,0,0], [0,0,0] ] #create a marker to show starting player's turn create_marker(player,player_panel_pos+Vector2i(cell_size/2,cell_size/2),true) $GameOverMenu.hide() get_tree().paused=false func create_marker(player,position,temp=false): #create marker note and add it as a child if player == -1: var circle = circle_scene.instantiate() circle.position = position add_child(circle) if temp: temp_marker=circle else: var cross = cross_scene.instantiate() cross.position = position add_child(cross) if temp: temp_marker=cross func check_win(): #add row, col, diag values for i in len(grid_data): row_sum = grid_data[i][0]+grid_data[i][1]+grid_data[i][2] col_sum = grid_data[0][i]+grid_data[1][i]+grid_data[2][i] diag1_sum = grid_data[0][0]+grid_data[1][1]+grid_data[2][2] diag2_sum = grid_data[0][2]+grid_data[1][1]+grid_data[2][0] #check if any player has 3 || -3 points if row_sum==-3 or col_sum==-3 or diag1_sum==-3 or diag2_sum==-3: winner = 1 if row_sum==3 or col_sum==3 or diag1_sum==3 or diag2_sum==3: winner = -1 return winner func _on_game_over_menu_restart(): new_game()