我让赢的部分工作,但我不明白为什么当游戏结束,它不显示按摩“它的一个抽签”。。。我的代码需要修改。我试过了,但我是游戏编码的乞丐。我非常感谢你的帮助。
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 !!!!
}
}
}
}
}
最佳答案
你决定抽签的逻辑不正确。当9次移动都没有赢家时,会出现平局。您的代码检查“我们有赢家”块中的抽签结果;只有当对赢家组合的检查返回true时,您才能到达它。如果是的话,你就不可能有平局,因为你已经确定你有赢家了。
你需要增加一个转数计数器,如果转数计数器在没有赢家的情况下达到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
}
}
}
关于swift - Swift 3中的TIC TAC TOE赢奖和平局问题,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/42939397/