73 lines
1.2 KiB
Go
73 lines
1.2 KiB
Go
package main
|
|
|
|
import (
|
|
"fmt"
|
|
"os"
|
|
"strconv"
|
|
"time"
|
|
)
|
|
|
|
var (
|
|
player PlayerStats
|
|
names []string
|
|
err error
|
|
guess string
|
|
guessLetter rune
|
|
filePath string
|
|
randWord string
|
|
hiddenChars []string
|
|
revealedChars []string
|
|
wrongChars []string
|
|
correctChars []string
|
|
)
|
|
|
|
type PlayerStats struct {
|
|
name string
|
|
lives int8
|
|
}
|
|
|
|
func main() {
|
|
if len(os.Args) != 2 {
|
|
fmt.Println("Usage: ./hangman words.txt")
|
|
os.Exit(1)
|
|
}
|
|
filePath = os.Args[1]
|
|
randWord = getWord(filePath)
|
|
|
|
fillSecrets()
|
|
|
|
for {
|
|
for {
|
|
clear()
|
|
fmt.Printf("Welcome to the hanged man game!\nMay I know your name?\nName: ")
|
|
player.name = scanLine()
|
|
|
|
fmt.Printf("How many lives would you like to have?\nLives: ")
|
|
stringLives := scanLine()
|
|
intLives, err := strconv.Atoi(stringLives)
|
|
|
|
if catchErr(err) != true {
|
|
player.lives = int8(intLives)
|
|
break
|
|
} else {
|
|
fmt.Println("Please, input a valid number")
|
|
time.Sleep(time.Second)
|
|
}
|
|
|
|
}
|
|
|
|
clear()
|
|
|
|
fmt.Printf("Name: %v\n", player.name)
|
|
fmt.Printf("Lives: %v\n", player.lives)
|
|
fmt.Printf("Is this correct? [y/n] ")
|
|
|
|
var choice string
|
|
_, err = fmt.Scanln(&choice)
|
|
catchErr(err)
|
|
if choice == "y" {
|
|
game()
|
|
}
|
|
}
|
|
}
|