~hokiegeek/life

ref: c41a449353d445a06ec7334b540afb793d577cd8 life/life.go -rw-r--r-- 2.4 KiB View raw
c41a4493HokieGeek Refactored to host on sr.ht and converted to a mod 10 months ago
                                                                                
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
package life

import (
	"bytes"
)

// Generation encapsulates a snapshot of each generation
type Generation struct {
	Num    int
	Living []Location
}

// Life structure is the primary structure for the simulation
type Life struct {
	pond        *pond
	processor   func(pond *pond, rules func(int, bool) bool)
	ruleset     func(int, bool) bool
	Seed        []Location
	Generations int
}

func (t *Life) process() *Generation {
	// Process any organisms that need to be
	t.processor(t.pond, t.ruleset)

	// Update the pond's statistics
	t.Generations++

	return &Generation{Num: t.Generations, Living: t.pond.living.GetAll()}
}

// Start enables the seeded simulation with each tick providing a Generation object
func (t *Life) Start(listener chan *Generation) func() {
	stop := false

	go func() {
		for {
			if stop {
				break
			} else {
				if listener != nil {
					listener <- t.process()
				}
			}
		}
	}()

	return func() {
		stop = true
	}
}

// Generation provides the snapshot of the given generation
// It will actually simulate the full progression from seed to the given generation
func (t *Life) Generation(num int) *Generation {
	var p *pond
	if num == t.Generations {
		p = t.pond
	} else {
		cloned, err := t.pond.Clone()
		if err != nil {
			// logf("Unable to clone pond: %s\n", err)
			return nil // FIXME
		}
		cloned.SetOrganisms(t.Seed)
		for i := 0; i < num; i++ {
			t.processor(cloned, t.ruleset)
		}

		p = cloned
	}

	return &Generation{Num: num, Living: p.living.GetAll()}
}

// Dimensions returns the dimensions of the Life board
func (t *Life) Dimensions() Dimensions {
	return t.pond.Dims
}

func (t *Life) String() string {
	var buf bytes.Buffer

	buf.WriteString(t.pond.String())

	return buf.String()
}

// New creates a new Life structure
func New(dims Dimensions,
	neighbors neighborsSelector,
	initializer func(Dimensions, Location) []Location,
	rules func(int, bool) bool,
	processor func(pond *pond, rules func(int, bool) bool)) (*Life, error) {
	s := new(Life)

	var err error
	livingTracker := newTracker()
	s.pond, err = newPond(dims, livingTracker, neighbors)
	if err != nil {
		return nil, err
	}

	// Save the given values
	s.ruleset = rules
	s.processor = processor

	// Initialize the pond and schedule the currently living organisms
	s.Seed = initializer(s.pond.Dims, Location{})
	s.pond.SetOrganisms(s.Seed)

	return s, nil
}

// vim: set foldmethod=marker: