Advertisement
Guest User

Untitled

a guest
Nov 5th, 2022
34
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Swift 4.26 KB | Source Code | 0 0
  1. import SwiftUI
  2.  
  3. struct ContentView: View {
  4.     @State private var usedWords = [String]()
  5.     @State private var rootWord = ""
  6.     @State private var newWord = ""
  7.    
  8.     @State private var errorTitle = ""
  9.     @State private var errorMessage = ""
  10.     @State private var showingError = false
  11.    
  12.     @State private var score = 0
  13.        
  14.     var body: some View {
  15.         NavigationView{
  16.             List{
  17.                 Section{
  18.                     TextField("Enter your word", text: $newWord)
  19.                         .autocapitalization(.none)
  20.                 }
  21.            
  22.                 Section {
  23.                     ForEach(usedWords, id: \.self) { word in
  24.                         HStack{
  25.                             Image(systemName: "\(word.count).circle")
  26.                             Text(word)
  27.                         }
  28.                     }
  29.                 }
  30.                
  31.                 Section{
  32.                     HStack{
  33.                         Spacer()
  34.                         Text("Your score: \(score)")
  35.                         Spacer()
  36.                     }
  37.                 }
  38.             }
  39.             .navigationTitle(rootWord)
  40.             .onSubmit(addNewWord)
  41.             .onAppear(perform: startGame)
  42.             .alert(errorTitle, isPresented: $showingError) {
  43.                 Button("OK", role: .cancel) { }
  44.             } message: {
  45.                 Text(errorMessage)
  46.             }
  47.             .toolbar {
  48.                 Button("Start new game") {
  49.                     startGame()
  50.                     usedWords.removeAll()
  51.                     score = 0
  52.                 }
  53.             }
  54.         }
  55.     }
  56.    
  57.     func addNewWord() {
  58.         let answer = newWord.lowercased().trimmingCharacters(in: .whitespacesAndNewlines)
  59.         guard answer.count > 0 else { return }
  60.        
  61.         guard isOriginal(word: answer) else {
  62.             wordError(title: "Word used already", message: "Be more original")
  63.             return
  64.         }
  65.        
  66.         guard isPossible(word: answer) else {
  67.             wordError(title: "Word not possible", message: "You can't spell that word from '\(rootWord)'!")
  68.             return
  69.         }
  70.        
  71.         guard isReal(word: answer) else {
  72.             wordError(title: "Word not recognized", message: "You can't just make them up")
  73.             return
  74.         }
  75.        
  76.         guard answer.count > 2 else {
  77.             wordError(title: "Type more than 3 characters", message: "")
  78.             return
  79.         }
  80.        
  81.         guard answer != rootWord else {
  82.             wordError(title: "Word is the same as root world", message: "You can't do that")
  83.             return
  84.         }
  85.        
  86.         withAnimation{
  87.             usedWords.insert(answer, at: 0)
  88.         }
  89.         newWord = ""
  90.         score += 1
  91.     }
  92.    
  93.     func startGame() {
  94.         if let startWordsURL = Bundle.main.url(forResource: "start", withExtension: "txt") {
  95.             if let startWords = try? String(contentsOf: startWordsURL) {
  96.                 let allWords = startWords.components(separatedBy: "\n")
  97.                 rootWord = allWords.randomElement() ?? "silkworm"
  98.                 return
  99.             }
  100.         }
  101.         fatalError("Could not load start.txt from bundle")
  102.     }
  103.     func isOriginal(word: String) -> Bool {
  104.         !usedWords.contains(word)
  105.     }
  106.     func isPossible(word: String) -> Bool {
  107.         var tempWord = rootWord
  108.         for letter in word {
  109.             if let pos = tempWord.firstIndex(of: letter) {
  110.                 tempWord.remove(at: pos)
  111.             } else {
  112.                 return false
  113.             }
  114.         }
  115.         return true
  116.     }
  117.     func isReal(word: String) -> Bool {
  118.         let checker = UITextChecker()
  119.         let range = NSRange(location: 0, length: word.utf16.count)
  120.         let misspelledRange = checker.rangeOfMisspelledWord(in: word, range: range, startingAt: 0, wrap: false, language: "en")
  121.        
  122.         return misspelledRange.location == NSNotFound
  123.     }
  124.    
  125.     func wordError(title: String, message: String) {
  126.         errorTitle = title
  127.         errorMessage = message
  128.         showingError = true
  129.     }
  130.    
  131. }
  132.  
  133. struct ContentView_Previews: PreviewProvider {
  134.     static var previews: some View {
  135.         ContentView()
  136.     }
  137. }
  138.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement