-
Notifications
You must be signed in to change notification settings - Fork 1
/
ViewController.swift
250 lines (196 loc) · 9.92 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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
//
// ViewController.swift
// SwiftyWords
//
// Created by Julian Moorhouse on 04/08/2019.
// Copyright © 2019 Mindwarp Consultancy Ltd. All rights reserved.
//
import UIKit
class ViewController: UIViewController {
var cluesLabel: UILabel!
var answersLabel: UILabel!
var currentAnswer: UITextField!
var scoreLabel: UILabel!
var letterButtons = [UIButton]()
var activatedButtons = [UIButton]()
var solutions = [String]()
var score = 0 {
didSet {
scoreLabel.text = "Score \(score)"
}
}
var level = 1
override func loadView() {
view = UIView()
view.backgroundColor = .white
scoreLabel = UILabel()
scoreLabel.translatesAutoresizingMaskIntoConstraints = false
scoreLabel.textAlignment = .right
scoreLabel.text = "Score: 0"
view.addSubview(scoreLabel)
cluesLabel = UILabel()
cluesLabel.translatesAutoresizingMaskIntoConstraints = false
cluesLabel.font = UIFont.systemFont(ofSize: 24)
cluesLabel.text = "CLUES"
cluesLabel.numberOfLines = 0
cluesLabel.setContentHuggingPriority(UILayoutPriority(1), for: .vertical)
view.addSubview(cluesLabel)
answersLabel = UILabel()
answersLabel.translatesAutoresizingMaskIntoConstraints = false
answersLabel.font = UIFont.systemFont(ofSize: 24)
answersLabel.text = "ANSWERS"
answersLabel.textAlignment = .right
answersLabel.numberOfLines = 0
answersLabel.setContentHuggingPriority(UILayoutPriority(1), for: .vertical)
view.addSubview(answersLabel)
currentAnswer = UITextField()
currentAnswer.translatesAutoresizingMaskIntoConstraints = false
currentAnswer.placeholder = "Tap letters to guess"
currentAnswer.textAlignment = .center
currentAnswer.font = UIFont.systemFont(ofSize: 44)
currentAnswer.isUserInteractionEnabled = false
view.addSubview(currentAnswer)
let submit = UIButton(type: .system)
submit.translatesAutoresizingMaskIntoConstraints = false
submit.setTitle("SUBMIT", for: .normal)
submit.addTarget(self, action: #selector(submitTapped), for: .touchUpInside)
view.addSubview(submit)
let clear = UIButton(type: .system)
clear.translatesAutoresizingMaskIntoConstraints = false
clear.setTitle("CLEAR", for: .normal)
clear.addTarget(self, action: #selector(clearTapped), for: .touchUpInside)
view.addSubview(clear)
let buttonsView = UIView()
buttonsView.translatesAutoresizingMaskIntoConstraints = false
buttonsView.layer.borderWidth = 1
buttonsView.layer.borderColor = UIColor.lightGray.cgColor
view.addSubview(buttonsView)
NSLayoutConstraint.activate([
scoreLabel.topAnchor.constraint(equalTo: view.layoutMarginsGuide.topAnchor),
scoreLabel.trailingAnchor.constraint(equalTo: view.layoutMarginsGuide.trailingAnchor),
cluesLabel.topAnchor.constraint(equalTo: scoreLabel.bottomAnchor),
cluesLabel.leadingAnchor.constraint(equalTo: view.layoutMarginsGuide.leadingAnchor, constant: 100),
cluesLabel.widthAnchor.constraint(equalTo: view.layoutMarginsGuide.widthAnchor, multiplier: 0.6, constant: -100),
answersLabel.topAnchor.constraint(equalTo: scoreLabel.bottomAnchor),
answersLabel.trailingAnchor.constraint(equalTo: view.layoutMarginsGuide.trailingAnchor, constant: -100),
answersLabel.widthAnchor.constraint(equalTo: view.layoutMarginsGuide.widthAnchor, multiplier: 0.4, constant: -100),
answersLabel.heightAnchor.constraint(equalTo: cluesLabel.heightAnchor),
currentAnswer.centerXAnchor.constraint(equalTo: view.centerXAnchor),
currentAnswer.widthAnchor.constraint(equalTo: view.widthAnchor, multiplier: 0.5),
currentAnswer.topAnchor.constraint(equalTo: cluesLabel.bottomAnchor, constant: 20),
submit.topAnchor.constraint(equalTo: currentAnswer.bottomAnchor),
submit.centerXAnchor.constraint(equalTo: view.centerXAnchor, constant: -100),
submit.heightAnchor.constraint(equalToConstant: 44),
clear.centerXAnchor.constraint(equalTo: view.centerXAnchor, constant: 100),
clear.centerYAnchor.constraint(equalTo: submit.centerYAnchor),
clear.heightAnchor.constraint(equalToConstant: 44),
buttonsView.widthAnchor.constraint(equalToConstant: 750),
buttonsView.heightAnchor.constraint(equalToConstant: 350),
buttonsView.centerXAnchor.constraint(equalTo: view.centerXAnchor),
buttonsView.topAnchor.constraint(equalTo: submit.bottomAnchor, constant: 20),
buttonsView.bottomAnchor.constraint(equalTo: view.layoutMarginsGuide.bottomAnchor, constant: -20)
])
let width = 150
let height = 80
for row in 0..<4 {
for column in 0..<5 {
let letterButton = UIButton(type: .system)
letterButton.titleLabel?.font = UIFont.systemFont(ofSize: 36)
letterButton.setTitle("WWW", for: .normal)
letterButton.showsTouchWhenHighlighted = true
let frame = CGRect(x: column * width, y: row * height, width: width, height: height)
letterButton.frame = frame
let buttonBlue = UIColor(red: 0/255, green: 122/255, blue: 255/255, alpha: 1.0)
letterButton.setTitleColor(buttonBlue, for: .normal)
letterButton.addTarget(self, action: #selector(letterTapped), for: .touchUpInside)
buttonsView.addSubview(letterButton)
letterButtons.append(letterButton)
}
}
}
override func viewDidLoad() {
super.viewDidLoad()
DispatchQueue.global(qos: .userInitiated).async { [weak self] in
self?.loadLevel()
}
}
@objc func letterTapped(_ sender: UIButton) {
guard let buttonTitle = sender.titleLabel?.text else { return }
currentAnswer.text = currentAnswer.text?.appending(buttonTitle)
activatedButtons.append(sender)
UIView.animate(withDuration: 1.5, delay: 0, usingSpringWithDamping: 0.5, initialSpringVelocity: 5, options: [], animations: {
sender.alpha = 0.0
}, completion: nil)
}
@objc func submitTapped(_ sender: UIButton) {
guard let answerText = currentAnswer.text else { return }
if let solutionsPosition = solutions.firstIndex(of: answerText) {
activatedButtons.removeAll()
var splitAnswers = answersLabel.text?.components(separatedBy: "\n")
splitAnswers?[solutionsPosition] = answerText
answersLabel.text = splitAnswers?.joined(separator: "\n")
currentAnswer.text = ""
score += 1
if score % 7 == 0 {
let ac = UIAlertController(title: "Well done!", message: "Are you ready for the next level?", preferredStyle: .alert)
ac.addAction(UIAlertAction(title: "Let's go!", style: .default, handler: levelUp))
present(ac, animated: true)
}
} else {
let ac = UIAlertController(title: "Incorrect", message: "Sorry that's a wrong answer!", preferredStyle: .alert)
ac.addAction(UIAlertAction(title: "OK", style: .default, handler: { [weak self] _ in
self?.clearTapped()
self?.score -= 1
}))
present(ac, animated: true, completion: nil)
}
}
@objc func clearTapped() {
currentAnswer.text = ""
for button in activatedButtons {
button.alpha = 1
}
activatedButtons.removeAll()
}
func loadLevel() {
var clueString = ""
var solutionsString = ""
var letterBits = [String]()
if let levelFileURL = Bundle.main.url(forResource: "level\(level)", withExtension: "txt") {
if let levelContents = try? String(contentsOf: levelFileURL) {
var lines = levelContents.components(separatedBy: "\n")
lines.shuffle()
for (index, line) in lines.enumerated() {
let parts = line.components(separatedBy: ": ")
let answer = parts[0]
let clue = parts[1]
clueString += "\(index + 1). \(clue)\n"
let solutionWord = answer.replacingOccurrences(of: "|", with: "")
solutionsString += "\(solutionWord.count) letters\n"
solutions.append(solutionWord)
let bits = answer.components(separatedBy: "|")
letterBits += bits
}
}
}
DispatchQueue.main.async { [weak self] in
self?.cluesLabel.text = clueString.trimmingCharacters(in: .whitespacesAndNewlines)
self?.answersLabel.text = solutionsString.trimmingCharacters(in: .whitespacesAndNewlines)
self?.letterButtons.shuffle()
guard let letterButtons = self?.letterButtons else { return }
if letterButtons.count == letterBits.count {
for i in 0..<letterButtons.count {
letterButtons[i].setTitle(letterBits[i], for: .normal)
}
}
}
}
func levelUp(action: UIAlertAction) {
level += 1
solutions.removeAll(keepingCapacity: true)
loadLevel()
for button in letterButtons {
button.isHidden = false
}
}
}