用于保持游戏得分的代码

时间:2014-11-28 17:27:43

标签: swift

我正在尝试编写跟踪岩石剪刀中的胜利,损失和关系的代码部分。程序运行成功,但是当我进入结果屏幕时它会冻结(可能是由于代码)。这是结果的代码。主要问题是与“做”声明的对接。这是:

@IBOutlet var yourChoice: UILabel!
@IBOutlet var computerChoice: UILabel!
@IBOutlet var results: UILabel!
@IBOutlet weak var score: UILabel!
@IBOutlet weak var scoreYou: UILabel!
@IBOutlet weak var tiesScore: UILabel!
@IBOutlet weak var scoreYou2: UILabel!
@IBOutlet weak var scoreTies2: UILabel!
@IBOutlet weak var scoreComputer2: UILabel!

var toPass: String!
var aiInfo: String!
var ai = arc4random_uniform(3)
var x: Int = 0 // Player Score
var y: Int = 0 // Ties
var z: Int = 0 // Computer Score

override func viewDidLoad() {
    super.viewDidLoad()

    self.yourChoice.text = "Your Choice: \(toPass)"

    if ai == 0 {
        aiInfo = "Rock"
    } else if ai == 1 {
        aiInfo = "Paper"
    } else if ai == 2 {
        aiInfo = "Scissors"
    } else {
        aiInfo = "Unknown"
    }
    self.computerChoice.text = "Computer Choice: \(aiInfo)"

    if toPass == aiInfo {
        self.results.text = "Results: Tie!"
        self.results.textColor = UIColor.blueColor()
    } else if toPass == "Rock" && aiInfo == "Scissors" {
        self.results.text = "Results: You Win!"
        self.results.textColor = UIColor.greenColor()
    } else if toPass == "Paper" && aiInfo == "Rock" {
        self.results.text = "Results: You Win!"
        self.results.textColor = UIColor.greenColor()
    } else if toPass == "Scissors" && aiInfo == "Paper" {
        self.results.text = "Results: You Win!"
        self.results.textColor = UIColor.greenColor()
    } else {
        self.results.text = "Results: You Lose!"
        self.results.textColor = UIColor.redColor()
    }

    do {
        x + 1
    } while results.text == "Results: You Win!"
    do {
        y + 1
    } while results.text == "Results: Tie!"
    do {
        z + 1
    } while results.text == "Results: You Lose!"

    scoreYou2.text = "\(x)"
    scoreTies2.text = "\(y)"
    scoreComputer2.text = "\(z)"
}

1 个答案:

答案 0 :(得分:1)

尝试改变:

do {
        x + 1
    } while results.text == "Results: You Win!"
    do {
        y + 1
    } while results.text == "Results: Tie!"
    do {
        z + 1
    } while results.text == "Results: You Lose!"

使用:

if results.text == "Results: You Win!" {
    x++
}
if results.text == "Results: Tie!" {
    y++
}
if results.text == "Results: You Lose!" {
    z++
}