// // ViewController.swift // TTT_SP2018 // // Created by John Robinson on 2/22/18. // Copyright © 2018 John Robinson. All rights reserved. // import UIKit class ViewController: UIViewController { //Game Variables var mBoard: [String] = ["0", "1", "2", "3", "4", "5", "6", "7", "8"] var mBoardArray: [UIImageView] = []; let BOARD_SIZE = 9 let HUMAN_PLAYER = "X" let COMPUTER_PLAYER = "O" var turn = "X" var win = 0 var move = -1 //PUT THE BUTTON OUTLETS HERE //PUT THE LABEL OUTLET HERE //PUT THE NEW GAME BUTTON ACTION HERE, THEN MOVE THE CODE BELOW INSIDE THE { } BRACKETS s1.image = nil s2.image = nil s3.image = nil s4.image = nil s5.image = nil s6.image = nil s7.image = nil s8.image = nil s9.image = nil mBoard = ["0", "1", "2", "3", "4", "5", "6", "7", "8"] infoLabel.text = "Player X's Turn" win = 0 turn = HUMAN_PLAYER /********* DON"T TOUCH ANYTHING BELOW THIS POINT ***************/ override func viewDidLoad() { super.viewDidLoad() // Do any additional setup after loading the view, typically from a nib. mBoardArray.append(s1) mBoardArray.append(s2) mBoardArray.append(s3) mBoardArray.append(s4) mBoardArray.append(s5) mBoardArray.append(s6) mBoardArray.append(s7) mBoardArray.append(s8) mBoardArray.append(s9) infoLabel.text = "Player X's Turn" let tapGesture1 = UITapGestureRecognizer(target: self, action: #selector(ViewController.img1Clicked)) tapGesture1.numberOfTapsRequired = 1 s1.addGestureRecognizer(tapGesture1) let tapGesture2 = UITapGestureRecognizer(target: self, action: #selector(ViewController.img2Clicked)) tapGesture2.numberOfTapsRequired = 1 s2.addGestureRecognizer(tapGesture2) let tapGesture3 = UITapGestureRecognizer(target: self, action: #selector(ViewController.img3Clicked)) tapGesture3.numberOfTapsRequired = 1 s3.addGestureRecognizer(tapGesture3) let tapGesture4 = UITapGestureRecognizer(target: self, action:#selector(ViewController.img4Clicked)) tapGesture4.numberOfTapsRequired = 1 s4.addGestureRecognizer(tapGesture4) let tapGesture5 = UITapGestureRecognizer(target: self, action: #selector(ViewController.img5Clicked)) tapGesture5.numberOfTapsRequired = 1 s5.addGestureRecognizer(tapGesture5) let tapGesture6 = UITapGestureRecognizer(target: self, action: #selector(ViewController.img6Clicked)) tapGesture6.numberOfTapsRequired = 1 s6.addGestureRecognizer(tapGesture6) let tapGesture7 = UITapGestureRecognizer(target: self, action: #selector(ViewController.img7Clicked)) tapGesture7.numberOfTapsRequired = 1 s7.addGestureRecognizer(tapGesture7) let tapGesture8 = UITapGestureRecognizer(target: self, action: #selector(ViewController.img8Clicked)) tapGesture8.numberOfTapsRequired = 1 s8.addGestureRecognizer(tapGesture8) let tapGesture9 = UITapGestureRecognizer(target: self, action: #selector(ViewController.img9Clicked)) tapGesture9.numberOfTapsRequired = 1 s9.addGestureRecognizer(tapGesture9) } override func didReceiveMemoryWarning() { super.didReceiveMemoryWarning() // Dispose of any resources that can be recreated. } // Check for a winner. Return // 0 if no winner or tie yet // 1 if it's a tie // 2 if X won // 3 if O won func checkForWinner() { // Check horizontal wins for i in stride(from: 0, to: 8, by: 3) { //print(i) if (mBoard[i] == HUMAN_PLAYER && mBoard[i+1] == HUMAN_PLAYER && mBoard[i+2] == HUMAN_PLAYER) { win = 2 return } if (mBoard[i] == COMPUTER_PLAYER && mBoard[i+1] == COMPUTER_PLAYER && mBoard[i+2] == COMPUTER_PLAYER) { win = 3 return } } // Check vertical wins for i in 0 ... 2 { if (mBoard[i] == HUMAN_PLAYER && mBoard[i+3] == HUMAN_PLAYER && mBoard[i+6] == HUMAN_PLAYER) { win = 2 return } if (mBoard[i] == COMPUTER_PLAYER && mBoard[i+3] == COMPUTER_PLAYER && mBoard[i+6] == COMPUTER_PLAYER) { win = 3 return } } // Check for diagonal wins if ((mBoard[0] == HUMAN_PLAYER && mBoard[4] == HUMAN_PLAYER && mBoard[8] == HUMAN_PLAYER) || (mBoard[2] == HUMAN_PLAYER && mBoard[4] == HUMAN_PLAYER && mBoard[6] == HUMAN_PLAYER)) { win = 2 return } if ((mBoard[0] == COMPUTER_PLAYER && mBoard[4] == COMPUTER_PLAYER && mBoard[8] == COMPUTER_PLAYER) || (mBoard[2] == COMPUTER_PLAYER && mBoard[4] == COMPUTER_PLAYER && mBoard[6] == COMPUTER_PLAYER)) { win = 3 return } // Check for tie for i in 0...BOARD_SIZE-1 { // If we find a number, then no one has won yet if (mBoard[i] != HUMAN_PLAYER && mBoard[i] != COMPUTER_PLAYER) { win = 0 return } } // If we make it through the previous loop, all places are taken, so it's a tie win = 1 } func showWinStatus(){ print("win = " + String(win)) if(win == 3){ print("Computer Wins!!") infoLabel.text = "Computer Wins !!!" } if(win == 2){ print("Human Wins!!") infoLabel.text = "Human Wins !!!" } if(win == 1){ print("It a Tie!!") infoLabel.text = "It's a Tie !!!" } } func displayBoard(){ print() print(mBoard[0] + " | " + mBoard[1] + " | " + mBoard[2]) print("-----------") print(mBoard[3] + " | " + mBoard[4] + " | " + mBoard[5]) print("-----------") print(mBoard[6] + " | " + mBoard[7] + " | " + mBoard[8]) print() } @objc func img1Clicked(){ if(win == 0){ if(turn == HUMAN_PLAYER){ s1.image = #imageLiteral(resourceName: "x_img.png") turn = COMPUTER_PLAYER infoLabel.text = "Computer's Turn" mBoard[0] = HUMAN_PLAYER displayBoard() checkForWinner() showWinStatus() if(win == 0){ getComputerMove() turn = self.HUMAN_PLAYER infoLabel.text = "Human's Turn" displayBoard() checkForWinner() showWinStatus() } } } } @objc func img2Clicked(){ if(win == 0){ if(turn == HUMAN_PLAYER){ s2.image = #imageLiteral(resourceName: "x_img.png") turn = COMPUTER_PLAYER infoLabel.text = "Computer's Turn" mBoard[1] = HUMAN_PLAYER displayBoard() checkForWinner() showWinStatus() if(win == 0){ getComputerMove() turn = self.HUMAN_PLAYER infoLabel.text = "Human's Turn" displayBoard() checkForWinner() showWinStatus() } } } } @objc func img3Clicked(){ if(win == 0){ if(turn == HUMAN_PLAYER){ s3.image = #imageLiteral(resourceName: "x_img.png") turn = COMPUTER_PLAYER infoLabel.text = "Computer's Turn" mBoard[2] = HUMAN_PLAYER displayBoard() checkForWinner() showWinStatus() if(win == 0){ getComputerMove() turn = self.HUMAN_PLAYER infoLabel.text = "Human's Turn" displayBoard() checkForWinner() showWinStatus() } } } } @objc func img4Clicked(){ if(win == 0){ if(turn == HUMAN_PLAYER){ s4.image = #imageLiteral(resourceName: "x_img.png") turn = COMPUTER_PLAYER infoLabel.text = "Computer's Turn" mBoard[3] = HUMAN_PLAYER displayBoard() checkForWinner() showWinStatus() if(win == 0){ getComputerMove() turn = self.HUMAN_PLAYER infoLabel.text = "Human's Turn" displayBoard() checkForWinner() showWinStatus() } } } } @objc func img5Clicked(){ if(win == 0){ if(turn == HUMAN_PLAYER){ s5.image = #imageLiteral(resourceName: "x_img.png") turn = COMPUTER_PLAYER infoLabel.text = "Computer's Turn" mBoard[4] = HUMAN_PLAYER displayBoard() checkForWinner() showWinStatus() if(win == 0){ getComputerMove() turn = self.HUMAN_PLAYER infoLabel.text = "Human's Turn" displayBoard() checkForWinner() showWinStatus() } } } } @objc func img6Clicked(){ if(win == 0){ if(turn == HUMAN_PLAYER){ s6.image = #imageLiteral(resourceName: "x_img.png") turn = COMPUTER_PLAYER infoLabel.text = "Computer's Turn" mBoard[5] = HUMAN_PLAYER displayBoard() checkForWinner() showWinStatus() if(win == 0){ getComputerMove() turn = self.HUMAN_PLAYER infoLabel.text = "Human's Turn" displayBoard() checkForWinner() showWinStatus() } } } } @objc func img7Clicked(){ if(win == 0){ if(turn == HUMAN_PLAYER){ s7.image = #imageLiteral(resourceName: "x_img.png") turn = COMPUTER_PLAYER infoLabel.text = "Computer's Turn" mBoard[6] = HUMAN_PLAYER displayBoard() checkForWinner() showWinStatus() if(win == 0){ getComputerMove() turn = self.HUMAN_PLAYER infoLabel.text = "Human's Turn" displayBoard() checkForWinner() showWinStatus() } } } } @objc func img8Clicked(){ if(win == 0){ if(turn == HUMAN_PLAYER){ s8.image = #imageLiteral(resourceName: "x_img.png") turn = COMPUTER_PLAYER infoLabel.text = "Computer's Turn" mBoard[7] = HUMAN_PLAYER displayBoard() checkForWinner() showWinStatus() if(win == 0){ getComputerMove() turn = self.HUMAN_PLAYER infoLabel.text = "Human's Turn" displayBoard() checkForWinner() showWinStatus() } } } } @objc func img9Clicked(){ if(win == 0){ if(turn == HUMAN_PLAYER){ s9.image = #imageLiteral(resourceName: "x_img.png") turn = COMPUTER_PLAYER infoLabel.text = "Computer's Turn" mBoard[8] = HUMAN_PLAYER displayBoard() checkForWinner() showWinStatus() if(win == 0){ getComputerMove() turn = self.HUMAN_PLAYER infoLabel.text = "Human's Turn" displayBoard() checkForWinner() showWinStatus() } } } } func getComputerMove() { // First see if there's a move O can make to win for i in 0 ... BOARD_SIZE-1 { if (mBoard[i] != HUMAN_PLAYER && mBoard[i] != COMPUTER_PLAYER) { let curr = mBoard[i] mBoard[i] = COMPUTER_PLAYER checkForWinner() if (win == 3) { print("Computer is making a winning moving to " + String((i))) mBoardArray[i].image = #imageLiteral(resourceName: "o_img.png") return } else { mBoard[i] = curr; } } } // See if there's a move O can make to block X from winning for i in 0 ... BOARD_SIZE-1 { if (mBoard[i] != HUMAN_PLAYER && mBoard[i] != COMPUTER_PLAYER) { let curr = mBoard[i]; // Save the current number mBoard[i] = HUMAN_PLAYER checkForWinner() if (win == 2) { print("Computer is making a blocking moving to " + String((i))) mBoard[i] = self.COMPUTER_PLAYER; mBoardArray[i].image = #imageLiteral(resourceName: "o_img.png") return } else{ mBoard[i] = curr} } } var count = 0 // Generate random move repeat { count += 1 move = Int(arc4random_uniform(9)) print("random move is " + String(move)) } while ((mBoard[move] == HUMAN_PLAYER || mBoard[move] == COMPUTER_PLAYER) && count < 9) if(mBoard[move] == HUMAN_PLAYER || mBoard[move] == COMPUTER_PLAYER){ return } else{ print("Computer is making a random moving to " + String((move))) mBoard[move] = COMPUTER_PLAYER mBoardArray[move].image = #imageLiteral(resourceName: "o_img.png") return } } }