diff --git a/golangr/file-reader/file-reader b/golangr/file-reader/file-reader deleted file mode 100755 index 209b7c3..0000000 Binary files a/golangr/file-reader/file-reader and /dev/null differ diff --git a/golangr/file-reader/main.go b/golangr/file-reader/main.go index 78ac17e..eeee2f9 100644 --- a/golangr/file-reader/main.go +++ b/golangr/file-reader/main.go @@ -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() + // } } } diff --git a/golangr/file-reader/names.txt b/golangr/file-reader/names.txt deleted file mode 100644 index 532cbde..0000000 --- a/golangr/file-reader/names.txt +++ /dev/null @@ -1,4 +0,0 @@ -word1 -word2 -word3 -word4 diff --git a/golangr/file-reader/words.txt b/golangr/file-reader/words.txt new file mode 100644 index 0000000..b0bf0b3 --- /dev/null +++ b/golangr/file-reader/words.txt @@ -0,0 +1,6 @@ +fantastic +achieve +security +tool +cathedral +committee