0 votes

Made a Tic Tac Toe game so I could brush up on my logic. I am using a CanvasLayer object as main and have a simple code running through 9 buttons. Essentially, an empty array is called and compared to the goal variable every time a button is pressed. I am using an array to change states and groups to call on rows and columns. Everything works except Column 1 (boxes 1, 4, 7 from clockwise top left, respectively).

update: it seems to be something in the ColumnCheck function. When i skip that, everything that's not a column works, including Rows and Across. Whenever the columnCheck happens, things don't tally correctly.

UPDATE 2: Yeah so I'm an idiot. My debug window wasn't maxed and it was just sending so many messages I didn't see the game over. Ahem. Move on.... nothin to see here..... :D

main code:

    extends CanvasLayer


var is_Turn_X = true
var turnTotal = 0
onready var myButtons = get_tree().get_nodes_in_group('all_buttons')


func _ready():
    startGame()


func _process(delta):
    if turnTotal == 9:
        endGame()



func winCheck():
    rowCheck()



func rowCheck():
    var row1 = get_tree().get_nodes_in_group('row_1')
    sumXO(row1)
    var row2 = get_tree().get_nodes_in_group('row_2')
    sumXO(row2)
    var row3 = get_tree().get_nodes_in_group('row_3')
    sumXO(row3)
    columnCheck()


func columnCheck():
    var col1 = get_tree().get_nodes_in_group('column_1')
    sumXO(col1)
    var col2 = get_tree().get_nodes_in_group('column_2')
    sumXO(col2)
    var col3 = get_tree().get_nodes_in_group('column_3')
    sumXO(col3)
    acrossCheck()


func acrossCheck():
    var acr1 = get_tree().get_nodes_in_group('across_1')
    sumXO(acr1)
    var acr2 = get_tree().get_nodes_in_group('across_2')
    sumXO(acr2)


func sumXO(target):
    var x = "HAS_X"
    var o = "HAS_O"
    var x_ct = []
    var o_ct = []
    var goal = target.size()/2
    var active = true

    for i in target:
        if i.state == x:
            x_ct.append(i)
            print("x score: ")
            print(x_ct)
        elif i.state == o:
            o_ct.append(i)
            print("o_score: ")
            print(o_ct)

    if active:
        if x_ct.size() == goal:
            print("x wins!")
            endGame()
        elif o_ct.size() == goal:
            print("o wins!")
            endGame()
        else:   
            active = false



func startGame():
    for button in myButtons:
        button.set_button_icon(null)
    is_Turn_X = true
    print("Game start!")


func endGame():
    print("Game over.")
    get_tree().paused = true

And then the button nodes, all with the same script:

    extends Button


onready var x_icon = preload("res://x.png")
onready var o_icon = preload("res://o.png")
var stateMachine = ["EMPTY", "HAS_X", "HAS_O"]
var state = stateMachine[0]


func _ready():
    self.connect("pressed", self, "on_Button_pressed")


func on_Button_pressed():
    if state == stateMachine[0]:
        if Global.is_Turn_X == true:
            self.set_button_icon(x_icon)
            state = stateMachine[1]
            turnEnd()
        if Global.is_Turn_X == false:
            self.set_button_icon(o_icon)
            state = stateMachine[2]
            turnEnd()


func turnEnd():
    yield(self, "button_up")
    print("State is: " + state)
    var nextState = not Global.is_Turn_X
    Global.winCheck()
    Global.is_Turn_X = nextState
    Global.turnTotal += 1
Godot version 3.4.3 stable
in Engine by (312 points)
edited by

Please log in or register to answer this question.

Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to webmaster@godotengine.org with your username.