-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathdumb.go
60 lines (50 loc) · 1.13 KB
/
dumb.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
package main
import (
"fmt"
"math/rand"
"os"
"strconv"
"strings"
"time"
"github.com/Nerdmaster/terminal"
)
func main() {
rand.Seed(time.Now().UTC().UnixNano())
var number = rand.Intn(10) + 1
oldState, err := terminal.MakeRaw(int(os.Stdin.Fd()))
if err != nil {
panic(err)
}
defer terminal.Restore(0, oldState)
var dt = terminal.Dumb(os.Stdin, os.Stdout)
fmt.Print("I'm thinking of a number from 1-10. Try to guess it!\r\n")
fmt.Print("(Type 'QUIT' at any time to exit)\r\n\r\n")
for {
fmt.Print("Guess a number: ")
guess, err := dt.ReadLine()
if strings.ToLower(guess) == "quit" {
fmt.Print("Quitter!\r\n")
break
}
if err != nil {
fmt.Print("Oh no, I got an error!\r\n")
fmt.Printf("%s\r\n", err)
break
}
var g int
g, err = strconv.Atoi(guess)
if err != nil {
fmt.Printf("Your entry, '%s', doesn't appear to be a valid number\r\n", guess)
continue
}
if g < 1 || g > 10 {
fmt.Print("Please enter a number in the range of 1 to 10, inclusive\r\n")
continue
}
if g == number {
fmt.Print("Correct!!\r\n")
break
}
fmt.Printf("%d is WRONG! Try again!\r\n", g)
}
}