Guest User

Untitled

a guest
Nov 16th, 2018
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.00 KB | None | 0 0
  1. //
  2. // ViewController.swift
  3. // BullsEye
  4. //
  5. // Created by Alex Iskander on 11/14/18.
  6. // Copyright © 2018 Alex Iskander. All rights reserved.
  7. //
  8.  
  9. import UIKit
  10.  
  11. class ViewController: UIViewController {
  12.  
  13. var currentValue: Int = 0
  14. var targetValue: Int = 0
  15. var score: Int = 0
  16. var round: Int = 0
  17.  
  18.  
  19. @IBOutlet weak var slider: UISlider!
  20. @IBOutlet weak var targetLabel: UILabel!
  21. @IBOutlet weak var scoreLabel: UILabel!
  22. @IBOutlet weak var roundLabel: UILabel!
  23.  
  24. override func viewDidLoad() {
  25. super.viewDidLoad()
  26. let roundedValue = slider.value.rounded()
  27. currentValue = Int(roundedValue)
  28. startNewRound()
  29.  
  30. }
  31.  
  32. @IBAction func showAlert() {
  33.  
  34. let difference = abs(targetValue-currentValue)
  35. let points = 100 - difference
  36.  
  37. score += points
  38.  
  39. let title: String
  40. } if difference ==0 {
  41.  
  42. title = "Perfect!"
  43. } else if difference <5 {
  44. title = "You Almost had it!"
  45. } else if difference <10 {
  46. title = "pretty good"
  47. } else {
  48. title = "Not even Close..."
  49.  
  50.  
  51.  
  52.  
  53. let message = "You Scored \(points) points"
  54.  
  55. let alert = UIAlertController(title: "OK", message: message, preferredStyle: .alert)
  56.  
  57. let action = UIAlertAction(title: "Awesome", style: .default, handler: nil)
  58.  
  59.  
  60.  
  61. alert.addAction(action)
  62.  
  63. present(alert, animated: true, completion: nil)
  64. startNewRound()
  65. }
  66. @IBAction func sliderMoved(_ slider: UISlider) {
  67. let roundedValue = slider.value.rounded()
  68. currentValue = Int(roundedValue)
  69.  
  70. }
  71.  
  72. func startNewRound() {
  73. round += 1
  74. targetValue = Int.random(in: 1...100)
  75. currentValue = 50
  76. slider.value = Float(currentValue)
  77. updateLabels()
  78. }
  79. func updateLabels() {
  80. targetLabel.text = String(targetValue)
  81. scoreLabel.text = String(score)
  82. roundLabel.text = String(round)
  83.  
  84.  
  85.  
  86. }
  87. }
Add Comment
Please, Sign In to add comment