-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
76 lines (66 loc) · 1.47 KB
/
main.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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
package main
import (
"flag"
"fmt"
"log"
"time"
"github.com/eiannone/keyboard"
)
func main() {
difficultyLevel := getDifficultyLevel()
board := initializeBoard(difficultyLevel)
board.cells.carve(board.playerXPos, board.playerYPos)
clearConsole()
defaultInitialTime := func(difficulty Difficulty) int {
switch difficulty {
case EASY_LEVEL:
return 60
case MEDIUM_LEVEL:
return 90
case HARD_LEVEL:
return 120
default:
return 90
}
}(Difficulty(difficultyLevel))
initialTime := flag.Int("time", defaultInitialTime, "time limit to reach the gate in seconds")
flag.Parse()
fmt.Println(board)
if err := keyboard.Open(); err != nil {
log.Fatal("error: cannot open keyboard: ", err)
}
defer keyboard.Close()
runeReceiverChan := make(chan rune, 1)
keyReceiverChan := make(chan keyboard.Key, 1)
ticker := time.NewTicker(time.Second)
go func() {
for {
r, key, err := keyboard.GetSingleKey()
if err == nil {
runeReceiverChan <- r
keyReceiverChan <- key
}
}
}()
for {
select {
case <-ticker.C:
*initialTime--
if *initialTime == 0 {
fmt.Println("you ran out of time :(")
return
}
clearConsole()
fmt.Printf("%v\nremaining: %v seconds\n", board, *initialTime)
case r := <-runeReceiverChan:
key := <-keyReceiverChan
board.handleMove(r, key)
clearConsole()
fmt.Printf("%v\nremaining: %v seconds\n", board, *initialTime)
if board.checkWin() {
fmt.Println("you won the game!")
return
}
}
}
}