~hokiegeek/life

ref: c41a449353d445a06ec7334b540afb793d577cd8 life/patterns.go -rw-r--r-- 8.9 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
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
package life

import (
	// "fmt"
	"math/rand"
	"time"
)

/////////////////////////// COMMON ///////////////////////////

func getCountsForDimensions(boardDims, patternDims Dimensions) (int, int) {
	numPerRow := boardDims.Width / patternDims.Height
	numPerCol := boardDims.Height / patternDims.Width

	// Special case for when the spacer is not needed
	if numPerRow == 0 && boardDims.Height == patternDims.Height-1 {
		numPerRow = 1
	}
	if numPerCol == 0 && boardDims.Width == patternDims.Width-1 {
		numPerCol = 1
	}

	// fmt.Printf(">>>> numPerRow = %d\n", numPerRow)
	// fmt.Printf(">>>> numPerCol = %d\n", numPerCol)
	return numPerRow, numPerCol
}

func getRepeatingPattern(boardDims, patternDims Dimensions, offset Location,
	pattern func(*[]Location, int, int)) []Location {

	numPerRow, numPerCol := getCountsForDimensions(boardDims, patternDims)

	seed := make([]Location, 0)
	for row := 0; row < numPerCol; row++ {
		// currentY := (row * patternDims.Height)
		currentY := (row * patternDims.Height) + offset.Y

		for col := 0; col < numPerRow; col++ {
			// currentX := (col * patternDims.Width)
			currentX := (col * patternDims.Width) + offset.X
			pattern(&seed, currentX, currentY)
		}
	}

	return seed
}

/////////////////// RANDOM ///////////////////

// Random generates a random pattern
func Random(dimensions Dimensions, offset Location, percent int) []Location {
	seed := make([]Location, 0)

	rand.Seed(time.Now().UnixNano())
	for i := 0; i < dimensions.Height; i++ {
		for j := 0; j < dimensions.Width; j++ {
			if (100 - rand.Intn(100)) <= percent {
				seed = append(seed, Location{X: j + offset.X, Y: i + offset.Y})
			}
		}
	}

	return seed
}

/////////////////// OSCILLATORS ///////////////////

// Blinkers generates a basic Blinker oscillator
//	Period 1  Period 2
// 	-0-       ---
// 	-0-       000
// 	-0-       ---
func Blinkers(dimensions Dimensions, offset Location) []Location {
	const HEIGHT = 4 // 3 for the line itself and 1 for the spacer
	return getRepeatingPattern(dimensions, Dimensions{Height: HEIGHT, Width: HEIGHT},
		offset,
		func(seed *[]Location, currentX, currentY int) {
			for i := 0; i < 3; i++ {
				*seed = append(*seed, Location{X: currentX + i, Y: currentY + 1})
			}
		})

}

// Toads generates a Toad oscillator
//	Period 1	  Period 2
// 	----       --0-
// 	-000       0--0
// 	000-       0--0
// 	----       -0--
func Toads(dimensions Dimensions, offset Location) []Location {
	const HEIGHT = 5
	return getRepeatingPattern(dimensions, Dimensions{Height: HEIGHT, Width: HEIGHT},
		offset,
		func(seed *[]Location, currentX, currentY int) {
			// ROW 1
			for i := 1; i < 4; i++ {
				*seed = append(*seed, Location{X: currentX + i, Y: currentY + 1})
			}
			// ROW 2
			for i := 0; i < 3; i++ {
				*seed = append(*seed, Location{X: currentX + i, Y: currentY + 2})
			}
		})
}

// Beacons generates a Beacon oscillator
//	Period 1   Period 2
//	00--       00--
// 	0---       00--
// 	---0       --00
// 	--00       --00
func Beacons(dimensions Dimensions, offset Location) []Location {

	const HEIGHT = 5
	return getRepeatingPattern(dimensions, Dimensions{Height: HEIGHT, Width: HEIGHT},
		offset,
		func(seed *[]Location, currentX, currentY int) {
			// ROW 1
			*seed = append(*seed, Location{X: currentX, Y: currentY})
			*seed = append(*seed, Location{X: currentX + 1, Y: currentY})
			// ROW 2
			*seed = append(*seed, Location{X: currentX, Y: currentY + 1})
			// ROW 3
			*seed = append(*seed, Location{X: currentX + 3, Y: currentY + 2})
			// ROW 4
			*seed = append(*seed, Location{X: currentX + 2, Y: currentY + 3})
			*seed = append(*seed, Location{X: currentX + 3, Y: currentY + 3})
		})
}

// Pulsar generates a Pulsar oscillator
//	Period 1          Period 2          Period 3
// 	---------------   ----0-----0----   ---------------
// 	---000---000---   ----0-----0----   ---00-----00---
// 	---------------   ----00---00----   ----00---00----
// 	-0----0-0----0-   ---------------   -0--0-0-0-0--0-
// 	-0----0-0----0-   000--00-00--000   -000-00-00-000-
// 	-0----0-0----0-   --0-0-0-0-0-0--   --0-0-0-0-0-0--
// 	---000---000---   ----00---00----   ---000---000---
// 	---------------   ---------------   ---------------
// 	---000---000---   ----00---00----   ---000---000---
// 	-0----0-0----0-   --0-0-0-0-0-0--   --0-0-0-0-0-0--
// 	-0----0-0----0-   000--00-00--000   -000-00-00-000-
// 	-0----0-0----0-   ---------------   -0--0-0-0-0--0-
// 	---------------   ----00---00----   ----00---00----
// 	---000---000---   ----0-----0----   ---00-----00---
// 	---------------   ----0-----0----   ---------------
func Pulsar(dimensions Dimensions, offset Location) []Location {
	const HEIGHT = 16
	return getRepeatingPattern(dimensions, Dimensions{Height: HEIGHT, Width: HEIGHT},
		offset,
		func(seed *[]Location, currentX, currentY int) {
			for i := 0; i < HEIGHT-1; i++ {
				switch i {
				case 1, 6, 8, 13:
					// Repeating pattern 1: --000---000--, rows: 0,5,7,12
					for j := 0; j < 2; j++ {
						for k := 3; k <= 5; k++ {
							*seed = append(*seed,
								Location{X: currentX + (k + (6 * j)), Y: currentY + i})
						}
					}
				case 3, 4, 5, 9, 10, 11:
					// Repeating pattern 2: 0----0-0----0, rows: 2,3,4,8,9,10
					for j := 0; j < 2; j++ {
						*seed = append(*seed, Location{X: currentX + (1 + (7 * j)), Y: currentY + i})
						*seed = append(*seed, Location{X: currentX + (6 + (7 * j)), Y: currentY + i})
					}
				}
			}
		})
}

/////////////////// SPACESHIPS ///////////////////

// Gliders generates a basic Glider spaceship
//	Period 1   Period 2   Period 3   Period 4
// 	-0--       ----       ----       ----
// 	--0-       0-0-       --0-       -0--
// 	000-       -00-       0-0-       --00
// 	----       -0--       -00-       -00-
func Gliders(dimensions Dimensions, offset Location) []Location {
	const (
		HEIGHT = 3
		WIDTH  = 4
	)
	return getRepeatingPattern(dimensions, Dimensions{Height: HEIGHT, Width: WIDTH},
		offset,
		func(seed *[]Location, currentX, currentY int) {
			// ROW 1
			*seed = append(*seed, Location{X: currentX + 1, Y: currentY})
			// ROW 2
			*seed = append(*seed, Location{X: currentX + 2, Y: currentY + 1})
			// ROW 3
			for i := 0; i < 3; i++ {
				*seed = append(*seed, Location{X: currentX + i, Y: currentY + 2})
			}
		})
}

/////////////////// STILLS ///////////////////

// Blocks generates the Block still pattern
//	00
// 	00
func Blocks(dimensions Dimensions, offset Location) []Location {

	const HEIGHT = 5
	return getRepeatingPattern(dimensions, Dimensions{Height: HEIGHT, Width: HEIGHT},
		offset,
		func(seed *[]Location, currentX, currentY int) {
			// ROW 1
			*seed = append(*seed, Location{X: currentX, Y: currentY})
			*seed = append(*seed, Location{X: currentX + 1, Y: currentY})
			// ROW 2
			*seed = append(*seed, Location{X: currentX, Y: currentY + 1})
			*seed = append(*seed, Location{X: currentX + 1, Y: currentY + 1})
		})
}

// Beehive generates the Beehive still pattern
//	-00-
// 	0--0
// 	-00-
func Beehive(dimensions Dimensions, offset Location) []Location {

	const (
		HEIGHT = 3
		WIDTH  = 4
	)
	return getRepeatingPattern(dimensions, Dimensions{Height: HEIGHT, Width: WIDTH},
		offset,
		func(seed *[]Location, currentX, currentY int) {
			for row := 0; row < 3; row++ {
				switch row {
				case 0, 2:
					*seed = append(*seed, Location{X: currentX + 1, Y: currentY + row})
					*seed = append(*seed, Location{X: currentX + 2, Y: currentY + row})
				case 1:
					*seed = append(*seed, Location{X: currentX, Y: currentY + row})
					*seed = append(*seed, Location{X: currentX + 3, Y: currentY + row})
				}
			}
		})
}

// Loaf generates the Loaf still pattern
//	-00-
// 	0--0
// 	-0-0
// 	--0-
func Loaf(dimensions Dimensions, offset Location) []Location {
	const HEIGHT = 4
	return getRepeatingPattern(dimensions, Dimensions{Height: HEIGHT, Width: HEIGHT},
		offset,
		func(seed *[]Location, currentX, currentY int) {
			// ROW 1
			*seed = append(*seed, Location{X: currentX + 1, Y: currentY})
			*seed = append(*seed, Location{X: currentX + 2, Y: currentY})
			// ROW 2
			*seed = append(*seed, Location{X: currentX, Y: currentY + 1})
			*seed = append(*seed, Location{X: currentX + 3, Y: currentY + 1})
			// ROW 3
			*seed = append(*seed, Location{X: currentX + 1, Y: currentY + 2})
			*seed = append(*seed, Location{X: currentX + 3, Y: currentY + 2})
			// ROW 4
			*seed = append(*seed, Location{X: currentX + 2, Y: currentY + 3})
		})
}

// Boat generates the Boat still pattern
//	00-
//	0-0
//	-0-
func Boat(dimensions Dimensions, offset Location) []Location {
	const HEIGHT = 3
	return getRepeatingPattern(dimensions, Dimensions{Height: HEIGHT, Width: HEIGHT},
		offset,
		func(seed *[]Location, currentX, currentY int) {
			// ROW 1
			*seed = append(*seed, Location{X: currentX, Y: currentY})
			*seed = append(*seed, Location{X: currentX + 1, Y: currentY})
			// ROW 2
			*seed = append(*seed, Location{X: currentX, Y: currentY + 1})
			*seed = append(*seed, Location{X: currentX + 2, Y: currentY + 1})
			// ROW 3
			*seed = append(*seed, Location{X: currentX + 1, Y: currentY + 2})
		})
}

// vim: set foldmethod=marker: