From 4a6b4d28c77522acf77fbc7faa0c677d1841886c Mon Sep 17 00:00:00 2001 From: raul Date: Sun, 18 Feb 2024 11:33:51 +0100 Subject: [PATCH] Added checking for already correct guesses --- main.go | 41 ++++++++++++++++++++++++++++------------- 1 file changed, 28 insertions(+), 13 deletions(-) diff --git a/main.go b/main.go index 17c2526..715d9b8 100644 --- a/main.go +++ b/main.go @@ -26,6 +26,7 @@ var ( hiddenChars []string revealedChars []string wrongChars []string + correctChars []string ) type PlayerStats struct { @@ -100,10 +101,11 @@ func getLetter(letter string) (isFound bool) { // TODO: Make gameStatus() prettier func gameStatus() { clear() - fmt.Println("Player:", player.name) - fmt.Println("Lives:", player.lives) - fmt.Println("Word:", hiddenChars) - fmt.Println("Wrong characters:", wrongChars) + fmt.Printf("Player: %v\n", player.name) + fmt.Printf("Lives: %v\n\n", player.lives) + fmt.Printf("Word: %v\n\n", hiddenChars) + fmt.Printf("Wrong characters: %v\n", wrongChars) + fmt.Printf("Correct characters: %v\n\n", correctChars) } func checkWin() { @@ -135,20 +137,25 @@ func checkLose() { // } // return isAlreadyGuessed // } -func alreadyWrong() (isGuess bool) { + +// Iterate over wrongChars and if the guess from the main game matches any of the elements, +// return True +func alreadyWrong(gs string) (isGuess bool) { var isAlreadyGuessed bool = false for _, v := range wrongChars { - if v == guess { + if v == gs { isAlreadyGuessed = true } } return isAlreadyGuessed } -func alreadyCorrect() (isGuess bool) { +// Iterate over correctChars and if the guess from the main game matches any of the elements, +// return True +func alreadyCorrect(gs string) (isGuess bool) { var isAlreadyGuessed bool = false - for _, v := range revealedChars { - if v == guess { + for _, v := range correctChars { + if v == gs { isAlreadyGuessed = true } } @@ -164,10 +171,18 @@ func game() { fmt.Printf("Guess: ") fmt.Scanln(&guess) - // TODO: Add checking for both wrong and correct characters - if getLetter(guess) != true { - if alreadyWrong() == true { - fmt.Printf("You've already guessed this letter") + if getLetter(guess) == true { + if alreadyCorrect(guess) == true { + fmt.Printf("[-] You've already guessed this correct letter") + time.Sleep(1 * time.Second) + } else { + correctChars = append(correctChars, guess) + } + } + + if getLetter(guess) == false { + if alreadyWrong(guess) == true { + fmt.Printf("[-] You've already guessed this wrong letter") time.Sleep(1 * time.Second) } else { fmt.Printf("Wrong!")