Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Tic-Tac-Toe

    • 0
    • 1
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 641
    Comment on it

    Hi all,

    Tic-Tac-Toe is simple game in which you will try to mark your sign in three blocks to make a row , column or diagonal.

     

    RULES FOR TIC-TAC-TOE

    1. The game is played on a grid that's 3 squares by 3 squares.

    2. You are X, your friend (or the computer in this case) is O. Players take turns putting their marks in empty squares.

    3. The first player to get 3 of her marks in a row (up, down, across, or diagonally) is the winner.

    4. When all 9 squares are full, the game is over. If no player has 3 marks in a row, the game ends in a tie.

     

    How to make this game in swift

    1. Make a User Interface .In storyboard , Make a UILabel to show player turn and 3x3 UIButton matrix and make there action in you swift viewcontroller class. For eg :- 

    2. In you UIViewController file , connect your button action like matrix [0] [0] to zeroZeroAction shown above.

    @IBOutlet weak var nextTurnLbl: UILabel!
        var nextTurn:String = "X"
        
        var array: [[Int]] = [[0,0,0],[0,0,0],[0,0,0]]
        
        override func viewDidLoad() {
            super.viewDidLoad()
            // Do any additional setup after loading the view, typically from a nib.
            nextTurnLbl.text = "Turn :" + nextTurn
        }
    
        override func didReceiveMemoryWarning() {
            super.didReceiveMemoryWarning()
            // Dispose of any resources that can be recreated.
        }
    
        @IBAction func zeroZero(sender: UIButton) {
            sender.setTitle(nextTurn, forState: UIControlState.Normal)
            changeTurn(0,y: 0)
        }
        @IBAction func zeroOne(sender: UIButton) {
            sender.setTitle(nextTurn, forState: UIControlState.Normal)
            changeTurn(0,y: 1)
        }
        @IBAction func zeroTwo(sender: UIButton) {
            sender.setTitle(nextTurn, forState: UIControlState.Normal)
            changeTurn(0,y: 2)
        }
        @IBAction func oneZero(sender: UIButton) {
            sender.setTitle(nextTurn, forState: UIControlState.Normal)
            changeTurn(1,y: 0)
        }
        @IBAction func oneOne(sender: UIButton) {
            sender.setTitle(nextTurn, forState: UIControlState.Normal)
            changeTurn(1,y: 1)
        }
        @IBAction func oneTwo(sender: UIButton) {
            sender.setTitle(nextTurn, forState: UIControlState.Normal)
            changeTurn(1,y: 2)
        }
        @IBAction func twoZero(sender: UIButton) {
            sender.setTitle(nextTurn, forState: UIControlState.Normal)
            changeTurn(2,y: 0)
        }
        @IBAction func twoOne(sender: UIButton) {
            sender.setTitle(nextTurn, forState: UIControlState.Normal)
            changeTurn(2,y: 1)
        }
        @IBAction func twoTwo(sender: UIButton) {
            sender.setTitle(nextTurn, forState: UIControlState.Normal)
            changeTurn(2,y: 2)
        }
        
        func changeTurn(x:Int,y:Int){
            if nextTurn == "X"{
                array[x][y] = 1
                nextTurn = "0"
            }else{
                array[x][y] = 2
                nextTurn = "X"
            }
            nextTurnLbl.text = "Turn : \(nextTurn)"
            print(array)
            winCheck()
        }
        
        func winCheck(){
            var rowWin = false
            var colWin = false
            for(var a=0;a<3;a++){
                let rowCheck = array[a][0]
                let colCheck = array[0][a]
                rowWin = true
                colWin = true
                if rowCheck != 0 || colCheck != 0{
                    for(var b=0;b<3;b++){
                        if (rowCheck != array[a][b] && rowCheck != 0){
                            rowWin = false
                        }
                       
                        if (colCheck != array[b][a] && colCheck != 0){
                            colWin = false
                        }
                    }
                    if rowWin == true && rowCheck != 0{
                        print("We have a winner")
                        alertShow(nextTurn == "X" ?"We have a winner 0":"We have a winner X")
                        break
                    }
                    if colWin == true && colCheck != 0 {
                        print("We have a winner")
                        alertShow(nextTurn == "X" ?"We have a winner 0":"We have a winner X")
                        break
                    }
                }
            }
            
            for (var aa = 1;aa<3;aa++){
                if (array[0][0] == aa && array[1][1] == aa && array[2][2] == aa){
                    print("We have a winner")
                   alertShow(nextTurn == "X" ?"We have a winner 0":"We have a winner X")
                    break
                }
                else if (array[0][2] == aa && array[1][1] == aa && array[2][0] == aa){
                    print("We have a winner")
                    alertShow(nextTurn == "X" ?"We have a winner 0":"We have a winner X")
                    break
                }
            }
            var draw=true
            for (var colum = 0;colum<3;colum++){
                for (var row = 0;row<3;row++){
                    if array[colum][row] == 0{
                       draw = false
                    }
                }
            }
            if draw == true {
                alertShow("Match is draw")
            }
            
        }
        
        func alertShow(mess:String){
            let msgAlert = UIAlertController(title: "Result", message: mess, preferredStyle: UIAlertControllerStyle.Alert)
            
            msgAlert.addAction(UIAlertAction(title: "Dismiss", style: .Default, handler: { (action: UIAlertAction!) in
                
            }))
            
            presentViewController(msgAlert, animated: true, completion: nil)
        }
        
    

    first remove all existing function and add these function to your viewcontroller file. Run the code and enjoy your game.

    Thanks For Reading

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: