From f93efc0986d6c55591e6c7acbfbbe777b2c7c5b0 Mon Sep 17 00:00:00 2001 From: raul Date: Thu, 22 Feb 2024 07:19:20 +0000 Subject: [PATCH] Fixed errors when entering empty input Submitting an empty guess would cause the game to print one of the two "Already guessed" lines, now it checks for empty input and ignores it accordingly. Also, I assigned the guess variable to an empty string at the end of the game() loop to reset it because I realized that inputting empty strings after a single guess would cause the same bug to re-appear because the variable hadn't changed from last attempt. --- main.go | 27 +++++++++++++++++++++++++-- 1 file changed, 25 insertions(+), 2 deletions(-) diff --git a/main.go b/main.go index ecec661..863a9d0 100644 --- a/main.go +++ b/main.go @@ -168,14 +168,34 @@ func alreadyCorrect(gs string) (isGuess bool) { return isAlreadyGuessed } +func isEmpty(guess string) (isEmpty bool) { + isEmpty = false + if guess == "" { + isEmpty = true + } + return isEmpty +} + func game() { for { gameStatus() checkLose() checkWin() - fmt.Printf("Guess: ") - fmt.Scanln(&guess) + checkEmpty: + for { + fmt.Printf("Guess: ") + fmt.Scanln(&guess) + + if isEmpty(guess) == false { + break checkEmpty + } else { + gameStatus() + } + + } + + // TODO: prevent players from inputting multiple characters, UTF-8 is gonna be a pain if getLetter(guess) == true { if alreadyCorrect(guess) == true { @@ -197,6 +217,9 @@ func game() { time.Sleep(1 * time.Second) } } + // Realized entering empty input would still cause "already guessed" errors + // because guess wasn't being reset + guess = "" } }