Skip to content

Commit

Permalink
Able to specify starter word via argument.
Browse files Browse the repository at this point in the history
  • Loading branch information
archy-bold committed Jan 5, 2022
1 parent 3141896 commit 4943681
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 4 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,4 @@ Run the command with no arguments to play a game with a random word.
- `-auto` - Automatically completes the puzzle
- `-word=[answer]` - Set the winning word with this argument.
- `-all` - Runs the auto-solver through every permutation, giving results when complete.
- `-starter=[word]` - Specify the starter word for strategies
7 changes: 4 additions & 3 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ func main() {
wordPtr := flag.String("word", "", "The game's answer")
cheatPtr := flag.Bool("cheat", false, "Whether to run the solver mode")
autoPtr := flag.Bool("auto", false, "Play the game automatically")
starterPtr := flag.String("starter", "", "The starter word to use in strategies")
allPtr := flag.Bool("all", false, "Play all permutations")
flag.Parse()

Expand All @@ -43,15 +44,15 @@ func main() {

var strat strategy.Strategy
if auto {
strat = strategy.NewCharFrequencyStrategy(NUM_LETTERS, letters, &validWords)
strat = strategy.NewCharFrequencyStrategy(NUM_LETTERS, letters, &validWords, *starterPtr)
}

// Play out all permutations
if *allPtr {
sumTries := 0
numSuccesses := 0
for _, answer := range validWords {
strat = strategy.NewCharFrequencyStrategy(NUM_LETTERS, letters, &validWords)
strat = strategy.NewCharFrequencyStrategy(NUM_LETTERS, letters, &validWords, *starterPtr)
g := game.CreateGame(answer, NUM_ATTEMPTS)

for {
Expand Down Expand Up @@ -82,7 +83,7 @@ func main() {

// Cheat mode
if *cheatPtr {
strat = strategy.NewCharFrequencyStrategy(NUM_LETTERS, letters, &validWords)
strat = strategy.NewCharFrequencyStrategy(NUM_LETTERS, letters, &validWords, *starterPtr)
ug := game.CreateUnknownGame(NUM_LETTERS, NUM_ATTEMPTS)
for {

Expand Down
10 changes: 9 additions & 1 deletion strategy/charfrequency.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ type HistogramEntry struct {

// CharFrequencyStrategy a strategy that plays based on the frequency of characters in the solutions list
type CharFrequencyStrategy struct {
attempts int
starter string
wordLength int
letters map[string]bool
validWords *[]string
Expand Down Expand Up @@ -123,6 +125,9 @@ word:

// GetNextMove simply returns the top-ranked word
func (s *CharFrequencyStrategy) GetNextMove() string {
if s.attempts == 0 && s.starter != "" {
return s.starter
}
return s.rankedWords[0].Key
}

Expand Down Expand Up @@ -153,6 +158,8 @@ func (s *CharFrequencyStrategy) SetMoveOutcome(row []game.GridCell) {
}
}

s.attempts++

// Rebuild the histogram and ranking
s.buildHistogram()
s.rankWords()
Expand All @@ -164,13 +171,14 @@ func (s *CharFrequencyStrategy) GetSuggestions(n int) PairList {
}

// NewCharFrequencyStrategy create a char frequency-based strategy given the word list and letters list
func NewCharFrequencyStrategy(wordLength int, letters []string, validWords *[]string) Strategy {
func NewCharFrequencyStrategy(wordLength int, letters []string, validWords *[]string, starter string) Strategy {
lettersMap := map[string]bool{}
for _, l := range letters {
lettersMap[l] = true
}

s := &CharFrequencyStrategy{
starter: starter,
wordLength: wordLength,
letters: lettersMap,
validWords: validWords,
Expand Down
Binary file modified wordle
Binary file not shown.

0 comments on commit 4943681

Please sign in to comment.