Hi I'm trying to figure this challenge out but I've run out of ideas, I also tried to put an alert into the load method but I'm kinda stuck on this one. Here's all my code:
//
// ViewController.swift
// Guess The Flag || Project2
//
// Created by George Beres on 05/03/2021.
//
import UIKit
class ViewController: UIViewController {
@IBOutlet weak var button1: UIButton!
@IBOutlet weak var button2: UIButton!
@IBOutlet weak var button3: UIButton!
// Hold the all countries of type "String"
var countries = [String]()
// Holds thr current score
var score = 0
// Holds the correct score
var correctAnswer = 0
// Holds the number of questions asked
var questionsAsked = 0
override func viewDidLoad() {
super.viewDidLoad()
countries += [
"estonia", "france", "germany", "ireland",
"italy", "monaco", "nigeria", "poland",
"russia", "spain", "uk", "us"
]
// Puts a border line around the buttons
button1.layer.borderWidth = 1.5
button2.layer.borderWidth = 1.5
button3.layer.borderWidth = 1.5
// Changes the border line color for the buttons
button1.layer.borderColor = UIColor.lightGray.cgColor
button2.layer.borderColor = UIColor.lightGray.cgColor
button3.layer.borderColor = UIColor.lightGray.cgColor
askQuestion(action: nil)
}
func askQuestion(action: UIAlertAction!) { // Each time this function is called will put different images into the buttons
countries.shuffle() // Mixes up the order of the countries array in-place
// Holds the correct answer within this range
correctAnswer = Int.random(in: 0...2)
button1.setImage(UIImage(named: countries[0]), for: .normal)
button2.setImage(UIImage(named: countries[1]), for: .normal)
button3.setImage(UIImage(named: countries[2]), for: .normal)
// Reads out the selected String from the "countries" array than upper case it
title = "Question Nr: \(questionsAsked) | Score: \(score) | \(countries[correctAnswer].uppercased())"
// Share button is added to grey navigation bar to let users share different data types to different apps
navigationItem.rightBarButtonItem = UIBarButtonItem(barButtonSystemItem: .action, target: self, action: #selector(showFinalScore))
}
@IBAction func buttonTapped(_ sender: UIButton) {
var title: String
// Shows two alerts to the user
if sender.tag == correctAnswer {
title = "Correct!"
score += 1
questionsAsked += 1
} else {
title = "Wrong! That's the flag of \(countries[sender.tag].uppercased())"
score -= 1
saveHighestScore()
questionsAsked += 1
}
showAlert(title: title)
}
func showAlert(title: String) {
if questionsAsked <= 10 {
saveHighestScore()
let ac = UIAlertController(title: title, message: "Your score is \(score)", preferredStyle: .alert)
ac.addAction(UIAlertAction(title: "Continue", style: .default, handler: askQuestion))
present(ac, animated: true)
// Shows a final alert message to the user with a final score after 10 questions answered
} else if questionsAsked > 10 { // Display the final score once the user answered 10 questions
saveHighestScore()
let ac = UIAlertController(title: title, message: "Your final score is \(score)", preferredStyle: .alert)
ac.addAction(UIAlertAction(title: "Play again", style: .default, handler: askQuestion))
present(ac, animated: true)
questionsAsked = 0
score = 0
}
loadHighestScore()
}
@objc func showFinalScore() {
guard let titleButton = title else {
print("There's no title here")
return
}
let vc = UIActivityViewController(activityItems: [titleButton], applicationActivities: [])
vc.popoverPresentationController?.barButtonItem = navigationItem.rightBarButtonItem
present(vc, animated: true)
}
func saveHighestScore() {
UserDefaults.standard.setValue(score, forKey: "score")
}
func loadHighestScore() {
score = UserDefaults.standard.object(forKey: "score") as? Int ?? 0
// let ac = UIAlertController(title: "New score!", message: "Your new score is \(score)", preferredStyle: .alert)
// ac.addAction(UIAlertAction(title: "Play again", style: .default, handler: askQuestion))
// ac.present(ac, animated: true)
// questionsAsked = 0
// score = 0
}
}