Added checking for repeating wrong characters

This commit is contained in:
raul 2024-02-15 06:50:36 +01:00
parent 729d871229
commit 8beacab86b
2 changed files with 20 additions and 6 deletions

2
go.mod
View File

@ -1,3 +1,3 @@
module file-reader
module hangman
go 1.22

24
main.go
View File

@ -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)
}
}
}
}