Quellcode durchsuchen

fix: better detection of game end

Detect error when no answer and no words left
Alex White vor 5 Monaten
Ursprung
Commit
5f352bbd1d
1 geänderte Dateien mit 12 neuen und 6 gelöschten Zeilen
  1. 12 6
      fallout.go

+ 12 - 6
fallout.go

@@ -35,6 +35,18 @@ func main() {
 
 		game.FilterWords(guess, score)
 
+		if len(game.Words) == 0 {
+			fmt.Printf("\n\nFATAL ERROR: something went wrong!  No words are left, this shouldn't happen!!!!!!!\n\n")
+			os.Exit(1)
+
+		} else if len(game.Words) == 1 {
+			fmt.Printf("Game is down to one word: %+v\n", game.Words)
+			for word := range game.Words {
+				fmt.Printf("\n\nThe answer is: %s\n\n", word)
+				os.Exit(0)
+			}
+		}
+
 		err = game.scoreWordsByCommonLetterLocations()
 		if err != nil {
 			fmt.Printf("Error scoring Words: %s\n", err)
@@ -54,12 +66,6 @@ func main() {
 		}
 		fmt.Printf("Best Guess: %s", bestGuess)
 
-		if len(game.Words) == 1 {
-			for word := range game.Words {
-				fmt.Printf("The word is: %s\n", word)
-				os.Exit(0)
-			}
-		}
 	}
 }