diff --git a/golangr/file-reader/main.go b/golangr/file-reader/main.go index eeee2f9..6041958 100644 --- a/golangr/file-reader/main.go +++ b/golangr/file-reader/main.go @@ -5,6 +5,7 @@ import ( "fmt" "math/rand" "os" + "reflect" "strconv" "strings" "time" @@ -12,7 +13,7 @@ import ( var ( player PlayerStats - names = []string{} + names []string err error guess string guessLetter rune @@ -22,23 +23,11 @@ var ( revealedChars []string ) -func clear() { - fmt.Print("\033[H\033[2J") -} - 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 -} - func main() { if len(os.Args) < 2 || len(os.Args) > 2 { fmt.Println("Usage: ./file-reader words.txt") @@ -59,22 +48,20 @@ func main() { 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) 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) @@ -82,11 +69,6 @@ func main() { game() } } - - // - // fmt.Printf("The chosen name is %v\n", randWord) - // fmt.Scanln(&guessLetter) - } func getLetter(letter string) (isFound bool) { @@ -105,20 +87,29 @@ func gameStatus() { fmt.Println("Player:", player.name) fmt.Println("Lives:", player.lives) 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() { clear() for { gameStatus() - if player.lives < 0 { - fmt.Println("You lose!") - os.Exit(0) - } - // if hiddenChars == revealedChars { - // - // } + checkLose() + checkWin() fmt.Printf("Guess: ") fmt.Scanln(&guess) if getLetter(guess) != true { @@ -127,18 +118,6 @@ func game() { time.Sleep(1 * time.Second) 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) { - file, err := os.Open(path) if err != nil { fmt.Println(err) @@ -167,3 +145,15 @@ func getWord(path string) (word string) { 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 +}