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.
This commit is contained in:
raul 2024-02-22 07:19:20 +00:00
parent 5e1c4315fc
commit f93efc0986
1 changed files with 25 additions and 2 deletions

27
main.go
View File

@ -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 = ""
}
}