-
Notifications
You must be signed in to change notification settings - Fork 0
/
states.go
197 lines (167 loc) · 3.97 KB
/
states.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 "fmt"
////states////
const (
stateMainMenu uint = iota
stateNewGame
stateRunningGame
statePausedGame
stateGameOver
stateExit
totalStates
)
////subStates////
//subStates: stateMainMenu
const (
stateMainMenuSelectorIndex uint = iota
stateMainMenuSubStates
)
const (
stateMainMenuSelectorInit int = 0
)
//subStates: stateNewGame
const (
stateNewGameSizeSelectorIndex uint = iota
stateNewGameSubStates
)
const (
stateNewGameSizeSelectorInit int = 0
)
//subStates: stateRunningGame
const (
stateRunningGameSubStates uint = iota
)
//subStates: statePausedGame
const (
statePausedGameSelectorIndex uint = iota
statePausedGameSubStates
)
const (
statePausedGameSelectorInit int = 0
)
//subStates: stateGameOver
const (
stateGameOverSelectorIndex uint = iota
stateGameOverSubStates
)
const (
stateGameOverSelectorInit int = 0
)
//subStates: stateExit
const (
stateExitSubStates uint = iota
)
//A shorthand way of accessing the number of substates per state.
var (
totalSubStates [totalStates]uint = [totalStates]uint{
stateMainMenuSubStates,
stateNewGameSubStates,
stateRunningGameSubStates,
statePausedGameSubStates,
stateGameOverSubStates,
stateExitSubStates,
}
)
//A shorthand was of accessing the initial values of substates for each state.
var (
subStateInitialValues [totalStates][]int = [totalStates][]int{
[]int{stateMainMenuSelectorInit},
[]int{stateNewGameSizeSelectorInit},
[]int{},
[]int{statePausedGameSelectorInit},
[]int{stateGameOverSelectorInit},
[]int{},
}
)
////Data structures////
const (
stateType uint8 = iota
subStateType
errorType
)
type stateRequest struct {
reqType uint8
subStateIndex uint
}
func initStateReq() stateRequest {
sr := stateRequest{
reqType: stateType,
subStateIndex: 0,
}
return sr
}
func initSubStateReq(i uint) stateRequest {
ssr := stateRequest{
reqType: subStateType,
subStateIndex: i,
}
return ssr
}
type stateDescriptor struct {
descType uint8
state uint
subState int
subStateIndex uint
}
func initStateDesc(s uint) stateDescriptor {
sd := stateDescriptor{
descType: stateType,
state: s,
subState: 0,
subStateIndex: 0,
}
return sd
}
func initSubStateDesc(s uint, ss int, i uint) stateDescriptor {
ssd := stateDescriptor{
descType: subStateType,
state: s,
subState: ss,
subStateIndex: i,
}
return ssd
}
func initErrorDesc() stateDescriptor {
err := stateDescriptor{
descType: errorType,
state: 0,
subState: 0,
subStateIndex: 0,
}
return err
}
////Full Request Functions////
func getState(rqst chan<- stateRequest, rcv <-chan stateDescriptor) uint {
rqst <- initStateReq()
return (<- rcv).state
}
func getSubState(rqst chan<- stateRequest, rcv <-chan stateDescriptor, state uint, subStateIndex uint) int {
if subStateIndex >= totalSubStates[state] {
panic(fmt.Sprintf("State %d does not have a subState %d.", state, subStateIndex))
}
rqst <- initSubStateReq(subStateIndex)
desc := <-rcv
if desc.descType == errorType {
panic(fmt.Sprintf("State has changed from %d to %d.", state, getState(rqst, rcv)))
}else if desc.descType == subStateType && desc.state != state {
panic(fmt.Sprintf("State has changed from %d to %d.", state, desc.state))
}
return desc.subState
}
////Full Modify Functions////
func setState(mdfy chan<- stateDescriptor, ack <-chan stateDescriptor, state uint) {
desc := initStateDesc(state)
mdfy <- desc
result := <-ack
if desc.descType != result.descType || desc.state != result.state {
panic(fmt.Sprintf("State was not changed to %d.", state))
}
}
func setSubState(mdfy chan<- stateDescriptor, ack <-chan stateDescriptor, state uint, subState int, subStateIndex uint) {
desc := initSubStateDesc(state, subState, subStateIndex)
mdfy <- desc
result := <-ack
if desc.descType != result.descType || desc.state != result.state || desc.subState != result.subState || desc.subStateIndex != result.subStateIndex {
panic(fmt.Sprintf("Sub-State %d was not changed to %d (where state is %d).", subStateIndex, subState, state))
}
}