-
Notifications
You must be signed in to change notification settings - Fork 0
/
gomaze.go
197 lines (168 loc) · 3.42 KB
/
gomaze.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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
package main
import (
"errors"
"fmt"
"log"
"os"
"time"
"github.com/gdamore/tcell"
"github.com/urfave/cli"
)
func initScreen() (tcell.Screen, error) {
tcell.SetEncodingFallback(tcell.EncodingFallbackASCII)
s, err := tcell.NewScreen()
if err != nil {
return nil, err
}
if err = s.Init(); err != nil {
return nil, err
}
return s, nil
}
func startGame(width, height int, seed bool, format bool) (Result, int, int, error) {
s, err := initScreen()
if err != nil {
return STOPPED, 0, 0, err
}
defer s.Fini()
w, h := s.Size()
m := NewMaze(w/2, h, seed, format, false)
ticker := time.NewTicker(time.Millisecond)
defer ticker.Stop()
game := Game{
screen: s,
maze: m,
ticker: ticker,
}
res, err := game.Loop()
return res, w / 2, h, err
}
func startSearch(width, height int, seed bool, format bool, bfs, dfs bool) (Result, error) {
s, err := initScreen()
if err != nil {
return STOPPED, err
}
defer s.Fini()
w, h := s.Size()
m := NewMaze(w/2, h, seed, format, true)
ticker := time.NewTicker(10 * time.Millisecond)
defer ticker.Stop()
game := Game{
screen: s,
maze: m,
bfs: bfs,
dfs: dfs,
ticker: ticker,
}
if bfs {
game.queue = make([]*Point, 0)
} else if dfs {
game.stack = make([]*Point, 0)
}
res, err := game.Loop()
if res == GOALED {
time.Sleep(5 * time.Second) // TODO: not use time.Sleep, use input routine
}
return res, err
}
func main() {
app := cli.NewApp()
app.Name = "gomaze"
app.Usage = "Generate maze"
app.Version = "0.1.0"
app.Flags = []cli.Flag{
&cli.IntFlag{
Name: "height",
Usage: "Set the height of maze",
Value: 30,
},
&cli.IntFlag{
Name: "width",
Usage: "Set the width of maze",
Value: 30,
},
&cli.BoolFlag{
Name: "seed",
Usage: "Set seed for generating specific maze",
},
&cli.BoolFlag{
Name: "screen",
Usage: "TUI mode",
},
&cli.BoolFlag{
Name: "format",
Usage: "Format output bold",
},
&cli.BoolFlag{
Name: "bfs",
Usage: "Show breadth first search",
},
&cli.BoolFlag{
Name: "dfs",
Usage: "Show depth first search",
},
&cli.BoolFlag{
Name: "debug",
},
}
app.Before = func(c *cli.Context) error {
if c.Bool("debug") {
log.SetOutput(os.Stderr)
} else {
file, err := os.Open(os.DevNull)
if err != nil {
return err
}
log.SetOutput(file)
}
return nil
}
app.Action = func(c *cli.Context) error {
tw := c.Int("width")
th := c.Int("height")
se := c.Bool("seed")
sc := c.Bool("screen")
wi := c.Bool("format")
bfs := c.Bool("bfs")
dfs := c.Bool("dfs")
if sc {
start := time.Now()
res, w, h, err := startGame(tw, th, se, wi)
if err != nil {
return err
}
end := time.Now()
if res == GOALED {
fmt.Println("Congrats!")
fmt.Printf("[Maze size] Width: %d / Height: %d\n", w, h)
fmt.Printf("[Your time] %s\n", end.Sub(start))
}
return nil
} else if bfs || dfs {
if bfs && dfs {
return errors.New("only choose one whether bfs or dfs")
}
start := time.Now()
res, err := startSearch(tw, th, se, wi, bfs, dfs)
if err != nil {
return err
}
end := time.Now()
if res == GOALED {
fmt.Println("Search complete!")
fmt.Printf("[Search time] %s\n", end.Sub(start))
}
return nil
} else {
m := NewMaze(tw, th, se, wi, false)
m.printMaze()
return nil
}
}
if err := app.Run(os.Args); err != nil {
fmt.Println(err)
}
}
func wait() {
time.Sleep(time.Second * 2)
}