2024-02-04 09:45:14 +01:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2024-02-06 17:12:29 +01:00
|
|
|
"bufio"
|
2024-02-04 09:45:14 +01:00
|
|
|
"fmt"
|
2024-02-07 17:15:34 +01:00
|
|
|
"math/rand"
|
2024-02-04 09:45:14 +01:00
|
|
|
"os"
|
2024-02-10 13:44:41 +01:00
|
|
|
"strconv"
|
|
|
|
"strings"
|
2024-02-10 15:13:59 +01:00
|
|
|
"time"
|
2024-02-09 10:35:46 +01:00
|
|
|
//"strconv"
|
2024-02-04 09:45:14 +01:00
|
|
|
)
|
|
|
|
|
2024-02-09 21:28:57 +01:00
|
|
|
type PlayerStats struct {
|
|
|
|
name string
|
|
|
|
lives uint8
|
|
|
|
}
|
|
|
|
|
2024-02-10 15:13:59 +01:00
|
|
|
func clear() {
|
|
|
|
fmt.Print("\033[H\033[2J")
|
|
|
|
}
|
|
|
|
|
2024-02-09 21:28:57 +01:00
|
|
|
var player PlayerStats
|
2024-02-04 09:45:14 +01:00
|
|
|
var names = []string{}
|
|
|
|
var err error
|
2024-02-09 10:35:46 +01:00
|
|
|
var guess string
|
2024-02-09 21:28:57 +01:00
|
|
|
var guessLetter rune
|
|
|
|
|
|
|
|
func scanLine() (line string) {
|
|
|
|
in := bufio.NewReader(os.Stdin)
|
2024-02-10 13:44:41 +01:00
|
|
|
lineNew, err := in.ReadString('\n')
|
2024-02-09 21:28:57 +01:00
|
|
|
catchErr(err)
|
2024-02-10 13:44:41 +01:00
|
|
|
line = strings.Trim(lineNew, "\n")
|
2024-02-09 21:28:57 +01:00
|
|
|
return line
|
|
|
|
}
|
2024-02-04 09:45:14 +01:00
|
|
|
|
|
|
|
func main() {
|
|
|
|
if len(os.Args) < 2 || len(os.Args) > 2 {
|
|
|
|
fmt.Println("Usage: ./file-reader names.txt")
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
|
2024-02-10 13:44:41 +01:00
|
|
|
for {
|
2024-02-10 15:13:59 +01:00
|
|
|
clear()
|
|
|
|
|
|
|
|
fmt.Printf("Welcome to the hanged man game!\nMay I know your name?\nName: ")
|
|
|
|
// _, err = fmt.Scanln(&player.name)
|
|
|
|
// catchErr(err)
|
|
|
|
player.name = scanLine()
|
|
|
|
|
2024-02-10 13:44:41 +01:00
|
|
|
fmt.Printf("How many lives would you like to have?\nLives: ")
|
|
|
|
stringLives := scanLine()
|
|
|
|
//TODO: fix this garbage
|
|
|
|
intLives, err := strconv.Atoi(stringLives)
|
|
|
|
catchErr(err)
|
|
|
|
player.lives = uint8(intLives)
|
|
|
|
|
2024-02-10 15:13:59 +01:00
|
|
|
clear()
|
2024-02-10 13:44:41 +01:00
|
|
|
fmt.Printf("Name: %v\n", player.name)
|
|
|
|
fmt.Printf("Lives: %v\n", player.lives)
|
|
|
|
|
2024-02-10 15:13:59 +01:00
|
|
|
fmt.Printf("Is this correct? [y/n] ")
|
2024-02-10 13:44:41 +01:00
|
|
|
var choice string
|
|
|
|
_, err = fmt.Scanln(&choice)
|
|
|
|
catchErr(err)
|
|
|
|
if choice == "y" {
|
2024-02-10 15:13:59 +01:00
|
|
|
game()
|
2024-02-10 13:44:41 +01:00
|
|
|
}
|
|
|
|
}
|
2024-02-09 21:28:57 +01:00
|
|
|
|
2024-02-10 15:13:59 +01:00
|
|
|
// filePath := os.Args[1]
|
|
|
|
// randWord := getWord(filePath)
|
|
|
|
//
|
|
|
|
// fmt.Printf("The chosen name is %v\n", randWord)
|
|
|
|
// fmt.Scanln(&guessLetter)
|
2024-02-04 09:45:14 +01:00
|
|
|
|
2024-02-10 15:13:59 +01:00
|
|
|
}
|
2024-02-08 13:31:01 +01:00
|
|
|
|
2024-02-10 15:13:59 +01:00
|
|
|
func gameStatus() {
|
|
|
|
clear()
|
|
|
|
fmt.Println("Player:", player.name)
|
|
|
|
fmt.Println("Lives:", player.lives)
|
2024-02-09 21:28:57 +01:00
|
|
|
}
|
2024-02-09 10:35:46 +01:00
|
|
|
|
2024-02-10 15:13:59 +01:00
|
|
|
func game() {
|
|
|
|
clear()
|
|
|
|
for {
|
|
|
|
gameStatus()
|
|
|
|
if player.lives == 0 {
|
|
|
|
fmt.Println("You lose!")
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
time.Sleep(2 * time.Second)
|
|
|
|
player.lives--
|
|
|
|
}
|
2024-02-09 10:35:46 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func catchErr(err error) {
|
|
|
|
if err != nil {
|
|
|
|
fmt.Println(err)
|
|
|
|
os.Exit(2)
|
|
|
|
}
|
2024-02-08 13:31:01 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func getWord(path string) (word string) {
|
|
|
|
|
|
|
|
file, err := os.Open(path)
|
2024-02-04 09:45:14 +01:00
|
|
|
if err != nil {
|
2024-02-08 13:31:01 +01:00
|
|
|
fmt.Println(err)
|
2024-02-09 10:35:46 +01:00
|
|
|
os.Exit(2)
|
2024-02-04 09:45:14 +01:00
|
|
|
}
|
2024-02-06 17:12:29 +01:00
|
|
|
defer file.Close()
|
2024-02-04 09:45:14 +01:00
|
|
|
|
2024-02-06 17:12:29 +01:00
|
|
|
scanner := bufio.NewScanner(file)
|
2024-02-08 13:31:01 +01:00
|
|
|
|
2024-02-06 17:12:29 +01:00
|
|
|
for scanner.Scan() {
|
|
|
|
names = append(names, scanner.Text())
|
2024-02-04 09:45:14 +01:00
|
|
|
}
|
2024-02-06 17:12:29 +01:00
|
|
|
|
2024-02-08 13:31:01 +01:00
|
|
|
randName := names[rand.Intn(len(names)-0)]
|
|
|
|
return randName
|
2024-02-04 09:45:14 +01:00
|
|
|
}
|