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

170 lines
3.0 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-04 09:45:14 +01:00
)
2024-02-13 06:56:52 +01:00
var (
player PlayerStats
names = []string{}
err error
guess string
guessLetter rune
filePath string
randWord string
hiddenChars []string
revealedChars []string
)
2024-02-10 15:13:59 +01:00
func clear() {
fmt.Print("\033[H\033[2J")
}
2024-02-13 06:56:52 +01:00
type PlayerStats struct {
name string
lives int8
}
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 {
2024-02-13 06:56:52 +01:00
fmt.Println("Usage: ./file-reader words.txt")
2024-02-04 09:45:14 +01:00
os.Exit(1)
}
2024-02-12 13:04:28 +01:00
filePath = os.Args[1]
randWord = getWord(filePath)
2024-02-04 09:45:14 +01:00
for range randWord {
2024-02-13 06:56:52 +01:00
// TODO: Detect spaces in string and don't set them to "_"
hiddenChars = append(hiddenChars, "_")
}
2024-02-13 06:56:52 +01:00
for _, v := range randWord {
revealedChars = append(revealedChars, string(v))
}
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-13 06:56:52 +01:00
func getLetter(letter string) (isFound bool) {
isFound = false
for i, v := range randWord {
if letter == string(v) {
isFound = true
hiddenChars[i] = string(v)
}
}
return isFound
}
2024-02-10 15:13:59 +01:00
func gameStatus() {
clear()
fmt.Println("Player:", player.name)
fmt.Println("Lives:", player.lives)
fmt.Println("Word:", hiddenChars)
2024-02-13 06:56:52 +01:00
//fmt.Println(revealedChars)
}
2024-02-09 10:35:46 +01:00
2024-02-10 15:13:59 +01:00
func game() {
clear()
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)
}
2024-02-13 06:56:52 +01:00
// if hiddenChars == revealedChars {
//
// }
2024-02-10 22:20:19 +01:00
fmt.Printf("Guess: ")
fmt.Scanln(&guess)
2024-02-13 06:56:52 +01:00
if getLetter(guess) != true {
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-13 06:56:52 +01:00
// Replacing guessing the whole word with guessing a single letter
// if guess == randWord {
// fmt.Println("You win!")
// os.Exit(0)
// } else {
// fmt.Printf("Wrong!")
// player.lives--
// time.Sleep(1 * time.Second)
// 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
}