-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstate.go
95 lines (75 loc) · 1.35 KB
/
state.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
package main
import (
"sync/atomic"
"time"
)
const (
defaultMines = 40
defaultSecondsLeft = 180
defaultGridRows = 20
defaultGridCols = 15
)
var state State
func init() {
state.timerActive = new(uint32)
state.Rows = defaultGridRows
state.Cols = defaultGridCols
ResetGame()
go func() {
t := time.NewTicker(time.Second)
defer t.Stop()
for {
// Wait for next tick.
<-t.C
// Check, if timer should stop.
if atomic.LoadUint32(state.timerActive) == 0 {
continue
}
// Decrement our timer and invalidate the window to trigger a redraw.
window.Run(DecrementTimer)
window.Invalidate()
}
}()
}
type State struct {
Running bool
Finished bool
Won bool
Lost bool
NumMines uint16
SecondsLeft uint16
Rows uint8
Cols uint8
Cells []CellState
// Atomic
timerActive *uint32
}
func dispatch(a Action) {
reducer(a)
}
type ActionType int
const (
clickedCell ActionType = iota
resetGame
decrementTimer
)
type Action struct {
Type ActionType
Data interface{}
}
type clickedCellData struct {
idx int
rightClick bool
}
func ClickedCell(idx int, rightClick bool) {
dispatch(Action{
Type: clickedCell,
Data: clickedCellData{idx: idx, rightClick: rightClick},
})
}
func ResetGame() {
dispatch(Action{Type: resetGame})
}
func DecrementTimer() {
dispatch(Action{Type: decrementTimer})
}