wordle-cli

Golang implementation of wordle in CLI.
git clone http://git.hanabi.in/repos/wordle-cli.git
Log | Files | Refs | README | LICENSE

commit 06f22bd2c41032901c514dfb74509392456fefe3
parent 2d766426590b56f6ad51ccfc740de0cdd50078df
Author: Agastya Chandrakant <me@hanabi.in>
Date:   Mon, 21 Feb 2022 15:22:20 +0530

Convert special cases to general cases.

Diffstat:
M.gitignore | 2+-
Msrc/main.go | 4++--
Msrc/utils/gameplay-fns.go | 25+++++++++++++------------
3 files changed, 16 insertions(+), 15 deletions(-)

diff --git a/.gitignore b/.gitignore @@ -1,2 +1,2 @@ wordle -*/.DS_Store +**/*.DS_Store diff --git a/src/main.go b/src/main.go @@ -6,6 +6,6 @@ import ( func main() { answer := gameplay.SelectAnswer() - guesses := gameplay.StartGuessing(answer) - gameplay.GracefullyFinishGame(answer, guesses) + guesses, didWin := gameplay.StartGuessing(answer) + gameplay.GracefullyFinishGame(answer, guesses, didWin) } diff --git a/src/utils/gameplay-fns.go b/src/utils/gameplay-fns.go @@ -157,12 +157,14 @@ func shouldPrintShareEmojis() bool { return false } -func StartGuessing(answer string) []string { +// The guessing function, returns guess history and if the user won the game. +func StartGuessing(answer string) ([]string, bool) { var alphabet = initAlphabetTable() var anslookup = algos.GenAnsLookup(answer) var prev_guesses = []string{} var colouredChoices = []string{} + didWin := false fmt.Printf("Guess a %d-letter word. You have %d tries.\n", word_size, chances) for cur_chance := 1; cur_chance <= chances; { @@ -173,24 +175,23 @@ func StartGuessing(answer string) []string { } else { cur_chance++ prev_guesses = append(prev_guesses, guess) - if guess != answer { - colour_string := algos.GetColours(answer, guess, anslookup, alphabet) - colouredChoices = append(colouredChoices, colour_string) - printColouredGuess(colour_string, guess) - } else if guess == answer { - colour_string := "GGGGG" // If the answer was correct, GetColours is not called, hence hard-coding. - colouredChoices = append(colouredChoices, colour_string) - fmt.Println("Correct guess!") + colour_string := algos.GetColours(answer, guess, anslookup, alphabet) + colouredChoices = append(colouredChoices, colour_string) + printColouredGuess(colour_string, guess) + if guess == answer { + didWin = true break } } printColouredAlpha(alphabet) } - return colouredChoices + return colouredChoices, didWin } // Handle end of the game once correct answer is reached, or when all chances are over. -func GracefullyFinishGame(answer string, guesses []string) { - fmt.Printf("Answer was: %s.\n", answer) +func GracefullyFinishGame(answer string, guesses []string, didWin bool) { + if !didWin { + fmt.Printf("Answer was: %s.\n", answer) + } printShare(guesses) }