hangman/tools.go

153 lines
3.2 KiB
Go

package main
import (
"bufio"
"fmt"
"github.com/inancgumus/screen"
"math/rand"
"os"
"reflect"
"runtime"
"strings"
)
//////////////////////////////////////////////////////////////////////
func clear() {
screen.Clear()
screen.MoveTopLeft()
//fmt.Print("\033[H\033[2J")
}
//////////////////////////////////////////////////////////////////////
func scanLine() (line string) {
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")
}
return line
}
//////////////////////////////////////////////////////////////////////
// Section for checking whether the player has lost or won
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!")
fmt.Printf("The word was %v!\n", revealedChars)
os.Exit(0)
}
}
//////////////////////////////////////////////////////////////////////
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 alreadyWrong(gs string) (isGuess bool) {
var isAlreadyGuessed bool = false
for _, v := range wrongChars {
if v == gs {
isAlreadyGuessed = true
}
}
return isAlreadyGuessed
}
// Iterate over correctChars and if the guess from the main game matches any of the elements,
// return True
func alreadyCorrect(gs string) (isGuess bool) {
var isAlreadyGuessed bool = false
for _, v := range correctChars {
if v == gs {
isAlreadyGuessed = true
}
}
return isAlreadyGuessed
}
//////////////////////////////////////////////////////////////////////
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 catchErr(err error) (errHappened bool) {
errHappened = false
if err != nil {
//fmt.Println(err)
errHappened = true
}
return errHappened
}
//////////////////////////////////////////////////////////////////////
// TODO: Make gameStatus() prettier
func gameStatus() {
clear()
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)
}
func isEmpty(guess string) (isEmpty bool) {
isEmpty = false
if guess == "" {
isEmpty = true
}
return isEmpty
}