2024-02-14 12:23:43 +01:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bufio"
|
|
|
|
"fmt"
|
2024-02-19 11:56:42 +01:00
|
|
|
"github.com/inancgumus/screen"
|
2024-02-14 12:23:43 +01:00
|
|
|
"math/rand"
|
|
|
|
"os"
|
|
|
|
"reflect"
|
2024-02-19 11:56:42 +01:00
|
|
|
"runtime"
|
2024-02-14 12:23:43 +01:00
|
|
|
"strconv"
|
|
|
|
"strings"
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
player PlayerStats
|
|
|
|
names []string
|
|
|
|
err error
|
|
|
|
guess string
|
|
|
|
guessLetter rune
|
|
|
|
filePath string
|
|
|
|
randWord string
|
|
|
|
hiddenChars []string
|
|
|
|
revealedChars []string
|
2024-02-15 06:50:36 +01:00
|
|
|
wrongChars []string
|
2024-02-18 11:33:51 +01:00
|
|
|
correctChars []string
|
2024-02-14 12:23:43 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
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() {
|
2024-02-15 06:50:36 +01:00
|
|
|
if len(os.Args) != 2 {
|
|
|
|
fmt.Println("Usage: ./hangman words.txt")
|
2024-02-14 12:23:43 +01:00
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
filePath = os.Args[1]
|
|
|
|
randWord = getWord(filePath)
|
|
|
|
|
|
|
|
fillSecrets()
|
|
|
|
|
|
|
|
for {
|
2024-02-20 09:26:01 +01:00
|
|
|
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)
|
|
|
|
if catchErr(err) != true {
|
|
|
|
player.lives = int8(intLives)
|
|
|
|
break
|
|
|
|
} else {
|
|
|
|
fmt.Println("Please, input a valid number")
|
|
|
|
time.Sleep(time.Second)
|
|
|
|
}
|
2024-02-14 12:23:43 +01:00
|
|
|
|
2024-02-20 09:26:01 +01:00
|
|
|
}
|
2024-02-14 12:23:43 +01:00
|
|
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2024-02-15 10:12:34 +01:00
|
|
|
// TODO: Make gameStatus() prettier
|
2024-02-14 12:23:43 +01:00
|
|
|
func gameStatus() {
|
|
|
|
clear()
|
2024-02-18 11:33:51 +01:00
|
|
|
fmt.Printf("Player: %v\n", player.name)
|
|
|
|
fmt.Printf("Lives: %v\n\n", player.lives)
|
|
|
|
fmt.Printf("Word: %v\n\n", hiddenChars)
|
|
|
|
fmt.Printf("Wrong characters: %v\n", wrongChars)
|
|
|
|
fmt.Printf("Correct characters: %v\n\n", correctChars)
|
2024-02-14 12:23:43 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
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!")
|
2024-02-20 09:26:01 +01:00
|
|
|
fmt.Printf("The word was %v!\n", revealedChars)
|
2024-02-14 12:23:43 +01:00
|
|
|
os.Exit(0)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-02-16 10:29:22 +01:00
|
|
|
// func alreadyGuessed() (isGuess bool) {
|
|
|
|
// var isAlreadyGuessed bool = false
|
|
|
|
// for _, v := range wrongChars {
|
|
|
|
// if v == guess {
|
|
|
|
// isAlreadyGuessed = true
|
|
|
|
// }
|
|
|
|
// }
|
|
|
|
// for _, v := range revealedChars {
|
|
|
|
// if v == guess {
|
|
|
|
// isAlreadyGuessed = true
|
|
|
|
// }
|
|
|
|
// }
|
|
|
|
// return isAlreadyGuessed
|
|
|
|
// }
|
2024-02-18 11:33:51 +01:00
|
|
|
|
|
|
|
// Iterate over wrongChars and if the guess from the main game matches any of the elements,
|
|
|
|
// return True
|
|
|
|
func alreadyWrong(gs string) (isGuess bool) {
|
2024-02-16 10:29:22 +01:00
|
|
|
var isAlreadyGuessed bool = false
|
|
|
|
for _, v := range wrongChars {
|
2024-02-18 11:33:51 +01:00
|
|
|
if v == gs {
|
2024-02-16 10:29:22 +01:00
|
|
|
isAlreadyGuessed = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return isAlreadyGuessed
|
|
|
|
}
|
|
|
|
|
2024-02-18 11:33:51 +01:00
|
|
|
// Iterate over correctChars and if the guess from the main game matches any of the elements,
|
|
|
|
// return True
|
|
|
|
func alreadyCorrect(gs string) (isGuess bool) {
|
2024-02-16 10:29:22 +01:00
|
|
|
var isAlreadyGuessed bool = false
|
2024-02-18 11:33:51 +01:00
|
|
|
for _, v := range correctChars {
|
|
|
|
if v == gs {
|
2024-02-16 10:29:22 +01:00
|
|
|
isAlreadyGuessed = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return isAlreadyGuessed
|
|
|
|
}
|
|
|
|
|
2024-02-22 08:19:20 +01:00
|
|
|
func isEmpty(guess string) (isEmpty bool) {
|
|
|
|
isEmpty = false
|
|
|
|
if guess == "" {
|
|
|
|
isEmpty = true
|
|
|
|
}
|
|
|
|
return isEmpty
|
|
|
|
}
|
|
|
|
|
2024-02-14 12:23:43 +01:00
|
|
|
func game() {
|
|
|
|
for {
|
|
|
|
gameStatus()
|
|
|
|
checkLose()
|
|
|
|
checkWin()
|
|
|
|
|
2024-02-22 08:19:20 +01:00
|
|
|
checkEmpty:
|
|
|
|
for {
|
|
|
|
fmt.Printf("Guess: ")
|
2024-02-23 08:37:28 +01:00
|
|
|
//fmt.Scanln(&guess)
|
|
|
|
guess = scanLine()
|
2024-02-22 08:19:20 +01:00
|
|
|
|
2024-02-23 08:37:28 +01:00
|
|
|
if isEmpty(guess) == false && len([]rune(guess)) == 1 {
|
2024-02-22 08:19:20 +01:00
|
|
|
break checkEmpty
|
|
|
|
} else {
|
|
|
|
gameStatus()
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2024-02-18 11:33:51 +01:00
|
|
|
if getLetter(guess) == true {
|
|
|
|
if alreadyCorrect(guess) == true {
|
|
|
|
fmt.Printf("[-] You've already guessed this correct letter")
|
|
|
|
time.Sleep(1 * time.Second)
|
|
|
|
} else {
|
|
|
|
correctChars = append(correctChars, guess)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if getLetter(guess) == false {
|
|
|
|
if alreadyWrong(guess) == true {
|
|
|
|
fmt.Printf("[-] You've already guessed this wrong letter")
|
2024-02-15 06:50:36 +01:00
|
|
|
time.Sleep(1 * time.Second)
|
|
|
|
} else {
|
|
|
|
fmt.Printf("Wrong!")
|
|
|
|
wrongChars = append(wrongChars, guess)
|
|
|
|
player.lives--
|
|
|
|
time.Sleep(1 * time.Second)
|
|
|
|
}
|
2024-02-14 12:23:43 +01:00
|
|
|
}
|
2024-02-22 08:19:20 +01:00
|
|
|
// Realized entering empty input would still cause "already guessed" errors
|
|
|
|
// because guess wasn't being reset
|
|
|
|
guess = ""
|
2024-02-14 12:23:43 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-02-20 09:26:01 +01:00
|
|
|
func catchErr(err error) (errHappened bool) {
|
|
|
|
errHappened = false
|
2024-02-14 12:23:43 +01:00
|
|
|
if err != nil {
|
2024-02-20 09:26:01 +01:00
|
|
|
//fmt.Println(err)
|
|
|
|
errHappened = true
|
2024-02-14 12:23:43 +01:00
|
|
|
}
|
2024-02-20 09:26:01 +01:00
|
|
|
return errHappened
|
2024-02-14 12:23:43 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
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() {
|
2024-02-19 11:56:42 +01:00
|
|
|
screen.Clear()
|
|
|
|
screen.MoveTopLeft()
|
|
|
|
//fmt.Print("\033[H\033[2J")
|
2024-02-14 12:23:43 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func scanLine() (line string) {
|
2024-02-19 11:56:42 +01:00
|
|
|
switch runtime.GOOS {
|
|
|
|
case "linux":
|
|
|
|
in := bufio.NewReader(os.Stdin)
|
|
|
|
lineNew, err := in.ReadString('\n')
|
|
|
|
catchErr(err)
|
|
|
|
line = strings.Trim(lineNew, "\n")
|
|
|
|
|
|
|
|
// I hate Windows
|
|
|
|
case "windows":
|
|
|
|
in := bufio.NewReader(os.Stdin)
|
|
|
|
lineNew, err := in.ReadString('\r')
|
|
|
|
catchErr(err)
|
|
|
|
line = strings.Trim(lineNew, "\r")
|
|
|
|
}
|
2024-02-14 12:23:43 +01:00
|
|
|
return line
|
|
|
|
}
|