巨石强森(Dwayne“ The Rock” Johnson)曾经说过:“成功并不总是与伟大有关,而是与一致性有关。一贯的努力导致成功;伟大将会来临。”

挑战

  • 不允许答案少于三个字母或与我们的起始单词相同。对于三字母检查,最简单的操作是将一个检查放入isReal(),如果单词长度在三个字母以下,则返回false。对于第二部分,只需将起始词与输入词进行比较,如果相同则返回false。
  • 添加一个称为的左键按钮项startGame(),以便用户可以在需要时以一个新单词重新启动。
  • 在下方放置一个文本视图,List以便你可以跟踪并显示给定根词的玩家得分。如何计算分数取决于你,但是涉及单词数量及其字母数的内容是合理的。

自定的得分规则

拼写的单词所含字母数量 3 4 5 6 7 8 超过8
得分 1 2 4 8 12 20 40

并且在拼写第4个单词开始,每拼写成功一次额外加5分

代码

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
import SwiftUI

struct ContentView: View {
@State private var usedWords = [String]()
@State private var rootWord = ""
@State private var newWord = ""

@State private var errorTitle = ""
@State private var errorMessage = ""
@State private var showingError = false

@State private var sumscore: Int = 0
@State private var score: Int = 0

var body: some View {
NavigationView {
VStack {
TextField("Enter your word", text: $newWord, onCommit: addNewWord)
.autocapitalization(.none)
.textFieldStyle(RoundedBorderTextFieldStyle())
.padding()
List(usedWords, id: \.self){
Image(systemName: "\($0.count).circle")
Text($0)
}
Text("You get \(sumscore)")
}
.navigationBarTitle(rootWord)
.navigationBarItems(trailing: Button(action: startGame) {
Image(systemName: "goforward")
.foregroundColor(.black)
})
.onAppear(perform: startGame)
}.alert(isPresented: $showingError){
Alert(---
title: Text(errorTitle), message: Text(errorMessage), dismissButton: .default(Text("OK")))


}

}
func addNewWord() {
//将单词全部小写并删除空格和换行符
let answer = newWord.lowercased().trimmingCharacters(in: .whitespacesAndNewlines)
guard isOriginal(word: answer) else {
wordError(---
title: "word used already", message: "Be more original")
return
}
guard isPossible(word: answer) else {
wordError(---
title: "Word not recongnized", 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 answer.count > 0 else {
return
}

//计算得分
caculationScore(answer: answer)


//将输入的文本加入到数组
usedWords.insert(answer, at: 0)
newWord = ""
}

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() ?? "sillkworm"
//初始化得分和数组
sumscore = 0
usedWords = [String]()
// 执行到这里就可以返回了
return
}
}
//如果产生问题则报错
fatalError("Could not load start.txt form bundle.")
}

func isOriginal(word: String) -> Bool {
!usedWords.contains(word)
}

func isPossible(word: String) -> Bool {
var tempWord = rootWord

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 threewordslim = word.count >= 3

// 检查与游戏词语是否相同
let samewordlim = !(word == rootWord)

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 && threewordslim && samewordlim
}

func wordError(---
title: String, message: String) {
errorTitle = title
errorMessage = message
showingError = true


}
func caculationScore(answer: String){
//计算得分
let wordnum = answer.count
let wordsnum = usedWords.count

switch wordnum {
case 3:
score = 1
case 4:
score = 2
case 5:
score = 4
case 6:
score = 8
case 7:
score = 12
case 8:
score = 20
default:
score = 40
}

//因为是添加之前,所以从第4的单词开始不是-3而是-2
let addscore = (wordsnum - 2) * 5

if addscore > 0 {
score += addscore
}

sumscore += score
score = 0
}
}

struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView()
}
}

参考资料

查看下一天的SwiftUI学习笔记

关于100days英文课程