simulator.go 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. package main
  2. import (
  3. "fmt"
  4. "os"
  5. )
  6. type Simulation struct {
  7. Game *Game
  8. BestGuess string
  9. BestGuessRounds int
  10. SuccessCount int
  11. FailCount int
  12. TotalRounds int
  13. }
  14. func NewSimulator(g Game) *Simulation {
  15. return &Simulation{
  16. Game: &g,
  17. }
  18. }
  19. func (s Simulation) SimulateAllPossibleGames() error {
  20. bestGuessLossCount := 99
  21. bestGuessTotalRounds := 99
  22. wordLossCounts := make(map[string]int)
  23. totalRounds := make(map[string]int)
  24. for initialWord := range s.Game.Words {
  25. fmt.Println(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>")
  26. lossCount, totalRoundCount := s.SimulateOneGame(initialWord)
  27. wordLossCounts[initialWord] = lossCount
  28. totalRounds[initialWord] = totalRoundCount
  29. if lossCount < bestGuessLossCount {
  30. bestGuessLossCount = lossCount
  31. bestGuessTotalRounds = totalRoundCount
  32. } else if lossCount == bestGuessLossCount && totalRoundCount < bestGuessTotalRounds {
  33. bestGuessTotalRounds = totalRoundCount
  34. }
  35. }
  36. fmt.Println(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>")
  37. err := s.Game.scoreWordsByCommonLetterLocations()
  38. if err != nil {
  39. return fmt.Errorf("Error scoring Words: %s", err)
  40. }
  41. for _, word := range s.Game.getSortedScores() {
  42. if wordLossCounts[word] == bestGuessLossCount && totalRounds[word] == bestGuessTotalRounds {
  43. fmt.Printf("\nBest Guess: %s Failed Branches: %d Total Rounds: %d\n\n", word, bestGuessLossCount, totalRounds[word])
  44. break
  45. }
  46. }
  47. return nil
  48. }
  49. func (s Simulation) SimulateOneGame(initialWord string) (int, int) {
  50. lossCount := 0
  51. totalRounds := 0
  52. maxRounds := 4
  53. ANSWER_LOOP:
  54. for answer := range s.Game.Words {
  55. simulatedGame := NewGame()
  56. simulatedGame.Words = make(map[string]*Word)
  57. for word := range s.Game.Words {
  58. simulatedGame.Words[word] = &Word{Word: word}
  59. }
  60. guess := initialWord
  61. for guessRounds := 1; guessRounds <= maxRounds; guessRounds++ {
  62. totalRounds++
  63. if guess == answer {
  64. fmt.Printf("Rounds: %d Initial Word: %s Target Word: %s\n", guessRounds, initialWord, answer)
  65. if guessRounds > maxRounds {
  66. lossCount++
  67. }
  68. continue ANSWER_LOOP
  69. }
  70. score := s.getScore(guess, answer)
  71. //fmt.Printf("Score: %d\n", score)
  72. simulatedGame.FilterWords(guess, score)
  73. //fmt.Printf("Words remaining: %+v\n", simulatedGame.Words)
  74. err := simulatedGame.scoreWordsByCommonLetterLocations()
  75. if err != nil {
  76. fmt.Printf("Error scoring Words: %s\n", err)
  77. os.Exit(1)
  78. }
  79. if len(simulatedGame.Words) == 1 {
  80. //fmt.Printf("The word is: %s\n", guess)
  81. for word := range simulatedGame.Words {
  82. if word != answer {
  83. fmt.Printf("Incorrectly guessed word: %s => This should never happen!\n", word)
  84. os.Exit(1)
  85. }
  86. if guessRounds > maxRounds {
  87. lossCount++
  88. }
  89. fmt.Printf("Rounds: %d Initial Word: %s Target Word: %s\n", guessRounds, initialWord, answer)
  90. continue ANSWER_LOOP
  91. }
  92. } else {
  93. //fmt.Printf("Number of words remaining: %d\n", len(simulatedGame.Words))
  94. }
  95. guess, err = simulatedGame.getBestGuess()
  96. if err != nil {
  97. fmt.Printf("Error calculating best guess: %s\n", err)
  98. os.Exit(1)
  99. }
  100. }
  101. }
  102. fmt.Printf("\nInitial Word: %s Loss Count: %d Total Rounds: %d\n", initialWord, lossCount, totalRounds)
  103. return lossCount, totalRounds
  104. }
  105. func (s Simulation) getScore(guess string, answer string) int {
  106. score := 0
  107. for idx, letter := range answer {
  108. if string(letter) == string(guess[idx]) {
  109. score++
  110. }
  111. }
  112. return score
  113. }