From 8beacab86b841d4762297bd21f1655d693d83ad4 Mon Sep 17 00:00:00 2001 From: raul Date: Thu, 15 Feb 2024 06:50:36 +0100 Subject: [PATCH] Added checking for repeating wrong characters --- go.mod | 2 +- main.go | 24 +++++++++++++++++++----- 2 files changed, 20 insertions(+), 6 deletions(-) diff --git a/go.mod b/go.mod index 7713c7f..e216892 100644 --- a/go.mod +++ b/go.mod @@ -1,3 +1,3 @@ -module file-reader +module hangman go 1.22 diff --git a/main.go b/main.go index 52c7fd3..1c0cfc5 100644 --- a/main.go +++ b/main.go @@ -21,6 +21,7 @@ var ( randWord string hiddenChars []string revealedChars []string + wrongChars []string ) type PlayerStats struct { @@ -45,8 +46,8 @@ func fillSecrets() { } func main() { - if len(os.Args) < 2 || len(os.Args) > 2 { - fmt.Println("Usage: ./file-reader words.txt") + if len(os.Args) != 2 { + fmt.Println("Usage: ./hangman words.txt") os.Exit(1) } filePath = os.Args[1] @@ -97,6 +98,7 @@ func gameStatus() { fmt.Println("Player:", player.name) fmt.Println("Lives:", player.lives) fmt.Println("Word:", hiddenChars) + fmt.Println("Wrong characters:", wrongChars) } func checkWin() { @@ -124,9 +126,21 @@ func game() { fmt.Scanln(&guess) if getLetter(guess) != true { - fmt.Printf("Wrong!") - player.lives-- - 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) + } } } }