[IOS] 간단한 랜덤 숫자 맞추기 앱 클론코딩

NOAH·2021년 6월 2일
0

ios

목록 보기
4/15

import UIKit

class ViewController: UIViewController {
    
    var randomValue: Int = 0
    var tryCount: Int = 0
    @IBOutlet weak var slider :UISlider!
    @IBOutlet weak var tryCountLabel: UILabel!
    @IBOutlet weak var sliderValueLabel: UILabel!
    @IBOutlet weak var minimumValueLabel: UILabel!
    @IBOutlet weak var maximumValueLabel: UILabel!
    
    
    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view.
        
        slider.setThumbImage(#imageLiteral(resourceName: "slider_thumb"), for: .normal)
        reset()
        
    }
    
    @IBAction func sliderValueChanged(_ sender: UISlider) {
        print(sender.value)
        
        let integerValue: Int = Int(sender.value)
        sliderValueLabel.text = String(integerValue)
    }
    
    func showAlert(message: String) {
        
        let alert = UIAlertController(title: nil,
                                      message: message,
                                      preferredStyle: .alert)
        
        let okAction =  UIAlertAction(title: "OK",
                                      style: .default) { (action) in self.reset()
        }
        
        alert.addAction(okAction)
        
        present(alert,
                animated: true,
                completion: nil)
        
    }
    
    
    
    @IBAction func touchUpHItButton (_ sender: UIButton) {
        print(slider.value)
        let hitValue: Int = Int(slider.value)
        slider.value = Float(hitValue)
        tryCount += 1
        // tryCountLabel.text = String(tryCount) + "/ 5"
        tryCountLabel.text = "\(tryCount) / 5"
        
        
        
        if randomValue == hitValue {
            //print("You Hit !! ")
            showAlert(message: "You HIT!!")
            reset()
            return // Hit이면 함수 종료
        } else if tryCount >= 5 {
            //print("You lose... ")
            showAlert(message: "You Lose...")
            reset()
        }
        
        
        
        if randomValue > hitValue {
            slider.minimumValue = Float(hitValue)
            minimumValueLabel.text = String(hitValue)
        } else if randomValue < hitValue {
            slider.maximumValue = Float(hitValue)
            maximumValueLabel.text = String(hitValue)
        }
    
        
    }
    
    @IBAction func touchUpResetButton(_ sender: UIButton){
        print("touch up reset button")
        reset()
        
    }
    
    func reset() {
        print("reset!")
        randomValue = Int.random(in: 1...30)
        print(randomValue)
        
        
        tryCount = 0
        tryCountLabel.text = "0 / 5"
        slider.minimumValue = 0
        slider.maximumValue = 30
        slider.value = 15
        
        minimumValueLabel.text = "0"
        maximumValueLabel.text = "30"
        sliderValueLabel.text = "15"
        
        
    }


}

0개의 댓글