2017-03-21 2 views
0

私は勝利の部分を稼いでいましたが、ゲームが終わったときにマッサージが表示されない理由を理解していません。私のコード。私は試しましたが、私はゲームコーディングの初心者です。私は非常に助けていただければ幸いです。TIC TAC TOEがスイフト3で勝利しました

import UIKit 

class ViewController: UIViewController { 
    @IBOutlet var winnerLabel: UILabel! 
    @IBOutlet var playAgainButton: UIButton! 

    @IBAction func playAgain(_ sender: Any) { 
     activePlayer = 2 
     activeGame = true 
     gameState = [0, 0, 0, 0, 0, 0, 0, 0, 0] 

     for i in 1..<10{ 
      if let button = view.viewWithTag(i) as? UIButton { 
       button.setImage(nil, for: []) 
      } 

      winnerLabel.isHidden = true 
      playAgainButton.isHidden = true 
     } 
    } 

    // 1 is nought, 2 is cross 
    var activePlayer = 2 
    var activeGame = true 
    var gameState = [0, 0, 0, 0, 0, 0, 0, 0, 0] // 0 - empty 1 - cross 2 - nought 

    let winningCombinations = [[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: UIButton) { // all of the buttons are in this same IBAction. 
     let activePosition = sender.tag - 1 

     if gameState[activePosition] == 0 && activeGame{ 
      gameState[activePosition] = activePlayer // helps type just one answer. either nought or cross. 

      if activePlayer == 2 { 
       sender.setImage(UIImage(named: "cross.png"), for: []) // makes it show the nought.png when button is pressed 
       activePlayer = 1 
      }else{ 
       sender.setImage(UIImage(named: "nought.png"), for: []) // makes it show the cross.png when button is pressed 
       activePlayer = 2 
      } 

      for combinations in winningCombinations{ 
       if gameState[combinations[0]] != 0 && gameState[combinations[0]] == gameState[combinations[1]] && gameState[combinations[1]] == gameState[combinations[2]]{ 
        // we have a winner! 
        activeGame = false 

        winnerLabel.isHidden = false 
        playAgainButton.isHidden = false 

        if gameState[combinations[0]] == 1 { 
         winnerLabel.text = "0 WINS!" 
        }else if gameState[combinations[1]] == 2{ 
         winnerLabel.text = "X WINS!" 
        }else{ 
         winnerLabel.text = "IT'S A DRAW!" // THE PROBLEM. IT DOESN'T DISPLAY THE TEXT "ITS A DRAW" HOW CAN I FIX IT? ANY HELP WILL BE APPRECIATED! THANK YOU !!!! 
        } 
       } 
      } 
     } 
    } 
+3

ドローシナリオの「else」は、あなたが勝者を決定した「if」内にあるためです。ドローがある場合は勝者がないので、他には決して到達しません。ドロー状態は、9ターン後に勝者を持たないことによって決定される。 – Paulw11

+0

あなたの言っていることを理解していますが、実際のコードで表示できると思いますか?私はそれを作るアイデアを持っているように、しかし私はすべて私が思い付くことができると思う..今私はまだ多くのコーディングを理解していないまだ初心者です。それがあまりにもそれを頼むことが私にトンを助けるならば!ありがとうございました。 –

答えて

1

ドローを決定するロジックが正しくありません。ドローは、9回の移動が行われ、勝者がいない場合に発生します。あなたのコードでは、「私たちは勝者がいる」ブロック内のドローをチェックします。勝利したコンビネーションの小切手が真である場合にのみ、それに達することができます。もしあなたが勝者を持っているとあなたはすでに決定しているので、あなたはおそらく抽選を持つことはできません。

ターンカウンターを追加する必要があります。ターンカウンターが勝者なしで9に達すると、引き分けがあります。

var turnCount = 0 

@IBAction func buttonPressed(_ sender: UIButton) { // all of the buttons are in this same IBAction. 
    let activePosition = sender.tag - 1 

    turnCount+= 1 

    if gameState[activePosition] == 0 && activeGame{ 
     gameState[activePosition] = activePlayer // helps type just one answer. either nought or cross. 

     if activePlayer == 2 { 
      sender.setImage(UIImage(named: "cross.png"), for: []) // makes it show the nought.png when button is pressed 
      activePlayer = 1 
     } else { 
      sender.setImage(UIImage(named: "nought.png"), for: []) // makes it show the cross.png when button is pressed 
      activePlayer = 2 
     } 

     for combinations in winningCombinations { 
      if gameState[combinations[0]] != 0 && gameState[combinations[0]] == gameState[combinations[1]] && gameState[combinations[1]] == gameState[combinations[2]]{ 
       // we have a winner! 
       activeGame = false 

       winnerLabel.isHidden = false 
       playAgainButton.isHidden = false 

       if gameState[combinations[0]] == 1 { 
        winnerLabel.text = "0 WINS!" 
       } else if gameState[combinations[1]] == 2{ 
        winnerLabel.text = "X WINS!" 
       } 
      } 
     } 

     if activeGame && turnCount == 9 { 
      winnerLabel.text = "IT'S A DRAW!" 
      activeGame = false 
     } 
    } 
}