Added win condition checking

You can actually win the game now
This commit is contained in:
raul 2024-02-13 07:32:07 +00:00
parent f29f803ddb
commit 30d42a3219
1 changed files with 34 additions and 44 deletions

View File

@ -5,6 +5,7 @@ import (
"fmt" "fmt"
"math/rand" "math/rand"
"os" "os"
"reflect"
"strconv" "strconv"
"strings" "strings"
"time" "time"
@ -12,7 +13,7 @@ import (
var ( var (
player PlayerStats player PlayerStats
names = []string{} names []string
err error err error
guess string guess string
guessLetter rune guessLetter rune
@ -22,23 +23,11 @@ var (
revealedChars []string revealedChars []string
) )
func clear() {
fmt.Print("\033[H\033[2J")
}
type PlayerStats struct { type PlayerStats struct {
name string name string
lives int8 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
}
func main() { func main() {
if len(os.Args) < 2 || len(os.Args) > 2 { if len(os.Args) < 2 || len(os.Args) > 2 {
fmt.Println("Usage: ./file-reader words.txt") fmt.Println("Usage: ./file-reader words.txt")
@ -59,22 +48,20 @@ func main() {
clear() clear()
fmt.Printf("Welcome to the hanged man game!\nMay I know your name?\nName: ") fmt.Printf("Welcome to the hanged man game!\nMay I know your name?\nName: ")
// _, err = fmt.Scanln(&player.name)
// catchErr(err)
player.name = scanLine() player.name = scanLine()
fmt.Printf("How many lives would you like to have?\nLives: ") fmt.Printf("How many lives would you like to have?\nLives: ")
stringLives := scanLine() stringLives := scanLine()
//TODO: fix this garbage
intLives, err := strconv.Atoi(stringLives) intLives, err := strconv.Atoi(stringLives)
catchErr(err) catchErr(err)
player.lives = int8(intLives) player.lives = int8(intLives)
clear() clear()
fmt.Printf("Name: %v\n", player.name) fmt.Printf("Name: %v\n", player.name)
fmt.Printf("Lives: %v\n", player.lives) fmt.Printf("Lives: %v\n", player.lives)
fmt.Printf("Is this correct? [y/n] ") fmt.Printf("Is this correct? [y/n] ")
var choice string var choice string
_, err = fmt.Scanln(&choice) _, err = fmt.Scanln(&choice)
catchErr(err) catchErr(err)
@ -82,11 +69,6 @@ func main() {
game() game()
} }
} }
//
// fmt.Printf("The chosen name is %v\n", randWord)
// fmt.Scanln(&guessLetter)
} }
func getLetter(letter string) (isFound bool) { func getLetter(letter string) (isFound bool) {
@ -105,20 +87,29 @@ func gameStatus() {
fmt.Println("Player:", player.name) fmt.Println("Player:", player.name)
fmt.Println("Lives:", player.lives) fmt.Println("Lives:", player.lives)
fmt.Println("Word:", hiddenChars) fmt.Println("Word:", hiddenChars)
//fmt.Println(revealedChars) }
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() { func game() {
clear() clear()
for { for {
gameStatus() gameStatus()
if player.lives < 0 { checkLose()
fmt.Println("You lose!") checkWin()
os.Exit(0)
}
// if hiddenChars == revealedChars {
//
// }
fmt.Printf("Guess: ") fmt.Printf("Guess: ")
fmt.Scanln(&guess) fmt.Scanln(&guess)
if getLetter(guess) != true { if getLetter(guess) != true {
@ -127,18 +118,6 @@ func game() {
time.Sleep(1 * time.Second) time.Sleep(1 * time.Second)
clear() clear()
} }
// 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()
// }
} }
} }
@ -150,7 +129,6 @@ func catchErr(err error) {
} }
func getWord(path string) (word string) { func getWord(path string) (word string) {
file, err := os.Open(path) file, err := os.Open(path)
if err != nil { if err != nil {
fmt.Println(err) fmt.Println(err)
@ -167,3 +145,15 @@ func getWord(path string) (word string) {
randName := names[rand.Intn(len(names)-0)] randName := names[rand.Intn(len(names)-0)]
return randName 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
}