Skip to content

Commit

Permalink
Made play mode default and the solver 'cheat' mode.
Browse files Browse the repository at this point in the history
  • Loading branch information
archy-bold committed Jan 5, 2022
1 parent 5561bce commit 0b4abb8
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 78 deletions.
156 changes: 78 additions & 78 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ var validWords = []string{}

func main() {
wordPtr := flag.String("word", "", "The game's answer")
playPtr := flag.Bool("play", false, "Whether to play the game")
cheatPtr := flag.Bool("cheat", false, "Whether to run the solver mode")
autoPtr := flag.Bool("auto", false, "Play the game automatically")
allPtr := flag.Bool("all", false, "Play all permutations")
flag.Parse()
Expand Down Expand Up @@ -80,100 +80,100 @@ func main() {
return
}

if *playPtr {
// If no answer given in the word flag, choose
answer := *wordPtr

if answer == "" {
rand.Seed(time.Now().Unix())
answer = validWords[rand.Intn(len(validWords))]
}
g := game.CreateGame(answer, NUM_ATTEMPTS)

// Cheat mode
if *cheatPtr {
strat = strategy.NewCharFrequencyStrategy(NUM_LETTERS, letters, &validWords)
ug := game.CreateUnknownGame(NUM_LETTERS, NUM_ATTEMPTS)
for {
// Play based on whether a strategy is provided
var word string
if strat != nil {
word = strat.GetNextMove()
} else {
fmt.Print("Enter your guess: ")
input, _ := reader.ReadString('\n')
word = strings.TrimSpace(input)
}

success, _ := g.Play(word)
// Print the top 10 answers
fmt.Println("Top answers:")
suggestions := strat.GetSuggestions(10)
for i, suggestion := range suggestions {
if suggestion.Key != "" {
fmt.Printf(" %d: %s (%d)\n", i+1, suggestion.Key, suggestion.Value)
}
}

if strat != nil {
strat.SetMoveOutcome(g.GetLastPlay())
// Read the entered word from stdin
// TODO handle errors such as wrong sized word, wrong pattern for response
fmt.Print("Enter number of entered word, or word itself: ")
word, _ := reader.ReadString('\n')
word = strings.TrimSpace(word)
if idx, err := strconv.Atoi(word); err == nil && idx <= len(suggestions) {
word = suggestions[idx-1].Key
}
wordParts := strings.Split(word, "")

fmt.Print("Enter the result, where x is incorrect, o is wrong position, y is correct eg yxxox: ")
input, _ := reader.ReadString('\n')
parts := strings.Split(strings.TrimSpace((input)), "")
row := make([]game.GridCell, NUM_LETTERS)
for i, chr := range parts {
if chr == "x" {
row[i] = game.GridCell{
Letter: wordParts[i],
Status: game.STATUS_WRONG,
}
} else if chr == "y" {
row[i] = game.GridCell{
Letter: wordParts[i],
Status: game.STATUS_CORRECT,
}
} else if chr == "o" {
row[i] = game.GridCell{
Letter: wordParts[i],
Status: game.STATUS_INCORRECT,
}
}
}

fmt.Println(g.OutputForConsole())
// Update the game grid and strategy
strat.SetMoveOutcome(row)
complete, _ := ug.(*game.UnknownGame).AddResult(row)

if success {
score, of := g.GetScore()
fmt.Printf("Great work! %d/%d\n", score, of)
return
} else if g.HasEnded() {
fmt.Printf("Better luck next time! X/%d\n", NUM_ATTEMPTS)
if complete {
score, _ := ug.GetScore()
fmt.Printf("Hooray! (%d/%d)\n", score, NUM_ATTEMPTS)
return
}
}
}

// Cheat mode
strat = strategy.NewCharFrequencyStrategy(NUM_LETTERS, letters, &validWords)
ug := game.CreateUnknownGame(NUM_LETTERS, NUM_ATTEMPTS)
for {
// If no answer given in the word flag, choose
answer := *wordPtr

// Print the top 10 answers
fmt.Println("Top answers:")
suggestions := strat.GetSuggestions(10)
for i, suggestion := range suggestions {
if suggestion.Key != "" {
fmt.Printf(" %d: %s (%d)\n", i+1, suggestion.Key, suggestion.Value)
}
}
if answer == "" {
rand.Seed(time.Now().Unix())
answer = validWords[rand.Intn(len(validWords))]
}
g := game.CreateGame(answer, NUM_ATTEMPTS)

// Read the entered word from stdin
// TODO handle errors such as wrong sized word, wrong pattern for response
fmt.Print("Enter number of entered word, or word itself: ")
word, _ := reader.ReadString('\n')
word = strings.TrimSpace(word)
if idx, err := strconv.Atoi(word); err == nil && idx <= len(suggestions) {
word = suggestions[idx-1].Key
for {
// Play based on whether a strategy is provided
var word string
if strat != nil {
word = strat.GetNextMove()
} else {
fmt.Print("Enter your guess: ")
input, _ := reader.ReadString('\n')
word = strings.TrimSpace(input)
}
wordParts := strings.Split(word, "")

fmt.Print("Enter the result, where x is incorrect, o is wrong position, y is correct eg yxxox: ")
input, _ := reader.ReadString('\n')
parts := strings.Split(strings.TrimSpace((input)), "")
row := make([]game.GridCell, NUM_LETTERS)
for i, chr := range parts {
if chr == "x" {
row[i] = game.GridCell{
Letter: wordParts[i],
Status: game.STATUS_WRONG,
}
} else if chr == "y" {
row[i] = game.GridCell{
Letter: wordParts[i],
Status: game.STATUS_CORRECT,
}
} else if chr == "o" {
row[i] = game.GridCell{
Letter: wordParts[i],
Status: game.STATUS_INCORRECT,
}
}

success, _ := g.Play(word)

if strat != nil {
strat.SetMoveOutcome(g.GetLastPlay())
}

// Update the game grid and strategy
strat.SetMoveOutcome(row)
complete, _ := ug.(*game.UnknownGame).AddResult(row)
fmt.Println(g.OutputForConsole())

if complete {
score, _ := ug.GetScore()
fmt.Printf("Hooray! (%d/%d)\n", score, NUM_ATTEMPTS)
if success {
score, of := g.GetScore()
fmt.Printf("Great work! %d/%d\n", score, of)
return
} else if g.HasEnded() {
fmt.Printf("Better luck next time! X/%d\n", NUM_ATTEMPTS)
return
}
}
Expand Down
Binary file modified wordle
Binary file not shown.

0 comments on commit 0b4abb8

Please sign in to comment.