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:
parent
5e1c4315fc
commit
f93efc0986
23
main.go
23
main.go
|
@ -168,15 +168,35 @@ func alreadyCorrect(gs string) (isGuess bool) {
|
||||||
return isAlreadyGuessed
|
return isAlreadyGuessed
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func isEmpty(guess string) (isEmpty bool) {
|
||||||
|
isEmpty = false
|
||||||
|
if guess == "" {
|
||||||
|
isEmpty = true
|
||||||
|
}
|
||||||
|
return isEmpty
|
||||||
|
}
|
||||||
|
|
||||||
func game() {
|
func game() {
|
||||||
for {
|
for {
|
||||||
gameStatus()
|
gameStatus()
|
||||||
checkLose()
|
checkLose()
|
||||||
checkWin()
|
checkWin()
|
||||||
|
|
||||||
|
checkEmpty:
|
||||||
|
for {
|
||||||
fmt.Printf("Guess: ")
|
fmt.Printf("Guess: ")
|
||||||
fmt.Scanln(&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 getLetter(guess) == true {
|
||||||
if alreadyCorrect(guess) == true {
|
if alreadyCorrect(guess) == true {
|
||||||
fmt.Printf("[-] You've already guessed this correct letter")
|
fmt.Printf("[-] You've already guessed this correct letter")
|
||||||
|
@ -197,6 +217,9 @@ func game() {
|
||||||
time.Sleep(1 * time.Second)
|
time.Sleep(1 * time.Second)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
// Realized entering empty input would still cause "already guessed" errors
|
||||||
|
// because guess wasn't being reset
|
||||||
|
guess = ""
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue