X_si_O/main.gd

91 lines
3.1 KiB
GDScript

extends Node
@export var circle_scene : PackedScene
@export var cross_scene : PackedScene
var player : int
var winner : int
var temp_marker # marker temporar/ cel din dreapta pt next player
var player_panel_pos : Vector2i # pozitie chenar dreapta in care apare ce jucator urmeaza
var board_size : int
var cell_size : int
var grid_pos : Vector2i
var grid_data : Array # matrice 3 pe 3
var row_sum : int
var col_sum : int
var diagonal1_sum : int
var diagonal2_sum : int
# Called when the node enters the scene tree for the first time.
func _ready(): # ruleaza instant cand nodul maine este utilizat
board_size = $Board.texture.get_width()
# imparte board_size la 3 pentru a obtine dimensiunea fiecarei celule
cell_size = board_size / 3
# obtinere coordonate ale panoului mic din dreapta
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 :
#verifica daca mouse-ul e pe tabla de joc
if event.position.x < board_size:
#convertire pozitie mouse in locatie grid
grid_pos = Vector2i(event.position / cell_size)
if grid_data[grid_pos.y][grid_pos.x] == 0 :
grid_data[grid_pos.y][grid_pos.x] = player # player e variabila sa astim ce salvam in memorie
#plasare simbol jucator pe tabla
create_marker(player, grid_pos * cell_size + Vector2i(cell_size /2, cell_size /2 ))
if check_win() != 0 :
print("game over")
if check_win() == 1 : print("Jucator 1 a castigat")
else: print("Jucator 2 a castigat")
get_tree().paused = true
player *= -1
#update player marcker din dreapta
#temp_marker.delete()
remove_child(temp_marker)
create_marker(player, player_panel_pos + Vector2i(cell_size /2, cell_size /2 ), true)
func new_game():
player = 1
winner = 0
grid_data = [
[0, 0, 0],
[0, 0, 0],
[0, 0, 0]
]
#creare marker pentru a arata care player incepe
create_marker(player, player_panel_pos + Vector2i(cell_size / 2, cell_size /2), true)
func create_marker(player, position, temp = false) :
#creaza un marcker node si il adauga ca un child
if player == -1 :
var circle = circle_scene.instantiate()
circle.position = position
add_child(circle)
if temp : temp_marker = circle
if player == 1 :
var cross = cross_scene.instantiate()
cross.position = position
add_child(cross)
if temp : temp_marker = cross
func check_win() :
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]
diagonal1_sum = grid_data[0][0] + grid_data[1][1] + grid_data[2][2]
diagonal2_sum = grid_data[0][2] + grid_data[1][1] + grid_data[2][0]
#verific daca un player are toate markurile pe o linie / a castigat
if row_sum == 3 or col_sum == 3 or diagonal1_sum ==3 or diagonal2_sum == 3 :
winner = 1
elif row_sum == -3 or col_sum == -3 or diagonal1_sum == -3 or diagonal2_sum == -3 :
winner = -1
return winner