这篇文章介绍了一个基于 SwiftUI 的单词游戏应用,通过用户输入一个单词,再根据这个单词的最后一个字母作为起始字母,继续输入下一个单词,不断循环下去。其中,游戏规则有一些挑战,如不允许答案少于三个字母或与起始单词相同等。作者还添加了自定义的得分规则,并在界面上展示了每个玩家的得分情况。最后,作者提出了一些可扩展的功能的建议,如添加难度等级、添加帮助等。
此内容根据文章生成,并经过人工审核,仅用于文章内容的解释与总结
投诉巨石强森(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 } 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英文课程