forked from fogleman/primitive
/
state.go
48 lines (42 loc) · 1.01 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
package primitive
type State struct {
Worker *Worker
Shape Shape
Alpha int
MutateAlpha bool
Score float64
}
func NewState(worker *Worker, shape Shape, alpha int) *State {
var mutateAlpha bool
if alpha == 0 {
alpha = 128
mutateAlpha = true
}
return &State{worker, shape, alpha, mutateAlpha, -1}
}
func (state *State) Energy() float64 {
if state.Score < 0 {
state.Score = state.Worker.Energy(state.Shape, state.Alpha)
}
return state.Score
}
func (state *State) DoMove() interface{} {
rnd := state.Worker.Rnd
oldState := state.Copy()
state.Shape.Mutate()
if state.MutateAlpha {
state.Alpha = clampInt(state.Alpha+rnd.Intn(21)-10, 1, 255)
}
state.Score = -1
return oldState
}
func (state *State) UndoMove(undo interface{}) {
oldState := undo.(*State)
state.Shape = oldState.Shape
state.Alpha = oldState.Alpha
state.Score = oldState.Score
}
func (state *State) Copy() Annealable {
return &State{
state.Worker, state.Shape.Copy(), state.Alpha, state.MutateAlpha, state.Score}
}