wordle

package module
v0.0.0-...-f0ed419 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 3, 2022 License: Apache-2.0 Imports: 6 Imported by: 0

README

Wordle Play

A little utility to "play" wordle games. Given a dictionary of possible words, it can try to guess at the answer using various strategies.

Requires the go 1.18 toolchain. Run go run ./cmd/... to run the utility.

A utility for playing "wordle" games on the commandline. Useful for exploring playing strategies.

Usage:
  wordle [command]

Available Commands:
  help        Help about any command
  interact    Interactively guess a wordle answer.
  play        Play automatically with the given answer.

Flags:
  -d, --debug                     Enable debug logging
      --exp float                 Scale weighted strategy by this exponent (default 1)
      --fallback string           Fallback strategy when a simpler strategy is needed (default "freq")
      --fallback-threshold int    Threshold where the fallback strategy is used (default 150)
      --hail-mary string          Choose a different strategy for the final guess. (default "freq")
  -h, --help                      help for wordle
  -o, --open stringArray          Force an opening sequence of guesses
      --score string              Choose among weighted words. One of: random, top (default "random")
      --seed int                  Random seed
  -s, --strategy string           Play strategy. One of: common, diversity, filtering, naive, selective (default "filtering")
      --word-frequencies string   Word frequency scores. (default "./word_freq.csv")
      --words string              Path to accepted word list (default "./words")

Use "wordle [command] --help" for more information about a command.

Documentation

Index

Constants

View Source
const GuessLimit = 6
View Source
const WordLen = 5

Variables

This section is empty.

Functions

This section is empty.

Types

type CommonLettersStrategy

type CommonLettersStrategy struct {
}

CommonLettersStrategy is a strategy to choose words with the most common letters among the possible words. The hypothesis is that this will get more "yellow" squares, which is useful.

In practice this is a losing strategy.

func NewCommonLettersStrategy

func NewCommonLettersStrategy() CommonLettersStrategy

func (CommonLettersStrategy) Weights

func (x CommonLettersStrategy) Weights(words []Word) []float64

type FilteringStrategy

type FilteringStrategy struct {
	// contains filtered or unexported fields
}

func NewFilteringStrategy

func NewFilteringStrategy(rng *rand.Rand, log Logger, fallback Strategy, threshold int, tiebreaker Scoring) *FilteringStrategy

Select the word that filters the most from the Possible game words. We can only know this based on the actual answer, so we estimate this by finding the average removed across all possible answers.

This is expensive, so we need another strategy for the early game when there are many possible answers.

This strategy helps the endgame where most letters are known, but the number of remaining possible words is higher than the number of remaining guesses. For example, if we play "arbas" and match "gg.gg", we have five possible words to try: "areas", "arias", "arnas", "arpas" and "arras" Trying them 1 by 1, we might run out of guesses. Guessing a word that can't be the answer, but that tests the uncommon letters between the words, can filter out possibilities more quickly. In the above case, "ferny" (where E, R and N) are in 3 of the 5 possible answers, guarantees finding the solution in 1 or 2 additional guesses.

func (FilteringStrategy) Guess

func (n FilteringStrategy) Guess(game *Game) Word

type Fixed

type Fixed struct {
	// contains filtered or unexported fields
}

Play a fixed opening sequence before continuing with a follow-on strategy.

func FixedStrategy

func FixedStrategy(open []Word, followOn Strategy) Fixed

func (Fixed) Guess

func (f Fixed) Guess(game *Game) Word

type Freq

type Freq struct {
	// contains filtered or unexported fields
}

func NewFreq

func NewFreq(weights map[Word]float64, defaultWeight float64) *Freq

Weight more common words higher.

func (*Freq) Weights

func (f *Freq) Weights(words []Word) []float64

type Game

type Game struct {
	// Guesses made by the player, up to GuessLimit.
	Guesses []Guess
	// contains filtered or unexported fields
}

func NewGame

func NewGame(words, used []Word) Game

func (Game) Guess

func (game Game) Guess(word Word, match Match) Game

Guess at the answer

TODO: the calculation of possible answers from the Match is not correct because this implementation assumes that Match yellow, "y", is set for _every_ matching instance of a character, when in fact it's only set for the first _n_ characters where _n_ is the number of instances of that character in the answer. For example, if I guess "geese" and the answer is "embed", I'll get a match of ".yy...", not ".yy.y" as this code expects.

func (Game) Over

func (game Game) Over() bool

func (Game) PossibleAnswers

func (game Game) PossibleAnswers() []Word

func (*Game) RemoveWord

func (game *Game) RemoveWord(removed Word)

Don't try Guess the given word. Useful if the official game doesn't like a word that we choose.

func (Game) String

func (game Game) String() string

func (Game) Won

func (game Game) Won() bool

type Guess

type Guess struct {
	Word  Word
	Match Match
}

Guess is the guess at an answer, along with the Match corresponding to that guess.

func (Guess) FilterPossible

func (g Guess) FilterPossible(words []Word) (possibleAnswers []Word)

Filter a given list of words to include only those that are possible answers given this Guess.

func (Guess) GreenAllows

func (g Guess) GreenAllows(answer Word) bool

Allows returns true if what we know from this Guess's Green squares allows the given answer.

func (Guess) MustInclude

func (g Guess) MustInclude() (must Letters, mustNot Letters)

type HailMary

type HailMary struct {
	// contains filtered or unexported fields
}

func NewHailMary

func NewHailMary(normal, hailmary Strategy) HailMary

A meta strategy which uses a normal strategy for the first five guesses and a "hailmary" strategy for the final guess.

func (HailMary) Guess

func (h HailMary) Guess(game *Game) Word

type LetterCounts

type LetterCounts [26]byte

func (*LetterCounts) Add

func (lc *LetterCounts) Add(c byte)

func (LetterCounts) Len

func (lc LetterCounts) Len() (n int)

func (*LetterCounts) Remove

func (lc *LetterCounts) Remove(c byte) bool

type Letters

type Letters uint32

Letters is a set of wordle letters

func NewLetters

func NewLetters(s []byte) (l Letters)

NewLetters constructs a new Letters set, initialized with the given characters

func (Letters) Add

func (a Letters) Add(b Letters) Letters

func (Letters) AddChar

func (a Letters) AddChar(c byte) Letters

func (Letters) Contains

func (a Letters) Contains(c byte) bool

func (Letters) Empty

func (l Letters) Empty() bool

func (Letters) Intersect

func (a Letters) Intersect(b Letters) Letters

func (Letters) Len

func (l Letters) Len() int

func (Letters) Remove

func (a Letters) Remove(b Letters) Letters

Remove removes b from a

func (Letters) String

func (c Letters) String() string

String displays Letters in a regex-like form.

type Logger

type Logger interface {
	Printf(template string, args ...interface{})
}

Generic debug logger used by some strategies. Compatible with the zerolog package.

type Match

type Match struct {
	// contains filtered or unexported fields
}

Match is the result of guessing the answer: each letter is colored one of: Grey when the letter is not in the answer; Yellow when the letter is in the answer; Green when the letter is in the answer, in this position.

func ParseMatch

func ParseMatch(s string) (Match, error)

ParseMatch parses a 5-letter string describing the "match" of a guess with the answer.

func (*Match) SetUsed

func (m *Match) SetUsed(idx int, exact bool)

func (Match) String

func (m Match) String() string

func (Match) Used

func (m Match) Used(idx int) bool

func (Match) Won

func (m Match) Won() bool

Won returns true when every square of the Match is Green.

type Naive

type Naive struct {
	// contains filtered or unexported fields
}

func NaiveStrategy

func NaiveStrategy(rng *rand.Rand) Naive

func (Naive) Guess

func (n Naive) Guess(game *Game) Word

type Scoring

type Scoring interface {
	Weights(words []Word) []float64
}

Scoring assigns a score, or "weight", to each word in the given array. The weights can be independent or dependent on the other words in the array.

type ScoringCache

type ScoringCache struct {
	// contains filtered or unexported fields
}

func NewScoringCache

func NewScoringCache(scoring Scoring, words []Word) *ScoringCache

func (*ScoringCache) Weights

func (c *ScoringCache) Weights(words []Word) []float64

type SelectiveScoring

type SelectiveScoring struct {
}

func NewSelectiveScale

func NewSelectiveScale() SelectiveScoring

func (SelectiveScoring) Weights

func (x SelectiveScoring) Weights(words []Word) []float64

type Strategy

type Strategy interface {
	Guess(w *Game) Word
}

Strategy is used to choose the next word to play in the given Game.

type Top

type Top struct {
	// contains filtered or unexported fields
}

Top is a strategy to choose word the top words for a given score. If multiple words have the same score, choose randomly among them.

func NewTop

func NewTop(rng *rand.Rand, scoring Scoring) *Top

func (*Top) Guess

func (x *Top) Guess(game *Game) Word

type UniqueLettersScoring

type UniqueLettersScoring struct {
}

func NewUniqueLettersScoring

func NewUniqueLettersScoring() UniqueLettersScoring

func (UniqueLettersScoring) Weights

func (n UniqueLettersScoring) Weights(words []Word) []float64

Weight words based on letter diversity

type WeightedStrategy

type WeightedStrategy struct {
	// contains filtered or unexported fields
}

WeightedStrategy is a strategy to choose words randomly, weighted by the score from a given scoring function.

func NewWeightedStrategy

func NewWeightedStrategy(rng *rand.Rand, scoring Scoring, pow float64) *WeightedStrategy

func (*WeightedStrategy) Guess

func (x *WeightedStrategy) Guess(game *Game) Word

type Word

type Word [WordLen]byte

func ParseWord

func ParseWord(s string) (w Word, err error)

func (Word) LetterCounts

func (w Word) LetterCounts() (lc LetterCounts)

func (Word) Letters

func (w Word) Letters() (l Letters)

func (Word) Match

func (guess Word) Match(actual Word) Match

func (Word) String

func (w Word) String() string

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL