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

84 lines
1.3 KiB
Go

package main
import (
"bufio"
"fmt"
"math/rand"
"os"
//"strconv"
)
type PlayerStats struct {
name string
lives uint8
}
var player PlayerStats
var names = []string{}
var err error
var guess string
var guessLetter rune
func scanLine() (line string) {
in := bufio.NewReader(os.Stdin)
line, err := in.ReadString('\n')
catchErr(err)
return line
}
func main() {
if len(os.Args) < 2 || len(os.Args) > 2 {
fmt.Println("Usage: ./file-reader names.txt")
os.Exit(1)
}
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.Println(player.name)
filePath := os.Args[1]
randWord := getWord(filePath)
fmt.Printf("The chosen name is %v\n", randWord)
// fmt.Scan(&guess)
fmt.Scanln(&guessLetter)
// if guess == randWord {
// fmt.Printf("You win!\n")
// } else {
// fmt.Printf("You lose!\n")
// }
}
func letterCheck(char rune) {
}
func catchErr(err error) {
if err != nil {
fmt.Println(err)
os.Exit(2)
}
}
func getWord(path string) (word string) {
file, err := os.Open(path)
if err != nil {
fmt.Println(err)
os.Exit(2)
}
defer file.Close()
scanner := bufio.NewScanner(file)
for scanner.Scan() {
names = append(names, scanner.Text())
}
randName := names[rand.Intn(len(names)-0)]
return randName
}