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
|
randWord string
|
||||||
hiddenChars []string
|
hiddenChars []string
|
||||||
revealedChars []string
|
revealedChars []string
|
||||||
|
wrongChars []string
|
||||||
)
|
)
|
||||||
|
|
||||||
type PlayerStats struct {
|
type PlayerStats struct {
|
||||||
|
@ -45,8 +46,8 @@ func fillSecrets() {
|
||||||
}
|
}
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
if len(os.Args) < 2 || len(os.Args) > 2 {
|
if len(os.Args) != 2 {
|
||||||
fmt.Println("Usage: ./file-reader words.txt")
|
fmt.Println("Usage: ./hangman words.txt")
|
||||||
os.Exit(1)
|
os.Exit(1)
|
||||||
}
|
}
|
||||||
filePath = os.Args[1]
|
filePath = os.Args[1]
|
||||||
|
@ -97,6 +98,7 @@ func gameStatus() {
|
||||||
fmt.Println("Player:", player.name)
|
fmt.Println("Player:", player.name)
|
||||||
fmt.Println("Lives:", player.lives)
|
fmt.Println("Lives:", player.lives)
|
||||||
fmt.Println("Word:", hiddenChars)
|
fmt.Println("Word:", hiddenChars)
|
||||||
|
fmt.Println("Wrong characters:", wrongChars)
|
||||||
}
|
}
|
||||||
|
|
||||||
func checkWin() {
|
func checkWin() {
|
||||||
|
@ -124,11 +126,23 @@ func game() {
|
||||||
fmt.Scanln(&guess)
|
fmt.Scanln(&guess)
|
||||||
|
|
||||||
if getLetter(guess) != true {
|
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!")
|
fmt.Printf("Wrong!")
|
||||||
|
wrongChars = append(wrongChars, guess)
|
||||||
player.lives--
|
player.lives--
|
||||||
time.Sleep(1 * time.Second)
|
time.Sleep(1 * time.Second)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func catchErr(err error) {
|
func catchErr(err error) {
|
||||||
|
|
Loading…
Reference in New Issue