Created
January 16, 2022 04:21
-
-
Save CodeNextPaco/88236b37105d403fd0814fd16ed82771 to your computer and use it in GitHub Desktop.
Flag Project 2 View Controller
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import UIKit | |
class ViewController: UIViewController { | |
@IBOutlet var button1: UIButton! | |
@IBOutlet var button2: UIButton! | |
@IBOutlet var button3: UIButton! | |
var countries = [String]() | |
var correctAnswer = 0 | |
var score = 0 | |
override func viewDidLoad() { | |
super.viewDidLoad() | |
// Do any additional setup after loading the view. | |
button1.layer.borderWidth = 1 | |
button2.layer.borderWidth = 1 | |
button3.layer.borderWidth = 1 | |
button1.layer.borderColor = UIColor.lightGray.cgColor | |
button2.layer.borderColor = UIColor.lightGray.cgColor | |
button3.layer.borderColor = UIColor.lightGray.cgColor | |
countries += ["estonia", "france", "germany", "ireland", "italy", "monaco", "nigeria", "poland", "russia", "spain", "uk", "us"] | |
askQuestion() | |
} | |
func askQuestion() { | |
//shuffle the array | |
countries.shuffle() | |
correctAnswer = Int.random(in: 0...2) | |
title = countries[correctAnswer].uppercased() | |
button1.setImage(UIImage(named: countries[0]), for: .normal) | |
button2.setImage(UIImage(named: countries[1]), for: .normal) | |
button3.setImage(UIImage(named: countries[2]), for: .normal) | |
} | |
@IBAction func buttonTapped(_ sender: UIButton) { | |
print("Button tapped") | |
print(sender.tag) | |
var title: String | |
if (sender.tag) == correctAnswer { | |
title = "Correct" | |
score += 1 | |
} else { | |
title = "Wrong" | |
score -= 1 | |
} | |
//create an Alert View Controller with a message and style. | |
let ac = UIAlertController(title: title, message: "You score is \(score).", preferredStyle: .alert) | |
//add an action to the alert (with a button) to dismiss and move on. | |
ac.addAction(UIAlertAction(title: "next", style: .default, handler: {_ in | |
self.askQuestion() | |
})) | |
//actually present the alert | |
self.present(ac, animated: true, completion: nil) | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
First commit