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
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!")