-
Notifications
You must be signed in to change notification settings - Fork 3
/
ContentView.swift
205 lines (174 loc) · 6.83 KB
/
ContentView.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
//
// ContentView.swift
// P05C WordScramble
//
// Created by Julian Moorhouse on 07/12/2020.
// Copyright © 2020 Mindwarp Consultancy Ltd. All rights reserved.
//
import SwiftUI
struct ContentView: View {
@State private var usedWords = ["Arthur", "Ford", "Trillian", "Zaphod", "Marvin", "Billy", "Frank", "Mary"]
@State private var rootWord = ""
@State private var newWord = ""
@State private var score = 0
@State private var errorTitle = ""
@State private var errorMessage = ""
@State private var showingError = false
var body: some View {
NavigationView {
GeometryReader { full in
VStack {
TextField("Enter your word", text: $newWord, onCommit: addNewWord)
.textFieldStyle(RoundedBorderTextFieldStyle())
.autocapitalization(.none)
.padding()
List(usedWords, id: \.self) { word in
VStack {
GeometryReader { geo in
HStack {
Image(systemName: "\(word.count).circle")
.foregroundColor(setColour(itemGeo: geo, fullGeo: full))
Text(word)
}
.offset(x: calcOffsetX(itemGeo: geo, fullGeo: full), y: 0)
// .onTapGesture {
// print("Global midy: \(geo.frame(in: .global).midY)")
// print("Local midy : \(geo.frame(in: .local).midY)")
// print("Global h : \(full.frame(in: .global).size.height)")
// print("Local h : \(full.frame(in: .local).size.height)")
//
// print("---")
// }
}
.frame(height: 25)
}
.accessibilityElement(children: .ignore)
.accessibility(label: Text("\(word), \(word.count) letters"))
}
Text("Score: \(score)")
.font(.headline)
.padding()
}
.navigationBarTitle(rootWord)
.navigationBarItems(trailing: Button(action: startGame) {
Text("Restart")
})
.onAppear(perform: startGame)
.alert(isPresented: $showingError) {
Alert(title: Text(errorTitle), message: Text(errorMessage), dismissButton: .default(Text("OK")))
}
}
}
}
// Calculate offset for slide right to left effect when scrolling
func calcOffsetX(itemGeo: GeometryProxy, fullGeo: GeometryProxy) -> CGFloat {
// Top Row
//---
//Global midy: 248.5
//Local midy : 12.5
//Global h : 551.0
//Local h : 551.0
// Bottom Row
//---
// Global midy: 577.5
// Local midy : 12.5
// Global h : 551.0
// Local h : 551.0
let midY = (itemGeo.frame(in: .global).midY) - 26.5
let listHeight = fullGeo.frame(in: .local).size.height
let scrollFactor = midY / listHeight
let width = itemGeo.frame(in: .local).size.width
let position = (scrollFactor * width) - 250//130
return position > 0 ? position : 0
}
func setColour(itemGeo: GeometryProxy, fullGeo: GeometryProxy) -> Color {
let midY = (itemGeo.frame(in: .global).midY) - 26.5
let listHeight = fullGeo.frame(in: .local).size.height
let scrollFactor = Double(midY / listHeight)
return Color(.sRGB, red: 1.0 - scrollFactor, green: scrollFactor, blue: 1.0, opacity: 1.0)
}
func addNewWord() {
let answer = newWord.lowercased().trimmingCharacters(in: .whitespacesAndNewlines)
guard answer.count > 0 else {
return
}
guard isOriginal(word: answer) else {
wordError(title: "Word used already", message: "Be more original")
return
}
guard isPossible(word: answer) else {
wordError(title: "Word not recognized", message: "You can't just make them up, you know!")
return
}
guard isReal(word: answer) else {
wordError(title: "Word not possible", message: "That isn't a real word")
return
}
guard isShort(word: answer) else {
wordError(title: "Word is short", message: "You word must be more than 3 characters!")
return
}
guard isSame(word: answer) else {
wordError(title: "Same word", message: "Your word is the same as the base word!")
return
}
usedWords.insert(answer, at: 0)
newWord = ""
score += 1
}
func startGame() {
if let startWordsURL = Bundle.main.url(forResource: "start", withExtension: "txt") {
if let startWords = try? String(contentsOf: startWordsURL) {
let allWords = startWords.components(separatedBy: "\n")
rootWord = allWords.randomElement() ?? "silkworm"
newWord = ""
score = 0
return
}
}
fatalError("Could not load start.txt from bundle.")
}
func isOriginal(word: String) -> Bool {
!usedWords.contains(word)
}
func isPossible(word: String) -> Bool {
var tempWord = rootWord.lowercased()
for letter in word {
if let pos = tempWord.firstIndex(of: letter) {
tempWord.remove(at: pos)
} else {
return false
}
}
return true
}
func isReal(word: String) -> Bool {
let checker = UITextChecker()
let range = NSRange(location: 0, length: word.utf16.count)
let misspelledRange = checker.rangeOfMisspelledWord(in: word, range: range, startingAt: 0, wrap: false, language: "en")
return misspelledRange.location == NSNotFound
}
func isShort(word: String) -> Bool {
if word.count <= 3 {
return false
}
return true
}
func isSame(word: String) -> Bool {
if word == rootWord {
return false
}
return true
}
func wordError(title: String, message: String) {
score -= 1
errorTitle = title
errorMessage = message
showingError = true
}
}
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView()
}
}