-
Notifications
You must be signed in to change notification settings - Fork 1
/
ViewController.swift
83 lines (63 loc) · 2.51 KB
/
ViewController.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
//
// ViewController.swift
// GuessTheFlag
//
// Created by Julian Moorhouse on 07/07/2019.
// Copyright © 2019 Mindwarp Consultancy Ltd. All rights reserved.
//
import UIKit
class ViewController: UIViewController {
@IBOutlet var button1: UIButton!
@IBOutlet var button2: UIButton!
@IBOutlet var button3: UIButton!
var countries = [String]()
var score = 0
var correctAnswer = 0
override func viewDidLoad() {
super.viewDidLoad()
countries += ["estonia", "france", "germany", "ireland", "italy", "monaco", "nigeria", "poland", "russia", "spain", "uk", "us"]
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
askQuestion()
}
func askQuestion(action: UIAlertAction! = nil) {
countries.shuffle()
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)
let country = countries[correctAnswer].uppercased()
title = "Guess \(country) ? : Score (\(score) of 10)"
}
@IBAction func buttonTapped(_ sender: UIButton) {
var title: String
var message: String = ""
if sender.tag == correctAnswer {
title = "Correct"
score += 1
} else {
title = "Wrong"
message += "Wrong! Thats the flag of \(countries[sender.tag])\n\n";
score -= 1
}
if (score >= 10) {
message += "Your final score was \(score)"
}
UIView.animate(withDuration: 0.3, delay: 0, usingSpringWithDamping: 0.2, initialSpringVelocity: 5, options: [], animations: {
sender.transform = CGAffineTransform(scaleX: 0.9, y: 0.9)
}) {finished in
sender.transform = .identity
}
if (message != "") {
let ac = UIAlertController(title: title, message: message, preferredStyle: .alert)
ac.addAction(UIAlertAction(title: "Continue", style: .default, handler: askQuestion))
present(ac, animated: true)
} else {
askQuestion()
}
}
}