better comments, simplified check

This commit is contained in:
2024-07-13 20:46:11 +02:00
parent 05d56568e4
commit 89903fdcec
2 changed files with 19 additions and 18 deletions

View File

@@ -13,9 +13,9 @@ import (
) )
type Cell struct { type Cell struct {
State uint8 State uint8 // 1==life, 0==dead
Neighbors [8]*Cell Neighbors [8]*Cell // all neighbors, max 8
NeighborCount int NeighborCount int // number of neighbors, might be less than 8 on edges
} }
func (cell *Cell) Count() uint8 { func (cell *Cell) Count() uint8 {

View File

@@ -100,34 +100,35 @@ func (scene *ScenePlay) SetNext(next SceneName) {
scene.Next = next scene.Next = next
} }
/* The standard Scene of Life is symbolized in rule-string notation
* as B3/S23 (23/3 here). A cell is born if it has exactly three
* neighbors, survives if it has two or three living neighbors,
* and dies otherwise.
* we abbreviate the calculation: if state is 0 and 3 neighbors
* are a life, check will be just 3. If the cell is alive, 9 will
* be added to the life neighbors (to avoid a collision with the
* result 3), which will be 11|12 in case of 2|3 life neighbors.
*/
func (scene *ScenePlay) CheckRuleB3S23(state uint8, neighbors uint8) uint8 { func (scene *ScenePlay) CheckRuleB3S23(state uint8, neighbors uint8) uint8 {
var nextstate uint8 switch (9 * state) + neighbors {
check := (9 * state) + neighbors
switch check {
case 11: case 11:
fallthrough fallthrough
case 12: case 12:
fallthrough fallthrough
case 3: case 3:
nextstate = Alive return Alive
default:
nextstate = Dead
} }
return nextstate return Dead
} }
/*
* The generic rule checker is able to calculate cell state for any
* GOL rul, including B3/S23.
*/
func (scene *ScenePlay) CheckRuleGeneric(state uint8, neighbors uint8) uint8 { func (scene *ScenePlay) CheckRuleGeneric(state uint8, neighbors uint8) uint8 {
var nextstate uint8 var nextstate uint8
// The standard Scene of Life is symbolized in rule-string notation
// as B3/S23 (23/3 here). A cell is born if it has exactly three
// neighbors, survives if it has two or three living neighbors,
// and dies otherwise. The first number, or list of numbers, is
// what is required for a dead cell to be born.
if state != 1 && Contains(scene.Config.Rule.Birth, neighbors) { if state != 1 && Contains(scene.Config.Rule.Birth, neighbors) {
nextstate = Alive nextstate = Alive
} else if state == 1 && Contains(scene.Config.Rule.Death, neighbors) { } else if state == 1 && Contains(scene.Config.Rule.Death, neighbors) {