-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathViewController.swift
90 lines (71 loc) · 3.13 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
83
84
85
86
87
88
89
90
//
// ViewController.swift
// Project2
//
// Created by Thomas Kellough on 6/14/19.
// Copyright © 2019 Thomas Kellough. 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
var totalQuestions = 0
var highestScore = 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
navigationItem.rightBarButtonItem = UIBarButtonItem(barButtonSystemItem: .action, target: self, action: #selector(shareScore))
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)
title = "\(countries[correctAnswer].uppercased()) - Score: \(score) Highest: \(highestScore)"
}
@IBAction func buttonTapped(_ sender: UIButton) {
var title: String?
if sender.tag == correctAnswer {
title = "Correct!"
score += 1
} else {
title = "Wrong! That's the flag of \(countries[sender.tag].uppercased())"
score -= 1
}
totalQuestions += 1
if totalQuestions == 10 {
if score > highestScore {
highestScore = score
}
let ac = UIAlertController(title: "Game Over!", message: "This game: \(score) Highest: \(highestScore)", preferredStyle: .alert)
ac.addAction(UIAlertAction(title: "Quit", style: .default, handler: askQuestion))
present(ac, animated: true)
totalQuestions = 0
score = 0
} else {
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)
}
}
@objc func shareScore() {
let textToShare = "My highest score is \(highestScore)! Can you beat me?"
let appLink = "www.placelinktoapphere.com"
let objectToShare = [textToShare, appLink]
let vc = UIActivityViewController(activityItems: objectToShare, applicationActivities: [])
vc.popoverPresentationController?.barButtonItem = navigationItem.backBarButtonItem
present(vc, animated: true)
}
}