lundi 19 décembre 2016

Tic-Tac-Toe in Swift 3 on Xcode 8 issues

I'm creating a tic-tac-toe game in swift 3. I want an alert to show when the game is won but there is something wrong with my winning logic that I cannot see.

For some reason, the alert works etc, but the user can click anywhere and it will appear even though I've specifically said where to touch. So the way I've tried to put it is that if three images = each other, winner =1 if not then winner = 2. and if winner = 1 then the win alert is triggered. But it seems at the minute that every time the user touches winner changes to 1 anyway. Can anyone shed some light onto why this is?

winAlert() is currently only called in the horizontal win function for now to save time etc.

I have also tried the checkforWin() function without bool and && but using || instead, but this returned the same issues.

class ViewController: UIViewController {

//---------------------OUTLETS------------------------
@IBOutlet weak var turn: UILabel!
@IBOutlet var x: UIImage!
@IBOutlet var o: UIImage!
@IBOutlet weak var tL: UIImageView!
@IBOutlet weak var tM: UIImageView!
@IBOutlet weak var tR: UIImageView!
@IBOutlet weak var mL: UIImageView!
@IBOutlet weak var m: UIImageView!
@IBOutlet weak var mR: UIImageView!
@IBOutlet weak var bL: UIImageView!
@IBOutlet weak var bM: UIImageView!
@IBOutlet weak var bR: UIImageView!
@IBOutlet weak var resetBtn: UIButton!
//-----------------OUTLETS--------------------------

override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.
    turn.text = "It is Player One's turn."
    tL.isUserInteractionEnabled = true
    tM.isUserInteractionEnabled = true
    tR.isUserInteractionEnabled = true
    bL.isUserInteractionEnabled = true
    bM.isUserInteractionEnabled = true
    bR.isUserInteractionEnabled = true
    mL.isUserInteractionEnabled = true
    m.isUserInteractionEnabled = true
    mR.isUserInteractionEnabled = true
    x = UIImage(named: "Cross.jpg")
    o = UIImage(named: "Naught.jpg")


    //--------------------MUSIC-----------------------
    do{
        music = try AVAudioPlayer(contentsOf: URL.init(fileURLWithPath: Bundle.main.path(forResource: "Latmun - Def (Original Mix)", ofType: "mp3")!))
        music.prepareToPlay()
    }
    catch{
        print(error)
    }
    //-------------------end of music----------------
}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}
var currentPlayer = 1
var winner = 0



//------------RESET GAME------------------------------
@IBAction func resetBtn(_ sender: UIButton) {
    resetBoard()
}
func resetBoard() {
    /// clear the images stored in the UIIMageView
    tL.image = nil
    tM.image = nil
    tR.image = nil
    mL.image = nil
    m.image = nil
    mR.image = nil
    bL.image = nil
    bM.image = nil
    bR.image = nil
    // reset the player and update the label text
    currentPlayer = 1
    turn.text = "It is Player One's Turn."
}
//-----------------------RESET GAME--------------------
//--------------------touch-----------------------
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {

    let touch = event?.allTouches!.first!
    // check to see which UIImage view was touched
    if tL.image == nil{
        if tL.frame.contains((touch?.location(in: self.view))!) {
            if currentPlayer == 1{
                tL.image = x
                currentPlayer = 2
                turn.text = "It is Player Two's Turn."
                }
            else{
                tL.image = o
                currentPlayer=1
                turn.text = "It is Player One's Turn."

            }
        }
    }
    if tM.image == nil{
        if tM.frame.contains((touch?.location(in: self.view))!) {
            if currentPlayer == 1{
                tM.image = x
                currentPlayer = 2
                turn.text = "It is Player Two's Turn."

            }
            else{
                tM.image = o
                currentPlayer=1
                turn.text = "It is Player One's Turn."

                }
            }
    }
    if tR.image == nil{
        if tR.frame.contains((touch?.location(in: self.view))!) {
            if currentPlayer == 1{
                tR.image = x
                currentPlayer = 2
                turn.text = "It is Player Two's Turn."

        }
        else{
            tR.image = o
            currentPlayer=1
            turn.text = "It is Player One's Turn."

        }
    }
    }
    if mL.image == nil{
        if mL.frame.contains((touch?.location(in: self.view))!) {
            if currentPlayer == 1{
                mL.image = x
                currentPlayer = 2
                turn.text = "It is Player Two's Turn."

        }
        else{
            mL.image = o
            currentPlayer=1
            turn.text = "It is Player One's Turn."

        }
    }
    }
    if m.image == nil{
        if m.frame.contains((touch?.location(in: self.view))!) {
            if currentPlayer == 1{
                m.image = x
                currentPlayer = 2
                turn.text = "It is Player Two's Turn."
        }
        else{
            m.image = o
            currentPlayer=1
            turn.text = "It is Player One's Turn."
        }
    }
    }
    if mR.image == nil{
        if mR.frame.contains((touch?.location(in: self.view))!) {
            if currentPlayer == 1{
                mR.image = x
                currentPlayer = 2
                turn.text = "It is Player Two's Turn."
        }
        else{
            mR.image = o
            currentPlayer=1
            turn.text = "It is Player One's Turn."
        }
    }
    }
    if bL.image == nil{
        if bL.frame.contains((touch?.location(in: self.view))!) {
            if currentPlayer == 1{
                bL.image = x
                currentPlayer = 2
                turn.text = "It is Player Two's Turn."
        }
        else{
            bL.image = o
            currentPlayer=1
            turn.text = "It is Player One's Turn."
        }
    }
    }
    if bM.image == nil{
        if bM.frame.contains((touch?.location(in: self.view))!) {
            if currentPlayer == 1{
                bM.image = x
                currentPlayer = 2
                turn.text = "It is Player Two's Turn."
        }
        else{
            bM.image = o
            currentPlayer=1
            turn.text = "It is Player One's Turn."
        }
    }
    }
    if bR.image == nil{
        if bR.frame.contains((touch?.location(in: self.view))!) {
            if currentPlayer == 1{
                bR.image = x
                currentPlayer = 2
                turn.text = "It is Player Two's Turn."
        }
        else{
            bR.image = o
            currentPlayer=1
            turn.text = "It is Player One's Turn."
        }
    }
    }

}
    //-----------------Touch--------------------------


//---------------------WIN LOGIC---------------------
//horizontal win
func checkforWin() -> Bool{
    if (tL.image == tM.image && tM.image == tR.image){
        winner = 1
        turn.text = "YOU wins!"
        winAlert()
    }
    else if (mL.image == m.image && m.image == mR.image){
        winner = 1
        winAlert()
    }
    else if (bL.image == bM.image && bM.image == bL.image){
        winner = 1
        winAlert()
    }
    //vertical win
    else if (tL.image == mL.image && mL.image == bL.image){
        winner = 1
        winAlert()
    }
    else if (tM.image == m.image && m.image == bM.image) {
        winner = 1
        winAlert()
    }
    else if (tR.image == mR.image && mR.image == bR.image){
        winner = 1
        winAlert()
    }
    //diagonal win
    else if (tL.image == m.image && m.image == bR.image){
        winner = 1
    }
    else if (tR.image == m.image && m.image == bL.image){
        winner = 1
    }
    else{
        //put in something about number of plays = 9 & no winner
        winner = 2
    }
    if (winner == 1){
        winAlert()
    }
    else{
        drawAlert()
    }
    return true
}

    //---------------------END OF WIN LOGIC-------------------------


func winAlert(){
    if (winner == 1){
        let winner = UIAlertController(title: "\(currentPlayer) Wins!", message: "Well done.", preferredStyle: .alert) // 1

        let firstAction = UIAlertAction(title: "Play again", style: .default) { (alert: UIAlertAction!) -> Void in self.turn.text =  "Player One to start."
        self.resetBoard()
        } // 2

        let secondAction = UIAlertAction(title: "Close", style: .default) { (alert: UIAlertAction!) -> Void in self.turn.text =  "You have ended the game"
        } // 3

    winner.addAction(firstAction) // 4
    winner.addAction(secondAction) // 5

    present(winner, animated: true, completion:nil) // 6
    }
}
func drawAlert(){
    if (winner == 2){
        let winner = UIAlertController(title: "It's A Draw!", message: "Unlucky.", preferredStyle: .alert) // 1

        let firstAction = UIAlertAction(title: "Play again", style: .default) { (alert: UIAlertAction!) -> Void in self.turn.text =  "Player One to start."
            self.resetBoard()
        } // 2

        let secondAction = UIAlertAction(title: "Close", style: .default) { (alert: UIAlertAction!) -> Void in self.turn.text =  "You have ended the game"
        } // 3

        winner.addAction(firstAction) // 4
        winner.addAction(secondAction) // 5

        present(winner, animated: true, completion:nil) // 6
    }
}

Aucun commentaire:

Enregistrer un commentaire