Moved file-reader into its own repo
Accidentally built hangman while practicing opening and reading from text files. This is fun
This commit is contained in:
parent
27ce3d859e
commit
729d871229
|
@ -0,0 +1,169 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"fmt"
|
||||
"math/rand"
|
||||
"os"
|
||||
"reflect"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
)
|
||||
|
||||
var (
|
||||
player PlayerStats
|
||||
names []string
|
||||
err error
|
||||
guess string
|
||||
guessLetter rune
|
||||
filePath string
|
||||
randWord string
|
||||
hiddenChars []string
|
||||
revealedChars []string
|
||||
)
|
||||
|
||||
type PlayerStats struct {
|
||||
name string
|
||||
lives int8
|
||||
}
|
||||
|
||||
func fillSecrets() {
|
||||
// Filling the array that will contain the unrevealed characters
|
||||
for _, v := range randWord {
|
||||
if string(v) == " " {
|
||||
hiddenChars = append(hiddenChars, " ")
|
||||
} else {
|
||||
hiddenChars = append(hiddenChars, "_")
|
||||
}
|
||||
}
|
||||
|
||||
// Filling the array that will contain the revealed characters
|
||||
for _, v := range randWord {
|
||||
revealedChars = append(revealedChars, string(v))
|
||||
}
|
||||
}
|
||||
|
||||
func main() {
|
||||
if len(os.Args) < 2 || len(os.Args) > 2 {
|
||||
fmt.Println("Usage: ./file-reader words.txt")
|
||||
os.Exit(1)
|
||||
}
|
||||
filePath = os.Args[1]
|
||||
randWord = getWord(filePath)
|
||||
|
||||
fillSecrets()
|
||||
|
||||
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)
|
||||
catchErr(err)
|
||||
player.lives = int8(intLives)
|
||||
|
||||
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()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
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
|
||||
}
|
||||
|
||||
func gameStatus() {
|
||||
clear()
|
||||
fmt.Println("Player:", player.name)
|
||||
fmt.Println("Lives:", player.lives)
|
||||
fmt.Println("Word:", hiddenChars)
|
||||
}
|
||||
|
||||
func checkWin() {
|
||||
var winStatus bool = reflect.DeepEqual(revealedChars, hiddenChars)
|
||||
if winStatus == true {
|
||||
fmt.Println("You win!")
|
||||
os.Exit(0)
|
||||
}
|
||||
}
|
||||
|
||||
func checkLose() {
|
||||
if player.lives < 0 {
|
||||
fmt.Println("You lose!")
|
||||
os.Exit(0)
|
||||
}
|
||||
}
|
||||
|
||||
func game() {
|
||||
for {
|
||||
gameStatus()
|
||||
checkLose()
|
||||
checkWin()
|
||||
|
||||
fmt.Printf("Guess: ")
|
||||
fmt.Scanln(&guess)
|
||||
|
||||
if getLetter(guess) != true {
|
||||
fmt.Printf("Wrong!")
|
||||
player.lives--
|
||||
time.Sleep(1 * time.Second)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
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
|
||||
}
|
||||
|
||||
func clear() {
|
||||
fmt.Print("\033[H\033[2J")
|
||||
}
|
||||
|
||||
func scanLine() (line string) {
|
||||
in := bufio.NewReader(os.Stdin)
|
||||
lineNew, err := in.ReadString('\n')
|
||||
catchErr(err)
|
||||
line = strings.Trim(lineNew, "\n")
|
||||
return line
|
||||
}
|
Loading…
Reference in New Issue