Skip to content

Instantly share code, notes, and snippets.

@KrauserHuang
Created January 25, 2021 10:11
Show Gist options
  • Save KrauserHuang/c96abdd886ece168df8590e820f2aed5 to your computer and use it in GitHub Desktop.
Save KrauserHuang/c96abdd886ece168df8590e820f2aed5 to your computer and use it in GitHub Desktop.
func checkAnswer() {
if typeTextField.text == String(questionNumber) {
let controller = UIAlertController(title: "答對了", message: "正確答案是\(questionNumber)", preferredStyle: .alert)
let action = UIAlertAction(title: "Re-Play", style: .default) { (_) in
self.playAgain()
}
controller.addAction(action)
present(controller, animated: true, completion: nil)
} else if typeTextField.text! > String(questionNumber) {
if typeTextField.text! >= String(upperBound) {
let controller = UIAlertController(title: "數字太大囉!", message: "請輸入範圍內的數字", preferredStyle: .alert)
let action = UIAlertAction(title: "了解", style: .default) { (_) in
self.typeTextField.text = ""
}
controller.addAction(action)
present(controller, animated: true, completion: nil)
} else {
chanceNumber -= 1
upperBound = Int(typeTextField.text!)!
chanceLabel.text = "你有\(chanceNumber)次機會!"
rangeLabel.text = "範圍 \(lowerBound) ~ \(upperBound)"
typeTextField.text = ""
}
} else if typeTextField.text! < String(questionNumber) {
if typeTextField.text! <= String(lowerBound) {
let controller = UIAlertController(title: "數字太小囉!", message: "請輸入範圍內的數字", preferredStyle: .alert)
let action = UIAlertAction(title: "了解", style: .default) { (_) in
self.typeTextField.text = ""
}
controller.addAction(action)
present(controller, animated: true, completion: nil)
} else {
chanceNumber -= 1
lowerBound = Int(typeTextField.text!)!
chanceLabel.text = "你有\(chanceNumber)次機會!"
rangeLabel.text = "範圍 \(lowerBound) ~ \(upperBound)"
typeTextField.text = ""
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment