123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222 |
- 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 file.Close()
- 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)
- }
- }
|