golang-exercises/golangr/file-reader/main.go

133 lines
2.2 KiB
Go
Raw Normal View History

2024-02-04 09:45:14 +01:00
package main
import (
"bufio"
2024-02-04 09:45:14 +01:00
"fmt"
"math/rand"
2024-02-04 09:45:14 +01:00
"os"
"strconv"
"strings"
2024-02-10 15:13:59 +01:00
"time"
2024-02-10 22:20:19 +01:00
//"time"
2024-02-09 10:35:46 +01:00
//"strconv"
2024-02-04 09:45:14 +01:00
)
type PlayerStats struct {
name string
2024-02-11 16:30:42 +01:00
lives int8
}
2024-02-10 15:13:59 +01:00
func clear() {
fmt.Print("\033[H\033[2J")
}
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
var guessLetter rune
func scanLine() (line string) {
in := bufio.NewReader(os.Stdin)
lineNew, err := in.ReadString('\n')
catchErr(err)
line = strings.Trim(lineNew, "\n")
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)
}
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()
fmt.Printf("How many lives would you like to have?\nLives: ")
stringLives := scanLine()
//TODO: fix this garbage
intLives, err := strconv.Atoi(stringLives)
catchErr(err)
2024-02-11 16:30:42 +01:00
player.lives = int8(intLives)
2024-02-10 15:13:59 +01:00
clear()
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] ")
var choice string
_, err = fmt.Scanln(&choice)
catchErr(err)
if choice == "y" {
2024-02-10 15:13:59 +01:00
game()
}
}
2024-02-10 15:13:59 +01:00
//
// 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 10:35:46 +01:00
2024-02-10 15:13:59 +01:00
func game() {
clear()
2024-02-10 22:20:19 +01:00
filePath := os.Args[1]
randWord := getWord(filePath)
2024-02-10 15:13:59 +01:00
for {
gameStatus()
2024-02-11 16:30:42 +01:00
if player.lives < 0 {
2024-02-10 15:13:59 +01:00
fmt.Println("You lose!")
2024-02-10 22:20:19 +01:00
os.Exit(0)
}
fmt.Printf("Guess: ")
fmt.Scanln(&guess)
if guess == randWord {
fmt.Println("You win!")
os.Exit(0)
} else {
2024-02-11 16:30:42 +01:00
fmt.Printf("Wrong!")
2024-02-10 22:20:19 +01:00
player.lives--
2024-02-11 16:30:42 +01:00
time.Sleep(1 * time.Second)
2024-02-10 22:20:19 +01:00
clear()
2024-02-10 15:13:59 +01:00
}
}
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
}
defer file.Close()
2024-02-04 09:45:14 +01:00
scanner := bufio.NewScanner(file)
2024-02-08 13:31:01 +01:00
for scanner.Scan() {
names = append(names, scanner.Text())
2024-02-04 09:45:14 +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
}