Jelajahi Sumber

fix: consider total rounds, improve answer detection

- consider total rounds in addition to max rounds

- resolve a bug in variable name total vs max
Alex White 5 bulan lalu
induk
melakukan
f678f19173
1 mengubah file dengan 29 tambahan dan 26 penghapusan
  1. 29 26
      simulator.go

+ 29 - 26
simulator.go

@@ -23,9 +23,11 @@ func NewSimulator(g Game) *Simulation {
 func (s Simulation) SimulateAllPossibleGames() (string, error) {
 
 	bestGuessLossCount := 99
+	bestGuessMaxRounds := 99
 	bestGuessTotalRounds := 99
 
 	wordLossCounts := make(map[string]int)
+	totRoundCounts := make(map[string]int)
 	maxRoundCounts := make(map[string]int)
 
 	for initialWord := range s.Game.Words {
@@ -37,18 +39,24 @@ func (s Simulation) SimulateAllPossibleGames() (string, error) {
 			simulatedGame.Words[word] = &Word{Word: word}
 		}
 
-		lossCount, maxRounds, _ := s.SimulateOneInitialWord(simulatedGame, initialWord)
+		lossCount, maxRounds, totalRounds := s.SimulateOneInitialWord(simulatedGame, initialWord)
 		wordLossCounts[initialWord] = lossCount
+		totRoundCounts[initialWord] = totalRounds
 		maxRoundCounts[initialWord] = maxRounds
 		if lossCount < bestGuessLossCount {
 			bestGuessLossCount = lossCount
-			bestGuessTotalRounds = maxRounds
-		} else if lossCount == bestGuessLossCount && maxRounds < bestGuessTotalRounds {
-			bestGuessTotalRounds = maxRounds
+			bestGuessTotalRounds = totalRounds
+			bestGuessMaxRounds = maxRounds
+		} else if lossCount == bestGuessLossCount && maxRounds < bestGuessMaxRounds {
+			bestGuessTotalRounds = totalRounds
+			bestGuessMaxRounds = maxRounds
+		} else if lossCount == bestGuessLossCount && maxRounds == bestGuessMaxRounds && totalRounds < bestGuessTotalRounds {
+			bestGuessTotalRounds = totalRounds
 		}
 
 	}
 	fmt.Println(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>")
+	fmt.Print("Simulation completed\n\n")
 
 	err := s.Game.scoreWordsByCommonLetterLocations()
 	if err != nil {
@@ -57,8 +65,8 @@ func (s Simulation) SimulateAllPossibleGames() (string, error) {
 
 	var bestGuess string
 	for _, word := range s.Game.getSortedScores() {
-		if wordLossCounts[word] == bestGuessLossCount && maxRoundCounts[word] == bestGuessTotalRounds {
-			fmt.Printf("\nBest Guess: %s  Failed Branches: %d  Max Rounds: %d\n\n", word, bestGuessLossCount, maxRoundCounts[word])
+		if wordLossCounts[word] == bestGuessLossCount && totRoundCounts[word] == bestGuessTotalRounds && maxRoundCounts[word] == bestGuessMaxRounds {
+			fmt.Printf("Best Guess: %s  Failed Branches: %d  Total Rounds: %d  Max Rounds: %d\n\n", word, bestGuessLossCount, bestGuessTotalRounds, bestGuessMaxRounds)
 			bestGuess = word
 			break
 		}
@@ -73,7 +81,7 @@ func (s Simulation) SimulateOneInitialWord(game *Game, initialWord string) (loss
 
 	for answer := range s.Game.Words {
 
-		debugPrint(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>")
+		debugPrint(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>\n")
 
 		debugPrint("Target Word: %s\n", answer)
 
@@ -106,11 +114,18 @@ func (s Simulation) SimulateOneGame(simulatedGame *Game, initialWord, answer str
 	var round int
 	for round = 1; round <= 10; round++ {
 
+		if len(simulatedGame.Words) == 1 {
+			debugPrint("Only one guess remaining after filtering: %s\n", guess)
+			break
+		}
+
 		if round == 1 {
 			debugPrint("First guess, initial Word: %s\n", initialWord)
 
 		} else {
 
+			//debugPrint(">>>>>>>>>>>>>>>>>>>>>>>>>>>>\n")
+			//fmt.Printf("Starting Sub Simulation: %+v\n", simulatedGame.Words)
 			//simulatedSubGame := NewGame()
 			//simulatedSubGame.Words = make(map[string]*Word)
 			//for word := range simulatedGame.Words {
@@ -124,6 +139,7 @@ func (s Simulation) SimulateOneGame(simulatedGame *Game, initialWord, answer str
 			//	fmt.Printf("Error simulating all possible games: %s\n", err)
 			//	os.Exit(1)
 			//}
+			//fmt.Printf("RESULT OF SUB Guess: %s\n", guess)
 
 			var err error
 			guess, err = simulatedGame.getBestGuess()
@@ -135,28 +151,15 @@ func (s Simulation) SimulateOneGame(simulatedGame *Game, initialWord, answer str
 
 		}
 
-		score := s.getScore(guess, answer)
-		debugPrint("guess '%s' matches %d locations in answer '%s'\n", guess, score, answer)
-
-		simulatedGame.FilterWords(guess, score)
-		debugPrint("Words remaining after filtering: %+v\n", simulatedGame.Words)
-
 		if guess == answer {
 			break
 		}
 
-		if len(simulatedGame.Words) == 1 {
-			debugPrint("The word is: %s\n", guess)
-			for word := range simulatedGame.Words {
-
-				if word != answer {
-					fmt.Printf("Incorrectly guessed word: %s => This should never happen!\n", word)
-					os.Exit(1)
-				}
+		score := s.getScore(guess, answer)
+		debugPrint("guess '%s' matches %d locations in answer '%s'\n", guess, score, answer)
 
-				break
-			}
-		}
+		simulatedGame.FilterWords(guess, score)
+		debugPrint("Words remaining after filtering: %+v\n", simulatedGame.Words)
 
 		err := simulatedGame.scoreWordsByCommonLetterLocations()
 		if err != nil {
@@ -168,11 +171,11 @@ func (s Simulation) SimulateOneGame(simulatedGame *Game, initialWord, answer str
 	}
 
 	if round > 4 {
-		fmt.Printf("Loss: Matched in %d rounds =>  Initial Word: %s  Target Word: %s\n", round, initialWord, answer)
+		fmt.Printf(" Loss in %d rounds =>  %s .. %s\n", round, initialWord, answer)
 		return false, round
 	}
 
-	fmt.Printf("Win:  Matched in %d rounds =>  Initial Word: %s  Target Word: %s\n", round, initialWord, answer)
+	fmt.Printf("  Win in %d rounds =>  %s .. %s\n", round, initialWord, answer)
 	return true, round
 }