You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
structContentView:View{@StateprivatevarusedWords=[String]()@StateprivatevarrootWord=""@StateprivatevarnewWord=""varbody:someView{NavigationView{VStack{TextField("Enter your word", text: $newWord, onCommit: addNewWord).textFieldStyle(RoundedBorderTextFieldStyle()).autocapitalization(.none).padding()List(usedWords, id: \.self){Image(systemName:"\($0.count).circle")Text($0)}}.navigationBarTitle(rootWord)}}func addNewWord(){
// lowercase and trim the word, to make sure we don't add duplicate words with case differences
letanswer= newWord.lowercased().trimmingCharacters(in:.whitespacesAndNewlines)
// exit if the remaining string is empty
guard answer.count >0else{return}
// extra validation to come
usedWords.insert(answer, at:0)
newWord =""}}
//
// ContentView.swift
// WordScramble
//
// Created by jrasmusson on 2021-05-01.
//
import SwiftUI
structContentView:View{@StateprivatevarusedWords=[String]()@StateprivatevarrootWord=""@StateprivatevarnewWord=""
// make showing alerts easier
@StateprivatevarerrorTitle=""@StateprivatevarerrorMessage=""@StateprivatevarshowingError=falsevarbody:someView{NavigationView{VStack{TextField("Enter your word", text: $newWord, onCommit: addNewWord).textFieldStyle(RoundedBorderTextFieldStyle()).autocapitalization(.none).padding()List(usedWords, id: \.self){Image(systemName:"\($0.count).circle")Text($0)}}.navigationBarTitle(rootWord).onAppear(perform: startGame).alert(isPresented: $showingError){Alert(title:Text(errorTitle), message:Text(errorMessage), dismissButton:.default(Text("OK")))}}}func addNewWord(){
// lowercase and trim the word, to make sure we don't add duplicate words with case differences
letanswer= newWord.lowercased().trimmingCharacters(in:.whitespacesAndNewlines)
// exit if the remaining string is empty
guard answer.count >0else{return}guardisOriginal(word: answer)else{wordError(title:"Word used already", message:"Be more original")return}guardisPossible(word: answer)else{wordError(title:"Word not recognized", message:"You can't just make them up, you know!")return}guardisReal(word: answer)else{wordError(title:"Word not possible", message:"That isn't a real word.")return}
usedWords.insert(answer, at:0)
newWord =""}func isOriginal(word:String)->Bool{
!usedWords.contains(word)}func isPossible(word:String)->Bool{vartempWord= rootWord
forletterin word {iflet pos = tempWord.firstIndex(of: letter){
tempWord.remove(at: pos)}else{returnfalse}}returntrue}func isReal(word:String)->Bool{letchecker=UITextChecker()letrange=NSRange(location:0, length: word.utf16.count)letmisspelledRange= checker.rangeOfMisspelledWord(in: word, range: range, startingAt:0, wrap:false, language:"en")return misspelledRange.location == NSNotFound
}func wordError(title:String, message:String){
errorTitle = title
errorMessage = message
showingError =true}func startGame(){
// 1. Find the URL for start.txt in our app bundle
iflet startWordsURL =Bundle.main.url(forResource:"start", withExtension:"txt"){
// 2. Load start.txt into a string
iflet startWords =try?String(contentsOf: startWordsURL){
// 3. Split the string up into an array of strings, splitting on line breaks
letallWords= startWords.components(separatedBy:"\n")
// 4. Pick one random word, or use "silkworm" as a sensible default
rootWord = allWords.randomElement()??"silkworm"
// If we are here everything has worked, so we can exit
return}}
// If were are *here* then there was a problem – trigger a crash and report the error
fatalError("Could not load start.txt from bundle.")}}structContentView_Previews:PreviewProvider{staticvarpreviews:someView{ContentView()}}