Added guessing individual word letters

This commit is contained in:
raul 2024-02-13 06:56:52 +01:00
parent ca05ff2eba
commit f29f803ddb
4 changed files with 54 additions and 21 deletions

Binary file not shown.

View File

@ -10,23 +10,26 @@ import (
"time"
)
type PlayerStats struct {
name string
lives int8
}
var (
player PlayerStats
names = []string{}
err error
guess string
guessLetter rune
filePath string
randWord string
hiddenChars []string
revealedChars []string
)
func clear() {
fmt.Print("\033[H\033[2J")
}
var player PlayerStats
var names = []string{}
var err error
var guess string
var guessLetter rune
var filePath string
var randWord string
var hiddenChars []string
type PlayerStats struct {
name string
lives int8
}
func scanLine() (line string) {
in := bufio.NewReader(os.Stdin)
@ -38,15 +41,19 @@ func scanLine() (line string) {
func main() {
if len(os.Args) < 2 || len(os.Args) > 2 {
fmt.Println("Usage: ./file-reader names.txt")
fmt.Println("Usage: ./file-reader words.txt")
os.Exit(1)
}
filePath = os.Args[1]
randWord = getWord(filePath)
for range randWord {
// TODO: Detect spaces in string and don't set them to "_"
hiddenChars = append(hiddenChars, "_")
}
for _, v := range randWord {
revealedChars = append(revealedChars, string(v))
}
for {
clear()
@ -82,11 +89,23 @@ func main() {
}
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)
//fmt.Println(revealedChars)
}
func game() {
@ -97,17 +116,29 @@ func game() {
fmt.Println("You lose!")
os.Exit(0)
}
// if hiddenChars == revealedChars {
//
// }
fmt.Printf("Guess: ")
fmt.Scanln(&guess)
if guess == randWord {
fmt.Println("You win!")
os.Exit(0)
} else {
if getLetter(guess) != true {
fmt.Printf("Wrong!")
player.lives--
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()
// }
}
}

View File

@ -1,4 +0,0 @@
word1
word2
word3
word4

View File

@ -0,0 +1,6 @@
fantastic
achieve
security
tool
cathedral
committee