Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //
- // ViewController.swift
- // BullsEye
- //
- // Created by Alex Iskander on 11/14/18.
- // Copyright © 2018 Alex Iskander. All rights reserved.
- //
- import UIKit
- class ViewController: UIViewController {
- var currentValue: Int = 0
- var targetValue: Int = 0
- var score: Int = 0
- var round: Int = 0
- @IBOutlet weak var slider: UISlider!
- @IBOutlet weak var targetLabel: UILabel!
- @IBOutlet weak var scoreLabel: UILabel!
- @IBOutlet weak var roundLabel: UILabel!
- override func viewDidLoad() {
- super.viewDidLoad()
- let roundedValue = slider.value.rounded()
- currentValue = Int(roundedValue)
- startNewRound()
- }
- @IBAction func showAlert() {
- let difference = abs(targetValue-currentValue)
- let points = 100 - difference
- score += points
- let title: String
- } if difference ==0 {
- title = "Perfect!"
- } else if difference <5 {
- title = "You Almost had it!"
- } else if difference <10 {
- title = "pretty good"
- } else {
- title = "Not even Close..."
- let message = "You Scored \(points) points"
- let alert = UIAlertController(title: "OK", message: message, preferredStyle: .alert)
- let action = UIAlertAction(title: "Awesome", style: .default, handler: nil)
- alert.addAction(action)
- present(alert, animated: true, completion: nil)
- startNewRound()
- }
- @IBAction func sliderMoved(_ slider: UISlider) {
- let roundedValue = slider.value.rounded()
- currentValue = Int(roundedValue)
- }
- func startNewRound() {
- round += 1
- targetValue = Int.random(in: 1...100)
- currentValue = 50
- slider.value = Float(currentValue)
- updateLabels()
- }
- func updateLabels() {
- targetLabel.text = String(targetValue)
- scoreLabel.text = String(score)
- roundLabel.text = String(round)
- }
- }
Add Comment
Please, Sign In to add comment