~hokiegeek/life

ref: 6aab9adeea1393048d04b235cd9d8145e302779e life/cmd/life/main.go -rw-r--r-- 3.3 KiB View raw
6aab9adeHokieGeek Updated version of go to build with 8 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
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
package main

import (
	"bufio"
	"flag"
	"fmt"
	"os"
	"time"

	"gitlab.com/pond-life/life"
)

func displaypond(strategy *life.Life, rate time.Duration, iterations int, static, paused bool) {
	// Clear the screen and put the cursor on the top left
	if static {
		fmt.Print("\033[2J")
		fmt.Print("\033[1;1H")
	}

	// Print the seed
	fmt.Println("Generation: 0")
	fmt.Print(strategy)

	if paused {
		reader := bufio.NewReader(os.Stdin)
		reader.ReadString('\n')
	}

	updates := make(chan *life.Generation)
	rateLimitedUpdates := make(chan *life.Generation)
	stop := strategy.Start(updates)

	go func() {
		countGenerations := 1
		ticker := time.NewTicker(rate)

		for {
			select {
			case <-ticker.C:
				gen := <-rateLimitedUpdates
				if static {
					fmt.Print("\033[1;1H")
				}
				fmt.Printf("Generation: %d\n", gen.Num)
				fmt.Print(strategy)

				if iterations >= 0 {
					countGenerations++
					if countGenerations >= iterations {
						ticker.Stop()
						stop()
						break
					}
				}
			}
		}
	}()

	for {
		select {
		case gen := <-updates:
			rateLimitedUpdates <- gen
		}
	}
}

func displayTestpond(width int, height int, rate time.Duration, initializer func(life.Dimensions, life.Location) []life.Location) {
	strategy, err := life.New(
		life.Dimensions{Height: height, Width: width},
		life.NeighborsAll,
		initializer,
		life.ConwayTester(),
		life.SimultaneousProcessor)
	if err == nil {
		displaypond(strategy, rate, -1, true, true)
	} else {
		fmt.Printf("Could not create: %s\n", err)
	}
}

func main() {

	patternPtr := flag.String("pattern", "random", "Specify the pattern to run")
	widthPtr := flag.Int("width", 1, "Width of the Life board")
	heightPtr := flag.Int("height", 1, "Height of the Life board")
	ratePtr := flag.Duration("rate", 1, "Rate at which the board should be updated")
	extraPtr := flag.Int("extra", -1, "Extra values for pattners (such as random)")

	flag.Parse()

	switch *patternPtr {
	case "blinkers":
		width := 9
		if *widthPtr > width {
			width = *widthPtr
		}
		height := 9
		if *heightPtr > height {
			height = *heightPtr
		}

		displayTestpond(width, height, *ratePtr, life.Blinkers)
	case "toads":
		width := 10
		if *widthPtr > width {
			width = *widthPtr
		}
		height := 10
		if *heightPtr > height {
			height = *heightPtr
		}

		displayTestpond(width, height, *ratePtr, life.Toads)
	case "glider":
		width := 30
		if *widthPtr > width {
			width = *widthPtr
		}
		height := 30
		if *heightPtr > height {
			height = *heightPtr
		}

		displayTestpond(width, height, *ratePtr,
			func(dimensions life.Dimensions, offset life.Location) []life.Location {
				return life.Gliders(life.Dimensions{Height: 4, Width: 4}, offset)
			})
	case "pulsar":
		width := 15
		if *widthPtr > width {
			width = *widthPtr
		}
		height := 15
		if *heightPtr > height {
			height = *heightPtr
		}

		displayTestpond(width, height, *ratePtr, life.Pulsar)
	case "random":
		width := 120
		if *widthPtr > width {
			width = *widthPtr
		}
		height := 45
		if *heightPtr > height {
			height = *heightPtr
		}

		percentCoverage := 35
		if *extraPtr > -1 {
			percentCoverage = *extraPtr
		}

		displayTestpond(width, height, *ratePtr,
			func(dimensions life.Dimensions, offset life.Location) []life.Location {
				return life.Random(dimensions, offset, percentCoverage)
			})
	default:
		fmt.Println("Did not recognize pattern")
	}
}

// vim: set foldmethod=marker: