From 894cd5c7c293f4186784a9dbd684ac67cc9ec9d8 Mon Sep 17 00:00:00 2001 From: raul Date: Fri, 16 Feb 2024 09:29:22 +0000 Subject: [PATCH] Cleaned up code for checking already guessed chars --- main.go | 66 +++++++++++++++++++++++++++++++++++++++++++++++++++------ 1 file changed, 59 insertions(+), 7 deletions(-) diff --git a/main.go b/main.go index d34ce42..17c2526 100644 --- a/main.go +++ b/main.go @@ -121,6 +121,40 @@ func checkLose() { } } +// func alreadyGuessed() (isGuess bool) { +// var isAlreadyGuessed bool = false +// for _, v := range wrongChars { +// if v == guess { +// isAlreadyGuessed = true +// } +// } +// for _, v := range revealedChars { +// if v == guess { +// isAlreadyGuessed = true +// } +// } +// return isAlreadyGuessed +// } +func alreadyWrong() (isGuess bool) { + var isAlreadyGuessed bool = false + for _, v := range wrongChars { + if v == guess { + isAlreadyGuessed = true + } + } + return isAlreadyGuessed +} + +func alreadyCorrect() (isGuess bool) { + var isAlreadyGuessed bool = false + for _, v := range revealedChars { + if v == guess { + isAlreadyGuessed = true + } + } + return isAlreadyGuessed +} + func game() { for { gameStatus() @@ -132,13 +166,7 @@ func game() { // TODO: Add checking for both wrong and correct characters if getLetter(guess) != true { - var isAlreadyWrong bool = false - for _, v := range wrongChars { - if v == guess { - isAlreadyWrong = true - } - } - if isAlreadyWrong == true { + if alreadyWrong() == true { fmt.Printf("You've already guessed this letter") time.Sleep(1 * time.Second) } else { @@ -148,6 +176,30 @@ func game() { time.Sleep(1 * time.Second) } } + + // if getLetter(guess) == true { + // if alreadyGuessed() == true { + // fmt.Printf("You've already guessed this letter") + // time.Sleep(1 * time.Second) + // } + // } + + // var isAlreadyWrong bool = false + // for _, v := range wrongChars { + // if v == guess { + // isAlreadyWrong = true + // } + // } + // if isAlreadyWrong == true { + // fmt.Printf("You've already guessed this letter") + // time.Sleep(1 * time.Second) + // } else { + // fmt.Printf("Wrong!") + // wrongChars = append(wrongChars, guess) + // player.lives-- + // time.Sleep(1 * time.Second) + // } + } }