# Day 6 - Media and Swift

I’m going to take a wack at making a tic tac toe game. Let’s see how this goes.

The overall construction of the game is pretty elementary and I get all of the basic concepts right off the bat such as switching out the image from a circle or cross based on a if statement using (next%2==0) to determine if its an odd or even turn. I did get to use the sender.tag to write to an array which player had placed in each available space. Basically setting gameState = [0,0,0…] and then using gameState[sender.tag] = 2 to indicate a cross has been placed in a given space. This 2 is written to the location in the array correlating with the sender tag which is assigned in the main storyboard. End result is something like: gameState = [0, 2, 0…] when sender.tag = 1 because the first space in the array is represented by 0.

The most complicated part of this app was determining who was the winner which was done by creating an array of arrays with all of the winning possibilites and then using a for loop to cycle through this array. See below.

At first this was a little over my head, but now I get it when I think of it like this: When checking to see if the first combination is true [0, 1, 2](all matching items across the top row) the for loop is looking at the gameState[0], gameState[1], gameState[2] to all be equal and it searches for gameState[x] by refering to the winningCombo array to determine which location in the gameState array it is comparing. The “!=0" statement at the end is simply to confirm that it doesn’t come up with a false positive at the beginning of the game when gameState is all 0's.

Still feeling the restlessness of creating loads of poorly designed apps, but i’ll get my fill soon enough and make something pretty. If you care to see the rest of the code it is at the bottom of the post.

Notes:

Image Replacement

• Reset counter variable: if (counter==2) {counter=1}
• Update image: variable.image = UIImage(named: “frame\(counter).png”)

Animation

• Override func viewDidLayoutSubviews(){}
• Override func viewDidAppear(animated: Bool){}
• Link multiple buttons to a function: cntrl+drag from view controller scene
• label.center = CGPointMake(label.center.x, label.center.y)
• UIView.animateWithDuration(1, animations: {}) *include self.*

Tic Tac Toe App

• Align elements to board image rather than view controller
• When button is pressed change to cross image: var image = UIImage(named: “cross.png”)sender.setImage(image, forState: .Normal)
• To set which button function is using: sender.tag
• Modulate between circle&cross: if (goNumber%2 == 0) {}
• retrieve tag of button: sender.tag

Errors

• I had the label button constantly showing up whenever the first button was pushed, I solved this by setting the label.alpha = 0 until it was time to display some text on it.
One clap, two clap, three clap, forty?

By clapping more or less, you can signal to us which stories really stand out.