Added checking for already correct guesses

This commit is contained in:
raul 2024-02-18 11:33:51 +01:00
parent 894cd5c7c2
commit 4a6b4d28c7
1 changed files with 28 additions and 13 deletions

41
main.go
View File

@ -26,6 +26,7 @@ var (
hiddenChars []string hiddenChars []string
revealedChars []string revealedChars []string
wrongChars []string wrongChars []string
correctChars []string
) )
type PlayerStats struct { type PlayerStats struct {
@ -100,10 +101,11 @@ func getLetter(letter string) (isFound bool) {
// TODO: Make gameStatus() prettier // TODO: Make gameStatus() prettier
func gameStatus() { func gameStatus() {
clear() clear()
fmt.Println("Player:", player.name) fmt.Printf("Player: %v\n", player.name)
fmt.Println("Lives:", player.lives) fmt.Printf("Lives: %v\n\n", player.lives)
fmt.Println("Word:", hiddenChars) fmt.Printf("Word: %v\n\n", hiddenChars)
fmt.Println("Wrong characters:", wrongChars) fmt.Printf("Wrong characters: %v\n", wrongChars)
fmt.Printf("Correct characters: %v\n\n", correctChars)
} }
func checkWin() { func checkWin() {
@ -135,20 +137,25 @@ func checkLose() {
// } // }
// return isAlreadyGuessed // 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 var isAlreadyGuessed bool = false
for _, v := range wrongChars { for _, v := range wrongChars {
if v == guess { if v == gs {
isAlreadyGuessed = true isAlreadyGuessed = true
} }
} }
return isAlreadyGuessed 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 var isAlreadyGuessed bool = false
for _, v := range revealedChars { for _, v := range correctChars {
if v == guess { if v == gs {
isAlreadyGuessed = true isAlreadyGuessed = true
} }
} }
@ -164,10 +171,18 @@ func game() {
fmt.Printf("Guess: ") fmt.Printf("Guess: ")
fmt.Scanln(&guess) fmt.Scanln(&guess)
// TODO: Add checking for both wrong and correct characters if getLetter(guess) == true {
if getLetter(guess) != true { if alreadyCorrect(guess) == true {
if alreadyWrong() == true { fmt.Printf("[-] You've already guessed this correct letter")
fmt.Printf("You've already guessed this 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) time.Sleep(1 * time.Second)
} else { } else {
fmt.Printf("Wrong!") fmt.Printf("Wrong!")