package main import ( "bufio" "fmt" "github.com/stretchr/testify/assert" "os" "testing" ) func TestSimulation_SimulateOneInitialWord(t *testing.T) { type fields struct { Words []string } type args struct { initialWord string } tests := []struct { name string words []string fields fields args args wantedLossCount int wantedMaxRounds int wantedTotalRounds int }{ { name: "simple game with no losses", words: []string{"aaa", "aab", "abb"}, args: args{ initialWord: "aaa", }, wantedLossCount: 0, wantedMaxRounds: 2, wantedTotalRounds: 5, }, { name: "simple failed game", words: []string{"aaa", "aab", "aac", "aad", "aae"}, args: args{ initialWord: "aaa", }, wantedLossCount: 1, wantedMaxRounds: 5, wantedTotalRounds: 15, }, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { game := &Game{} game.Words = make(map[string]*Word) for _, word := range tt.words { game.Words[word] = &Word{Word: word} } s := Simulation{ Game: game, } lossCount, maxRounds, totalRounds := s.SimulateOneInitialWord(game, tt.args.initialWord) assert.Equalf(t, tt.wantedLossCount, lossCount, "loss count for %v => %v", game, tt.args.initialWord) assert.Equalf(t, tt.wantedMaxRounds, maxRounds, "max rounds for %v => %v)", game, tt.args.initialWord) assert.Equalf(t, tt.wantedTotalRounds, totalRounds, "total rounds for %v => %v)", game, tt.args.initialWord) }) } } func TestSimulation_SimulateOneGame(t *testing.T) { type fields struct { BestGuess string BestGuessRounds int SuccessCount int FailCount int TotalRounds int } type args struct { initialWord string answer string } tests := []struct { name string words []string fields fields args args won bool numRounds int }{ { name: "initial word is answer", words: []string{"aaa", "aab", "abb"}, args: args{ initialWord: "aaa", answer: "aaa", }, won: true, numRounds: 1, }, { name: "one guess to get answer", words: []string{"aaa", "aab", "abb"}, args: args{ initialWord: "aaa", answer: "aab", }, won: true, numRounds: 2, }, { name: "one guess to get answer", words: []string{"aaa", "aab", "aac", "aad", "aae"}, args: args{ initialWord: "aaa", answer: "aae", }, won: false, numRounds: 5, }, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { game := &Game{} game.Words = make(map[string]*Word) for _, word := range tt.words { game.Words[word] = &Word{Word: word} } s := NewSimulator(*game) got, got1 := s.SimulateOneGame(game, tt.args.initialWord, tt.args.answer) assert.Equalf(t, tt.won, got, "SimulateOneGame(%v, %v, %v)", game, tt.args.initialWord, tt.args.answer) assert.Equalf(t, tt.numRounds, got1, "SimulateOneGame(%v, %v, %v)", game, tt.args.initialWord, tt.args.answer) }) } } func TestSimulation_SimulateAllPossibleGames(t *testing.T) { tests := []struct { name string words []string want string wantErr assert.ErrorAssertionFunc }{ { name: "simple game with no losses", words: []string{"aaa", "aab", "aac", "aad", "aae"}, want: "aaa", wantErr: assert.NoError, }, { name: "simple game with no losses", words: []string{"hazards", "hurting", "manages", "varying", "gabbing", "packing", "warring", "cantina", "caravan", "gangers", "falling", "waiting", "screens", "happens", "largest", "lawless", "dangers", "variety"}, want: "warring", wantErr: assert.NoError, }, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { game := &Game{} game.Words = make(map[string]*Word) for _, word := range tt.words { game.Words[word] = &Word{Word: word} } s := NewSimulator(*game) got, err := s.SimulateAllPossibleGames() if !tt.wantErr(t, err, fmt.Sprintf("SimulateAllPossibleGames()")) { return } assert.Equalf(t, tt.want, got, "SimulateAllPossibleGames()") }) } } func TestSimulation_SimulateAllPossibleGamesFromGameLog(t *testing.T) { file, err := os.Open("games.txt") if err != nil { t.Fatalf("Error opening file: %s", err) } defer func(file *os.File) { err := file.Close() if err != nil { t.Fatalf("Error closing file: %s", err) } }(file) var words []string scanner := bufio.NewScanner(file) for scanner.Scan() { line := scanner.Text() fmt.Println(line) if line == "." { game := &Game{} game.Words = make(map[string]*Word) for _, word := range words { game.Words[word] = &Word{Word: word} } s := NewSimulator(*game) _, err := s.SimulateAllPossibleGames() assert.NoError(t, err) words = nil // skip the answer scanner.Scan() for scanner.Scan() { line = scanner.Text() fmt.Printf("Got line: %s\n", line) if line != "" { break } } } fmt.Printf("Adding word: %s\n", line) words = append(words, line) } }