-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrabbit.go
More file actions
313 lines (274 loc) · 7.55 KB
/
rabbit.go
File metadata and controls
313 lines (274 loc) · 7.55 KB
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
309
310
311
312
313
package main
import (
"encoding/json"
"time"
)
// A state the rabbit can be in.
type RabbitState uint
// An event that can be performed on a rabbit.
type RabbitAction uint
const (
// Initial state.
Wandering RabbitState = iota
// Transient state. The rabbit will move to fleeing.
Spotted
// The rabbit is in a fleeing state when it's spotted.
Fleeing
// If the rabbit was successfully caught.
Caught
// If the rabbit died this will be the state. The rabbit only dies
// if the location it's in no longer exists.
Dead
)
const (
// Default action. This is performed on every rabbit.
Wait RabbitAction = iota
// A rabbit is spotted.
Spot
// Force a fee.
Flee
// When a catch attempt succeeds.
Catch
// When a rabbit dies. :(
Kill
)
// The time that elapses before a rabbit wants to moved.
const IdleTime = time.Duration(5) * time.Minute
// The time that elapses before a rabbit moves after being spotted.
const FleeTime = time.Duration(5) * time.Second
// A forest is a place that can be traversed. Locations in a forest
// are simple strings.
type Forest interface {
// Returns true if passed location exists.
LocationExists(loc string) bool
// Returns a location fairly close to the one provided.
NearbyLocation(loc string) string
// Returns a faraway location, this could be anywhere
// except the location passed (unless it's the only location).
FarawayLocation(loc string) string
}
// A rabbit is a simple creature that likes to move around a forest. You can
// spot it, try to catch it, tag it, or accidentally kill it. :(
type Rabbit struct {
// The forest the rabbit lives in.
home Forest
// The current location in the forest. May be "", in which
// case the rabbit is no longer in the forest (dead, caught).
location string
// A tag identifying this specific rabbit.
tag string
// The last location visited. May be "", in which case the
// rabbit never moved.
lastLocation string
// The last time the rabbit moved to new location.
lastMoved time.Time
// The time the rabbit was spotted last. May be nil.
lastSpotted *time.Time
// State of the rabbit.
state RabbitState
// These are set to the defaults.
idleTime time.Duration
fleeTime time.Duration
}
var rMachine Machine
func init() {
// Create the rabbit state machine.
rMachine = NewMachine()
rMachine.AddTransition(State(Wandering), Action(Wait), State(Wandering))
rMachine.AddTransition(State(Wandering), Action(Spot), State(Spotted))
rMachine.AddTransition(State(Wandering), Action(Catch), State(Caught))
rMachine.AddTransition(State(Wandering), Action(Kill), State(Dead))
rMachine.AddTransition(State(Spotted), Action(Wait), State(Fleeing))
// Can't spot an already spotted rabbit.
rMachine.AddTransition(State(Spotted), Action(Flee), State(Fleeing))
rMachine.AddTransition(State(Spotted), Action(Catch), State(Caught))
rMachine.AddTransition(State(Spotted), Action(Kill), State(Dead))
rMachine.AddTransition(State(Fleeing), Action(Wait), State(Wandering))
// Can't spot or catch a fleeing rabbit.
rMachine.AddTransition(State(Fleeing), Action(Kill), State(Dead))
}
// Creates a new rabbit and moves it to a faraway location.
func NewRabbit(f Forest) Rabbit {
r := Rabbit{
f, "", "", "", time.Now(), nil, Wandering,
IdleTime, FleeTime,
}
r.location = f.FarawayLocation("")
return r
}
// Step 1 for becoming Stateful.
func (r *Rabbit) State() State {
return State(r.state)
}
// Step 2 for becoming Stateful.
func (r *Rabbit) ShouldTransition(act Action, to State) bool {
ract := act.(RabbitAction)
rstate := to.(RabbitState)
switch ract {
case Wait:
if rstate == Wandering {
return time.Now().Sub(r.lastMoved) >= r.idleTime
} else if rstate == Fleeing {
return time.Now().Sub(*r.lastSpotted) >= r.fleeTime
} else {
panic("Waiting when not wandering or fleeing.")
}
case Catch:
elapsed := time.Now().Sub(*r.lastSpotted)
catchchance := 1.0 - float64(elapsed) / float64(FleeTime)
return chance(catchchance)
default:
return true
}
}
// Step 3 for becoming Stateful.
func (r *Rabbit) EnterState(state State) {
rstate := state.(RabbitState)
switch rstate {
case Wandering:
r.lastMoved = time.Now()
r.lastLocation = r.location
r.location = r.home.NearbyLocation(r.location)
r.state = rstate
case Spotted:
// Uh-oh!
r.state = rstate
t := time.Now()
r.lastSpotted = &t
// Will start to flee the next update.
case Fleeing:
r.lastMoved = time.Now()
r.lastLocation = r.location
r.location = r.home.FarawayLocation(r.location)
r.state = rstate
case Caught:
r.location = ""
r.state = rstate
case Dead:
r.location = ""
r.state = rstate
default:
}
}
// This is called before every operation. Returns true if the rabbit
// awake and ready.
func (r *Rabbit) wakeup() bool {
if !r.IsPlaying() {
return false
}
rMachine.Perform(r, Wait)
return true
}
// Used mostly for testing. The default is preferred.
func (r *Rabbit) setIdleTime(d time.Duration) {
r.idleTime = d
}
// Used mostly for testing. The default is preferred.
func (r *Rabbit) setFleeTime(d time.Duration) {
r.fleeTime = d
}
// Changes the home of the rabbit.
func (r *Rabbit) ChangeHome(f Forest) {
r.home = f
}
// A place in the forest was disturbed. Possibly move, or
// if the place is here, the rabbit is spotted.
func (r *Rabbit) DisturbanceAt(loc string) {
if !r.wakeup() {
return
}
if r.location == loc {
rMachine.Perform(r, Spot)
}
}
// Attempts to catch the rabbit. The rabbit first checks if
// it already moved with wakeup(). The chance to catch the
// rabbit is the inverse of the time is has left before moving.
func (r *Rabbit) TryCatch(loc string) bool {
if !r.wakeup() {
return false
}
if r.location != loc {
return false
}
if !rMachine.Perform(r, Catch) {
// Oh-well, better luck next time.
rMachine.Perform(r, Flee)
return false
}
return true
}
// Attempts to tag the rabbit. Right now it's a 100% chance.
func (r *Rabbit) TryTag(loc, tag string) bool {
if !r.wakeup() {
return false
}
if r.location != loc {
return false
}
r.tag = tag
rMachine.Perform(r, Flee)
return true
}
// Returns the current location of the rabbit.
func (r *Rabbit) Location() string {
return r.location
}
// Returns the current tag of the rabbit, "" is none.
func (r *Rabbit) Tag() string {
return r.tag
}
// Returns true if this rabbit has JUST been spotted. This
// state will immediately move to the fleeing state.
func (r *Rabbit) JustSpotted() bool {
return r.state == Spotted
}
// Returns true if the rabbit is apart of the game. The rabbit
// is no longer playing if it's caught/dead/etc. Or if the location
// the rabbit is no longer exists.
func (r *Rabbit) IsPlaying() bool {
if !r.home.LocationExists(r.location) {
rMachine.Perform(r, Kill)
return false
}
return r.state != Dead && r.state != Caught
}
// Used for marshalling/unmarshalling.
type rabbit struct {
Location string
Tag string
LastLocation string
LastMoved time.Time
LastSpotted *time.Time
State RabbitState
IdleTime time.Duration
FleeTime time.Duration
}
func (r *Rabbit) UnmarshalJSON(b []byte) error {
data := rabbit{}
err := json.Unmarshal(b, &data)
if err != nil {
return err
}
r.location = data.Location
r.tag = data.Tag
r.lastLocation = data.LastLocation
r.lastMoved = data.LastMoved
r.lastSpotted = data.LastSpotted
r.state = data.State
r.idleTime = data.IdleTime
r.fleeTime = data.FleeTime
return nil
}
func (r *Rabbit) MarshalJSON() ([]byte, error) {
return json.Marshal(&rabbit{
Location: r.location,
Tag: r.tag,
LastLocation: r.lastLocation,
LastMoved: r.lastMoved,
LastSpotted: r.lastSpotted,
State: r.state,
IdleTime: r.idleTime,
FleeTime: r.fleeTime,
})
}