diff --git a/go.mod b/go.mod index e216892..e47e888 100644 --- a/go.mod +++ b/go.mod @@ -1,3 +1,10 @@ module hangman go 1.22 + +require ( + github.com/inancgumus/screen v0.0.0-20190314163918-06e984b86ed3 // indirect + golang.org/x/crypto v0.19.0 // indirect + golang.org/x/sys v0.17.0 // indirect + golang.org/x/term v0.17.0 // indirect +) diff --git a/go.sum b/go.sum new file mode 100644 index 0000000..3c0efdb --- /dev/null +++ b/go.sum @@ -0,0 +1,8 @@ +github.com/inancgumus/screen v0.0.0-20190314163918-06e984b86ed3 h1:fO9A67/izFYFYky7l1pDP5Dr0BTCRkaQJUG6Jm5ehsk= +github.com/inancgumus/screen v0.0.0-20190314163918-06e984b86ed3/go.mod h1:Ey4uAp+LvIl+s5jRbOHLcZpUDnkjLBROl15fZLwPlTM= +golang.org/x/crypto v0.19.0 h1:ENy+Az/9Y1vSrlrvBSyna3PITt4tiZLf7sgCjZBX7Wo= +golang.org/x/crypto v0.19.0/go.mod h1:Iy9bg/ha4yyC70EfRS8jz+B6ybOBKMaSxLj6P6oBDfU= +golang.org/x/sys v0.17.0 h1:25cE3gD+tdBA7lp7QfhuV+rJiE9YXTcS3VG1SqssI/Y= +golang.org/x/sys v0.17.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= +golang.org/x/term v0.17.0 h1:mkTF7LCd6WGJNL3K1Ad7kwxNfYAW6a8a8QqtMblp/4U= +golang.org/x/term v0.17.0/go.mod h1:lLRBjIVuehSbZlaOtGMbcMncT+aqLLLmKrsjNrUguwk= diff --git a/main.go b/main.go index 715d9b8..105bee5 100644 --- a/main.go +++ b/main.go @@ -3,18 +3,16 @@ package main import ( "bufio" "fmt" + "github.com/inancgumus/screen" "math/rand" "os" "reflect" + "runtime" "strconv" "strings" "time" ) -// This is a branch testing comment - -// This is yet another comment for the sake of testing branches - var ( player PlayerStats names []string @@ -63,6 +61,8 @@ func main() { for { clear() + // TODO: Use type assertions to do error handling + 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: ") @@ -243,15 +243,44 @@ func getWord(path string) (word string) { return randName } -// TODO: Add OS agnostic clearing +// func setClear() { +// switch runtime.GOOS { +// case "linux": +// fmt.Println("You're using linux") +// time.Sleep(1 * time.Second) +// clearCommand = exec.Command("clear") +// clearCommand.Stdout = os.Stdout +// case "windows": +// fmt.Println("You're using windows") +// time.Sleep(1 * time.Second) +// clearCommand = exec.Command("cmd", "/c", "cls") +// clearCommand.Stdout = os.Stdout +// default: +// fmt.Println("Huh?") +// os.Exit(1) +// } +// } + func clear() { - fmt.Print("\033[H\033[2J") + screen.Clear() + screen.MoveTopLeft() + //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") + 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 }