Finish initial implementation of Project 2

This commit is contained in:
CypherPoet 2019-01-14 22:35:58 -05:00
parent a75509be15
commit c7c1ac607f
2 changed files with 61 additions and 8 deletions

View File

@ -24,21 +24,30 @@
<state key="normal" image="us">
<color key="titleColor" red="0.45880425499999999" green="0.42457667529999998" blue="0.85103040929999996" alpha="1" colorSpace="custom" customColorSpace="displayP3"/>
</state>
<connections>
<action selector="buttonTapped:" destination="BYZ-38-t0r" eventType="touchUpInside" id="60c-f8-bO2"/>
</connections>
</button>
<button opaque="NO" contentMode="scaleToFill" contentHorizontalAlignment="center" contentVerticalAlignment="center" lineBreakMode="middleTruncation" translatesAutoresizingMaskIntoConstraints="NO" id="ZzW-Kt-38n">
<button opaque="NO" tag="1" contentMode="scaleToFill" contentHorizontalAlignment="center" contentVerticalAlignment="center" lineBreakMode="middleTruncation" translatesAutoresizingMaskIntoConstraints="NO" id="ZzW-Kt-38n">
<rect key="frame" x="87.5" y="230" width="200" height="100"/>
<color key="backgroundColor" red="0.98754758880000004" green="0.35643531880000001" blue="0.43980186630000001" alpha="1" colorSpace="custom" customColorSpace="displayP3"/>
<size key="titleShadowOffset" width="2" height="-2"/>
<state key="normal" image="us">
<color key="titleColor" red="0.45880425499999999" green="0.42457667529999998" blue="0.85103040929999996" alpha="1" colorSpace="custom" customColorSpace="displayP3"/>
</state>
<connections>
<action selector="buttonTapped:" destination="BYZ-38-t0r" eventType="touchUpInside" id="5VZ-Mb-yvZ"/>
</connections>
</button>
<button opaque="NO" contentMode="scaleToFill" contentHorizontalAlignment="center" contentVerticalAlignment="center" lineBreakMode="middleTruncation" translatesAutoresizingMaskIntoConstraints="NO" id="NEb-PX-xnZ">
<button opaque="NO" tag="2" contentMode="scaleToFill" contentHorizontalAlignment="center" contentVerticalAlignment="center" lineBreakMode="middleTruncation" translatesAutoresizingMaskIntoConstraints="NO" id="NEb-PX-xnZ">
<rect key="frame" x="87.5" y="360" width="200" height="100"/>
<color key="backgroundColor" red="0.98754758880000004" green="0.35643531880000001" blue="0.43980186630000001" alpha="1" colorSpace="custom" customColorSpace="displayP3"/>
<state key="normal" image="us">
<color key="titleColor" red="0.45880425499999999" green="0.42457667529999998" blue="0.85103040929999996" alpha="1" colorSpace="custom" customColorSpace="displayP3"/>
</state>
<connections>
<action selector="buttonTapped:" destination="BYZ-38-t0r" eventType="touchUpInside" id="U4R-CP-ARt"/>
</connections>
</button>
</subviews>
<color key="backgroundColor" red="1" green="1" blue="1" alpha="1" colorSpace="custom" customColorSpace="sRGB"/>

View File

@ -30,7 +30,8 @@ class ViewController: UIViewController {
@IBOutlet var button3: UIButton!
var currentScore = 0
var flagChoiceKeys = [String]()
var correctFlagKey: String?
override func viewDidLoad() {
super.viewDidLoad()
@ -42,15 +43,18 @@ class ViewController: UIViewController {
func askQuestion() {
let flagKeys = flagFilePathsAndNames.keys.shuffled()[..<3]
let flagToGuess = flagFilePathsAndNames[flagKeys.randomElement() ?? flagKeys[0]]
flagChoiceKeys = Array(flagFilePathsAndNames.keys.shuffled()[..<3])
correctFlagKey = flagChoiceKeys.randomElement()
if let _flagToGuess = flagToGuess {
title = "Which flag belongs to \(_flagToGuess)"
if let _key = correctFlagKey {
if let flagName = flagFilePathsAndNames[_key] {
// 📝 If we didn't get here, that would be a serious problem that needed more robust error handling
title = "Which flag belongs to \(flagName)?"
}
}
for (index, button) in [button1, button2, button3].enumerated() {
button?.setImage(UIImage(named: flagKeys[index]), for: .normal)
button?.setImage(UIImage(named: flagChoiceKeys[index]), for: .normal)
}
}
@ -61,5 +65,45 @@ class ViewController: UIViewController {
button?.layer.borderColor = UIColor(red: 1.00, green: 0.28, blue: 0.38, alpha: 1.00).cgColor
}
}
func handleChoice(wasCorrect: Bool) {
if wasCorrect {
title = "Correct!"
currentScore += 1
} else {
title = "Incorrect!"
currentScore = min(currentScore - 3, 0)
}
let responseMessage = "Your score is now \(currentScore)."
let alertController = UIAlertController(title: title, message: responseMessage, preferredStyle: .alert)
alertController.addAction(
UIAlertAction(title: "Continue", style: .default, handler: _askAnotherQuestion)
)
present(alertController, animated: true)
}
@IBAction func buttonTapped(_ sender: UIButton) {
let flagKeyChosen = flagChoiceKeys[sender.tag]
if flagKeyChosen == correctFlagKey {
handleChoice(wasCorrect: true)
} else {
handleChoice(wasCorrect: false)
}
}
/// callback for the UIAlertAction that conforms to the signature it expects
/// (that is, taking a UIAlertAction argument).
///
/// All we really want to do, though, is ask another quesstion, so this function
/// just hides the handler implementation detail away from `askQuestion`
func _askAnotherQuestion(action: UIAlertAction) {
askQuestion()
}
}