Skip to content

Commit

Permalink
prompt user to play again
Browse files Browse the repository at this point in the history
  • Loading branch information
benfrisbie committed Feb 5, 2022
1 parent 90d21d5 commit e57f647
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 43 deletions.
87 changes: 47 additions & 40 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,9 @@ import (
"bufio"
"flag"
"fmt"
"math/rand"
"os"
"os/signal"
"strings"
"syscall"
"time"

"github.com/benfrisbie/gowordle/pkg/wordle"
Expand All @@ -34,52 +33,60 @@ func main() {
}

log.Debug().Int64("seed", *seed).Msg("initializing game")
random := rand.New(rand.NewSource(*seed))
var game *wordle.Wordle

// setup signal handler
sigs := make(chan os.Signal, 1)
signal.Notify(sigs, syscall.SIGINT, syscall.SIGTERM)
go func() {
sig := <-sigs
fmt.Println(sig)
if game != nil {
fmt.Printf("The correct word was %s\n", game.Word)
}
os.Exit(0)
}()

// load words and game
// load words
words := wordle.NewWords(*wordPath, *solutionsPath)
game = wordle.NewWordle(words.RandomSolution(*seed), *maxGuesses)
fmt.Printf("Random word selected. You have %d guesses. Ready, Set, Go!\n", *maxGuesses)

// start game loop
var reader = bufio.NewReader(os.Stdin)

for {
// prompt user for guess
fmt.Printf("Guess: ")
guess, _ := reader.ReadString('\n')
guess = strings.ToLower(strings.TrimSuffix(guess, "\n"))
game = wordle.NewWordle(words.RandomSolution(random), *maxGuesses)
fmt.Printf("Random word selected. You have %d guesses. Ready, Set, Go!\n", *maxGuesses)

// validate guess
if len(guess) != len(game.Word) {
fmt.Printf("incorrect length\n")
continue
} else if !words.Exists(guess) {
fmt.Printf("not a real word\n")
continue
}
// start game loop
for {
// prompt user for guess
fmt.Printf("Guess: ")
guess, err := reader.ReadString('\n')
if err != nil {
log.Fatal().Err(err).Msg("error reading input")
}
guess = strings.ToLower(strings.TrimSuffix(guess, "\n"))

// send guess to game and print hints
hint := game.Guess(guess)
fmt.Println(hint + " - " + game.GetAlphabetHints())
// validate guess
if len(guess) != len(game.Word) {
fmt.Printf("incorrect length\n")
continue
} else if !words.Exists(guess) {
fmt.Printf("not a real word\n")
continue
}

// Check for end of game
if game.IsWin() {
fmt.Printf("You won! You used %d out of %d guesses!\n", game.Guesses, game.MaxGuesses)
break
} else if game.IsLose() {
fmt.Printf("You lost! The word was %v\n", game.Word)
// send guess to game and print hints
hint := game.Guess(guess)
fmt.Println(hint + " - " + game.GetAlphabetHints())

// Check for end of game
if game.IsWin() {
fmt.Printf("You won! You used %d out of %d guesses!\n", game.Guesses, game.MaxGuesses)
break
} else if game.IsLose() {
fmt.Printf("You lost! The word was %v\n", game.Word)
break
}
}

fmt.Printf("Would you like to play again? (y/N)\n")
response, err := reader.ReadString('\n')
if err != nil {
log.Fatal().Err(err).Msg("error reading input")
}
response = strings.ToLower(strings.TrimSpace(response))
if response == "y" || response == "yes" {
fmt.Println()
continue
} else {
break
}
}
Expand Down
5 changes: 2 additions & 3 deletions pkg/wordle/words.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,9 +59,8 @@ func ingestWordsFromFile(path string) ([]string, error) {
}

// RandomSolution chooses a random word from the solution list
func (w *Words) RandomSolution(seed int64) string {
i := rand.New(rand.NewSource(seed)).Intn(len(w.solutions))
return w.solutions[i]
func (w *Words) RandomSolution(random *rand.Rand) string {
return w.solutions[random.Intn(len(w.solutions))]
}

// Exists checks if a word exists in the list of words
Expand Down

0 comments on commit e57f647

Please sign in to comment.