Added checking for repeating wrong characters
This commit is contained in:
parent
729d871229
commit
8beacab86b
18
main.go
18
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,12 +126,24 @@ func game() {
|
|||
fmt.Scanln(&guess)
|
||||
|
||||
if getLetter(guess) != true {
|
||||
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)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func catchErr(err error) {
|
||||
if err != nil {
|
||||
|
|
Loading…
Reference in New Issue