Day 6
Calculating points at end of each round
let difference = abs(currentValue - targetValue)
let points = 100 - difference
Calculating score at end of each round
- Initialize score as a global variable
var score = 0
- Update score as the sum of the previous score and current round’s points.
score += points
Displaying score at correct label
- First creating an IBOutlet for the label to display the score.
@IBOutlet weak var scoreLabel: UILabel!
- Connecting this outlet to the label displaying 999999 currently.
- Updating the value of scoreLabel by converting score to string in updateLabels method.
scoreLabel.text = String(score)
Changing the slider value
Score label updated with the new score on clicking Hit Me
Tracking rounds
- Initialize a global variable currentRound to 0.
var currentRound = 0
- Creating an IBOutlet for the label to display the round.
@IBOutlet weak var roundLabel: UILabel!
- Connecting this outlet to the label displaying 999 currently.
- Updating the value of roundLabel by converting currentRound to string in updateLabels method.
- Incrementing currentRound by 1 every time new round starts in startNewRound method
func startNewRound(){
targetValue = Int(arc4random_uniform(101))
currentValue = 50
currentRound += 1
slider.value = Float(currentValue)
updateLabels()
}
func updateLabels(){
targetLabel.text = String(targetValue)
scoreLabel.text = String(score)
roundLabel.text = String(currentRound)
}
Value of round starts with 1
Value of round updating when we click Hit Me