0

I'm following a tutorial to learn swift and one of the apps is tic tac toe. The code uses lots of arrays and arrays within arrays, and it became confusing pretty quickly. I don't want to move on to the next tutorial until I'm confident in my understand of how this code is working. My questions about this code are as follows:

  • How does combo[] know which space it is referring to when checking for a win?
  • In for combo in winCombos, what is combo's function?
  • How does winCombos work with gameState to tell if there is a winner?

Here is the code:

import UIKit

class ViewController: UIViewController {

    var turnNumber = 1

    var winner = 0

    @IBOutlet weak var button0: UIButton!

    var gameState = [0, 0, 0, 0, 0, 0, 0, 0, 0]

    let winCombos = [[0,1,2], [3,4,5], [6,7,8], [0,3,6], [1,4,7], [2,5,8], [0,4,8], [2,4,6]]

    @IBAction func buttonPressed(sender: AnyObject) {

        if(gameState[sender.tag] == 0) {

            var oImage = UIImage(named: "Tic Tac Toe O.png")
            var xImage = UIImage(named: "Tic Tac Toe X.png")



            if(turnNumber%2 == 0) {

                sender.setImage(oImage, forState: .Normal)
                gameState[sender.tag] = 1
            }else{
                sender.setImage(xImage, forState: .Normal)
                gameState[sender.tag] = 2
            }

            for combo in winCombos {
                if(gameState[combo[0]] == gameState[combo[1]] && gameState[combo[1]] == gameState[combo[2]] && gameState[combo[0]] != 0) {

                    println(combo[0])
                    println(combo[1])
                    println(combo[2])

                    winner = gameState[combo[0]]
                }
            }

            if( winner != 0){
                println("Winner!")
            }

            turnNumber++

        }

    }

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }


}

1 Answer 1

2

The gameState array is simply the cells of the Tic Tac Toe game, with indexes as follows:

 0 | 1 | 2
---+---+---
 3 | 4 | 5
---+---+---
 6 | 7 | 8

From that, it's easy to discern what winCombo is, it's the list of possible wins, such as 0,1,2 (top row) and 2,4,6 (bottom-left to top-right diagonal).

The statement:

for combo in winCombos

is just a loop, setting combo to each of the values in winCombos and using that to see if there's been a win.

So ther first time through the loop, combo is set to [0,1,2] and you can check if all those cells are non-zero and equal to each other - if so, you have a win.

Ditto for subsequent iterations of that loop, where combo takes on the subsequent values in the winCombos array.

Let's say you have a win on the final check 2,4,6 with all the values being X (2 in this case). The checking statement can be gradually narrowed down from:

if(gameState[combo[0]] == gameState[combo[1]] &&
   gameState[combo[1]] == gameState[combo[2]] &&
   gameState[combo[0]] != 0)

to:

if(gameState[2] == gameState[4] && gameState[4] == gameState[6] && gameState[2] != 0)

and, from there:

if(2 == 2 && 2 == 2 && 2 != 0)

to get your winner.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.