Reputation: 105
I'm trying to write the part of code that keeps track of wins, losses and ties in rock paper scissors. The program runs successfully however, it freezes when I enter the results screen (maybe due to the code). This is the code for results. Main issue is at the buttom with the "do" statements. Here it is:
@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)"
}
Upvotes: 0
Views: 475
Reputation: 236340
try changing:
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!"
with:
if results.text == "Results: You Win!" {
x++
}
if results.text == "Results: Tie!" {
y++
}
if results.text == "Results: You Lose!" {
z++
}
Upvotes: 1