Need Help With Tic Tac Toe Game

Hi everyone,

I am building a tic tac toe game. It is a pretty simple two-player game, where the user taps once if they want to place an “x” on the square or twice if they want to place an “o”. This system works, but I have now run into a problem. If I want to see who won, the only way I can think of to do this would be to check if the image values of 3 buttons in a row are the same. But what if the user has tapped once but is actually about to tap twice? How could I make sure that the user doesn’t lose when they are actually just trying to place an “o” down?

Thank you,

i think its weird that tapping twice will mark an “o”

shouldn’t it easier to just set an X or an O every other tap instead of listening to the kind of tap (single or double)

but if we still go about your app then there checking the winner should be AFTER the labeling (tap) so you can determine if they made a line

Hi @fuerte.francis,

My mistake. I mean to say that every other tap would set an X or an O.

Thanks for the help

so ideally to check for winning you should have named your grid like this

1 2 3
4 5 6
7 8 9

then you need to check all possible combinations of the label text if they all match (X or O)

so 1 2 3, 1 5 9, 1 4 7, 4 5 6, 7 8 9, 2 5 8, 3 6 9 (you can make this into a function to make checking easier)

That’s exactly what I did…
However, I am having trouble writing the function. The variables are not being able to be called, and I don’t know what to do.

Here is my code:
import UIKit

class ViewController: UIViewController {

@IBOutlet weak var button1: UIButton!

@IBOutlet weak var button2: UIButton!

@IBOutlet weak var button3: UIButton!

@IBOutlet weak var button4: UIButton!

@IBOutlet weak var button5: UIButton!

@IBOutlet weak var button6: UIButton!

@IBOutlet weak var button7: UIButton!

@IBOutlet weak var button8: UIButton!

@IBOutlet weak var button9: UIButton!
@IBOutlet weak var startbutton: UIButton!
var a = 0
var b = 0
var c = 0
var d = 0
var e = 0
var f = 0
var g = 0
var h = 0
var i = 0



override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view.
}

var a1 = 0
var b1 = 0
var c1 = 0
var d1 = 0
var e1 = 0
var f1 = 0
var g1 = 0
var h1 = 0
var i1 = 0
@IBAction func button(_ sender: Any) {
    startbutton.isHidden = true
}



@IBAction func bt1(_ sender: Any) {
	a = a+1
    if a%2==0 {
        button1.setImage(UIImage(named: "o.png"), for: .normal)
		a1 = 1
    }
		
    else {
        button1.setImage(UIImage(named: "x.png"), for: .normal)
        a1 = -1
    }
	if a1 == 1 && d1 == 1 && g1 == 1 {
		
		
	}
}

@IBAction func bt2(_ sender: Any) {
    b = b+1
    if b%2==0 {
        button2.setImage(UIImage(named: "o.png"), for: .normal)
        b1 = 1
    }
    else {
        button2.setImage(UIImage(named: "x.png"), for: .normal)
        b1 = -1
    }
    
}

@IBAction func bt3(_ sender: Any) {
    c = c+1
    if c%2==0 {
        button3.setImage(UIImage(named: "o.png"), for: .normal)
        c1 = 1
    }
    else {
        button3.setImage(UIImage(named: "x.png"), for: .normal)
        c1 = -1
    }
    
}

@IBAction func bt4(_ sender: Any) {
    d = d+1
    if d%2==0 {
        button4.setImage(UIImage(named: "o.png"), for: .normal)
        d1 = 1
    }
    else {
        button4.setImage(UIImage(named: "x.png"), for: .normal)
        d1 = -1
    }
}

@IBAction func bt5(_ sender: Any) {
    e = e+1
    if e%2==0 {
        button5.setImage(UIImage(named: "o.png"), for: .normal)
        e1 = 1
    }
    else {
        button5.setImage(UIImage(named: "x.png"), for: .normal)
        e1 = -1
    }
    
}

@IBAction func bt6(_ sender: Any) {
    f = f+1
    if f%2==0 {
        button6.setImage(UIImage(named: "o.png"), for: .normal)
        f1 = 1
    }
    else {
        button6.setImage(UIImage(named: "x.png"), for: .normal)
        f1 = -1
    }
    
}

@IBAction func bt7(_ sender: Any) {
    g = g+1
    if g%2==0 {
        button7.setImage(UIImage(named: "o.png"), for: .normal)
        g1 = 1
    }
    else {
        button7.setImage(UIImage(named: "x.png"), for: .normal)
        g1 = -1
}

}

@IBAction func bt8(_ sender: Any) {
    h = h+1
        if h%2==0 {
            button8.setImage(UIImage(named: "o.png"), for: .normal)
            h1 = 1
        }
        else {
            button8.setImage(UIImage(named: "x.png"), for: .normal)
            h1 = -1
    }
    
}


@IBAction func bt9(_ sender: Any) {
    i = i+1
        if i%2==0 {
            button9.setImage(UIImage(named: "o.png"), for: .normal)
            i1 = 1
        }
        else {
            button9.setImage(UIImage(named: "x.png"), for: .normal)
            i1 = -1
	}

}

}

I can’t call the variable a1, b1, c1, etc

Please help

can you try moving the declaration just below the a=0,b=0,etc

Hi,

That didn’t work. I declared the variables right below the a = 0 etc. Still don’t know what to do. What should I do now?

Thanks for your help,
YourAvgCoder

i think something might be wrong on how it is written if thats the case, try deleting and retyping it

That didn’t work :cry:

thats weird… try compressing your project so we can take a look at your code ourselves