simulator_test.go 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  1. package main
  2. import (
  3. "bufio"
  4. "fmt"
  5. "github.com/stretchr/testify/assert"
  6. "os"
  7. "testing"
  8. )
  9. func TestSimulation_SimulateOneInitialWord(t *testing.T) {
  10. type fields struct {
  11. Words []string
  12. }
  13. type args struct {
  14. initialWord string
  15. }
  16. tests := []struct {
  17. name string
  18. words []string
  19. fields fields
  20. args args
  21. wantedLossCount int
  22. wantedMaxRounds int
  23. wantedTotalRounds int
  24. }{
  25. {
  26. name: "simple game with no losses",
  27. words: []string{"aaa", "aab", "abb"},
  28. args: args{
  29. initialWord: "aaa",
  30. },
  31. wantedLossCount: 0,
  32. wantedMaxRounds: 2,
  33. wantedTotalRounds: 5,
  34. },
  35. {
  36. name: "simple failed game",
  37. words: []string{"aaa", "aab", "aac", "aad", "aae"},
  38. args: args{
  39. initialWord: "aaa",
  40. },
  41. wantedLossCount: 1,
  42. wantedMaxRounds: 5,
  43. wantedTotalRounds: 15,
  44. },
  45. }
  46. for _, tt := range tests {
  47. t.Run(tt.name, func(t *testing.T) {
  48. game := &Game{}
  49. game.Words = make(map[string]*Word)
  50. for _, word := range tt.words {
  51. game.Words[word] = &Word{Word: word}
  52. }
  53. s := Simulation{
  54. Game: game,
  55. }
  56. lossCount, maxRounds, totalRounds := s.SimulateOneInitialWord(game, tt.args.initialWord)
  57. assert.Equalf(t, tt.wantedLossCount, lossCount, "loss count for %v => %v", game, tt.args.initialWord)
  58. assert.Equalf(t, tt.wantedMaxRounds, maxRounds, "max rounds for %v => %v)", game, tt.args.initialWord)
  59. assert.Equalf(t, tt.wantedTotalRounds, totalRounds, "total rounds for %v => %v)", game, tt.args.initialWord)
  60. })
  61. }
  62. }
  63. func TestSimulation_SimulateOneGame(t *testing.T) {
  64. type fields struct {
  65. BestGuess string
  66. BestGuessRounds int
  67. SuccessCount int
  68. FailCount int
  69. TotalRounds int
  70. }
  71. type args struct {
  72. initialWord string
  73. answer string
  74. }
  75. tests := []struct {
  76. name string
  77. words []string
  78. fields fields
  79. args args
  80. won bool
  81. numRounds int
  82. }{
  83. {
  84. name: "initial word is answer",
  85. words: []string{"aaa", "aab", "abb"},
  86. args: args{
  87. initialWord: "aaa",
  88. answer: "aaa",
  89. },
  90. won: true,
  91. numRounds: 1,
  92. },
  93. {
  94. name: "one guess to get answer",
  95. words: []string{"aaa", "aab", "abb"},
  96. args: args{
  97. initialWord: "aaa",
  98. answer: "aab",
  99. },
  100. won: true,
  101. numRounds: 2,
  102. },
  103. {
  104. name: "one guess to get answer",
  105. words: []string{"aaa", "aab", "aac", "aad", "aae"},
  106. args: args{
  107. initialWord: "aaa",
  108. answer: "aae",
  109. },
  110. won: false,
  111. numRounds: 5,
  112. },
  113. }
  114. for _, tt := range tests {
  115. t.Run(tt.name, func(t *testing.T) {
  116. game := &Game{}
  117. game.Words = make(map[string]*Word)
  118. for _, word := range tt.words {
  119. game.Words[word] = &Word{Word: word}
  120. }
  121. s := NewSimulator(*game)
  122. got, got1 := s.SimulateOneGame(game, tt.args.initialWord, tt.args.answer)
  123. assert.Equalf(t, tt.won, got, "SimulateOneGame(%v, %v, %v)", game, tt.args.initialWord, tt.args.answer)
  124. assert.Equalf(t, tt.numRounds, got1, "SimulateOneGame(%v, %v, %v)", game, tt.args.initialWord, tt.args.answer)
  125. })
  126. }
  127. }
  128. func TestSimulation_SimulateAllPossibleGames(t *testing.T) {
  129. tests := []struct {
  130. name string
  131. words []string
  132. want string
  133. wantErr assert.ErrorAssertionFunc
  134. }{
  135. {
  136. name: "simple game with no losses",
  137. words: []string{"aaa", "aab", "aac", "aad", "aae"},
  138. want: "aaa",
  139. wantErr: assert.NoError,
  140. },
  141. {
  142. name: "simple game with no losses",
  143. words: []string{"hazards", "hurting", "manages", "varying", "gabbing", "packing", "warring", "cantina", "caravan", "gangers", "falling", "waiting", "screens", "happens", "largest", "lawless", "dangers", "variety"},
  144. want: "warring",
  145. wantErr: assert.NoError,
  146. },
  147. }
  148. for _, tt := range tests {
  149. t.Run(tt.name, func(t *testing.T) {
  150. game := &Game{}
  151. game.Words = make(map[string]*Word)
  152. for _, word := range tt.words {
  153. game.Words[word] = &Word{Word: word}
  154. }
  155. s := NewSimulator(*game)
  156. got, err := s.SimulateAllPossibleGames()
  157. if !tt.wantErr(t, err, fmt.Sprintf("SimulateAllPossibleGames()")) {
  158. return
  159. }
  160. assert.Equalf(t, tt.want, got, "SimulateAllPossibleGames()")
  161. })
  162. }
  163. }
  164. func TestSimulation_SimulateAllPossibleGamesFromGameLog(t *testing.T) {
  165. file, err := os.Open("games.txt")
  166. if err != nil {
  167. t.Fatalf("Error opening file: %s", err)
  168. }
  169. defer file.Close()
  170. var words []string
  171. scanner := bufio.NewScanner(file)
  172. for scanner.Scan() {
  173. line := scanner.Text()
  174. fmt.Println(line)
  175. if line == "." {
  176. game := &Game{}
  177. game.Words = make(map[string]*Word)
  178. for _, word := range words {
  179. game.Words[word] = &Word{Word: word}
  180. }
  181. s := NewSimulator(*game)
  182. _, err := s.SimulateAllPossibleGames()
  183. assert.NoError(t, err)
  184. words = nil
  185. // skip the answer
  186. scanner.Scan()
  187. for scanner.Scan() {
  188. line = scanner.Text()
  189. fmt.Printf("Got line: %s\n", line)
  190. if line != "" {
  191. break
  192. }
  193. }
  194. }
  195. fmt.Printf("Adding word: %s\n", line)
  196. words = append(words, line)
  197. }
  198. }