-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcontrol.go
308 lines (265 loc) · 5.37 KB
/
control.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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
package main
import (
"fmt"
"strconv"
"github.com/jroimartin/gocui"
)
func isdie() bool {
return die
}
func toggdebug(g *gocui.Gui, v *gocui.View) error {
debug = !debug
showdebug(g, v, "Key:D toggdebug")
return nil
}
func showdebug(g *gocui.Gui, v *gocui.View, log string) error {
out, err := g.View("v2")
if err != nil {
return err
}
if debug {
out.Frame = true
out.Clear()
fmt.Fprintf(out, log)
printbomb(out)
} else {
out.Clear()
out.Frame = false
}
return nil
}
func upperView(g *gocui.Gui, v *gocui.View) error {
if isdie() {
return nil
}
nextIndex := active - 20
if nextIndex < 0 {
nextIndex = nextIndex + 260
}
name := blockArr[nextIndex]
showdebug(g, v, "Key:UP Action:Going from view "+v.Name()+" to "+name)
if _, err := setCurrentViewOnTop(g, name); err != nil {
return err
}
active = nextIndex
return nil
}
func downView(g *gocui.Gui, v *gocui.View) error {
if isdie() {
return nil
}
nextIndex := active + 20
if nextIndex > 260 {
nextIndex = nextIndex - 260
}
name := blockArr[nextIndex]
showdebug(g, v, "Key:Down Action:Going from view "+v.Name()+" to "+name)
if _, err := setCurrentViewOnTop(g, name); err != nil {
return err
}
active = nextIndex
return nil
}
func leftView(g *gocui.Gui, v *gocui.View) error {
if isdie() {
return nil
}
nextIndex := active - 1
if nextIndex%20 == 19 || nextIndex == -1 {
nextIndex = nextIndex + 20
}
name := blockArr[nextIndex]
showdebug(g, v, "Key:Left Action:Going from view "+v.Name()+" to "+name)
if _, err := setCurrentViewOnTop(g, name); err != nil {
return err
}
active = nextIndex
return nil
}
func rightView(g *gocui.Gui, v *gocui.View) error {
if isdie() {
return nil
}
nextIndex := active + 1
if nextIndex%20 == 0 {
nextIndex = nextIndex - 20
if nextIndex > 260 {
nextIndex = 1
}
}
name := blockArr[nextIndex]
showdebug(g, v, "Key:Right Action:Going from view "+v.Name()+" to "+name)
if _, err := setCurrentViewOnTop(g, name); err != nil {
return err
}
active = nextIndex
return nil
}
func toggleView(g *gocui.Gui, v *gocui.View) error {
//check dis
if isdie() {
return nil
}
//first step must save
if stepcount == 0 {
for boomArr[active] != 0 {
boomArr = bombinit()
}
}
if blockstatus[active] != 2 {
name := blockArr[active]
block, err := g.View(name)
if err != nil {
return err
}
block.Frame = false
block.Clear()
fmt.Fprintf(block, "%2v", strconv.Itoa(boomArr[active]))
switch boomArr[active] {
case -1:
if blockstatus[active] != 2 {
gameover(g)
}
case 0:
cleanblock(g, findempty(active, boomArr, []int{}))
default:
cleanblock(g, flagcheck(active, boomArr, blockstatus))
if isdie() {
gameover(g)
}
}
stepcount++
}
checkwin(g)
printstatus(g)
//debug
showdebug(g, v, "Key:Space Action:toggle view "+v.Name())
return nil
}
func printstatus(g *gocui.Gui) error {
bfine := 0
for i := 1; i < 260; i++ {
if blockstatus[i] == 2 {
bfine++
}
}
out, err := g.View("status")
if err != nil {
return err
}
out.Clear()
out.Title = "Game Status"
fmt.Fprintln(out, "Step:", stepcount)
fmt.Fprintln(out, "Bomb find:", bfine, "/40")
return nil
}
func cleanblock(g *gocui.Gui, blocklist []int) error {
for _, s := range blocklist {
block, err := g.View(blockArr[s])
if err != nil {
return err
}
block.Frame = false
block.Clear()
fmt.Fprintf(block, "%2v", strconv.Itoa(boomArr[s]))
blockstatus[s] = 1
}
return nil
}
func checkwin(g *gocui.Gui) error {
//display all boom
for i := 1; i < 260; i++ {
if boomArr[i] >= 0 && blockstatus[i] != 1 {
return nil
}
}
die = true
if _, err := g.SetViewOnTop("GameOver"); err != nil {
return err
}
out, err := g.View("GameOver")
if err != nil {
return err
}
out.Clear()
out.Frame = true
out.Title = "You Win"
fmt.Fprintf(out, "Good Job! \nPress R to Start New Game")
return nil
}
func gameover(g *gocui.Gui) error {
die = true
//display all boom
for i := 1; i < 260; i++ {
if boomArr[i] == -1 {
block, err := g.View(blockArr[i])
if err != nil {
return err
}
block.Frame = false
block.Clear()
fmt.Fprintf(block, "%2v", strconv.Itoa(boomArr[i]))
}
}
if _, err := g.SetViewOnTop("GameOver"); err != nil {
return err
}
out, err := g.View("GameOver")
if err != nil {
return err
}
out.Clear()
out.Frame = true
out.Title = "GameOver"
fmt.Fprintf(out, "Boom hits Game Over ! \nPress R to Restart")
return nil
}
func ResetView(g *gocui.Gui, v *gocui.View) error {
if _, err := g.SetViewOnBottom("GameOver"); err != nil {
return err
}
out, err := g.View("GameOver")
if err != nil {
return err
}
out.Frame = false
boomArr = bombinit()
die = false
for i := 1; i <= 260; i++ {
name := blockArr[i]
block, err := g.View(name)
if err != nil {
return err
}
block.Frame = true
block.Clear()
}
blockstatus = [260]int{}
stepcount = 0
printstatus(g)
return nil
}
func flagView(g *gocui.Gui, v *gocui.View) error {
if blockstatus[active] != 1 {
name := blockArr[active]
block, err := g.View(name)
if err != nil {
return err
}
block.Clear()
if blockstatus[active] == 0 {
blockstatus[active] = 2
fmt.Fprintf(block, "%2v", "F")
} else {
blockstatus[active] = 0
}
}
printstatus(g)
return nil
}
func printbomb(v *gocui.View) error {
for i := 20; i <= 260; i = i + 20 {
fmt.Fprintf(v, "\n%2v%1v", boomArr[i-20:i], blockstatus[i-20:i])
}
return nil
}