|
|
|
@ -15,10 +15,28 @@ const (
|
|
|
|
|
LIVE_CELL = " "
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
var (
|
|
|
|
|
neighbors = []neighbor{
|
|
|
|
|
neighbor{-1, -1},
|
|
|
|
|
neighbor{-1, 0},
|
|
|
|
|
neighbor{-1, 1},
|
|
|
|
|
neighbor{0, -1},
|
|
|
|
|
neighbor{0, 1},
|
|
|
|
|
neighbor{1, -1},
|
|
|
|
|
neighbor{1, 0},
|
|
|
|
|
neighbor{1, 1},
|
|
|
|
|
}
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
|
rand.Seed(time.Now().UTC().UnixNano())
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
type neighbor struct {
|
|
|
|
|
X int
|
|
|
|
|
Y int
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
type GameOfLife struct {
|
|
|
|
|
State *GameState
|
|
|
|
|
}
|
|
|
|
@ -27,6 +45,7 @@ type GameStateCell struct {
|
|
|
|
|
Value int
|
|
|
|
|
X int
|
|
|
|
|
Y int
|
|
|
|
|
cols []int
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
type gameGridRow struct {
|
|
|
|
@ -124,31 +143,43 @@ func (state *GameState) Import(other *GameState) (err error) {
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (state *GameState) Cells(cells chan *GameStateCell) error {
|
|
|
|
|
func (state *GameState) Cells() (<-chan *GameStateCell, error) {
|
|
|
|
|
height := state.Height()
|
|
|
|
|
width := state.Width()
|
|
|
|
|
|
|
|
|
|
if height < 0 || width < 0 {
|
|
|
|
|
return errors.New("state has invalid dimensions!")
|
|
|
|
|
return nil, errors.New("state has invalid dimensions!")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
defer close(cells)
|
|
|
|
|
cells := make(chan *GameStateCell)
|
|
|
|
|
|
|
|
|
|
for y := 0; y < height; y++ {
|
|
|
|
|
for x := 0; x < width; x++ {
|
|
|
|
|
value, err := state.Get(x, y)
|
|
|
|
|
go func(state *GameState, c chan *GameStateCell) {
|
|
|
|
|
defer close(c)
|
|
|
|
|
height := state.Height()
|
|
|
|
|
width := state.Width()
|
|
|
|
|
|
|
|
|
|
for y := 0; y < height; y++ {
|
|
|
|
|
row, err := state.GetRow(y)
|
|
|
|
|
if err != nil {
|
|
|
|
|
panic(err)
|
|
|
|
|
}
|
|
|
|
|
cells <- &GameStateCell{
|
|
|
|
|
Value: value,
|
|
|
|
|
X: x,
|
|
|
|
|
Y: y,
|
|
|
|
|
|
|
|
|
|
for x := 0; x < width; x++ {
|
|
|
|
|
value, err := state.Get(x, y)
|
|
|
|
|
if err != nil {
|
|
|
|
|
panic(err)
|
|
|
|
|
}
|
|
|
|
|
c <- &GameStateCell{
|
|
|
|
|
Value: value,
|
|
|
|
|
X: x,
|
|
|
|
|
Y: y,
|
|
|
|
|
cols: row.Cols,
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}(state, cells)
|
|
|
|
|
|
|
|
|
|
return nil
|
|
|
|
|
return (<-chan *GameStateCell)(cells), nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (game *GameOfLife) Mutate() error {
|
|
|
|
@ -169,20 +200,21 @@ func (state *GameState) Mutate() error {
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (game *GameOfLife) Checksum() string {
|
|
|
|
|
func (game *GameOfLife) Checksum() (string, error) {
|
|
|
|
|
return game.State.Checksum()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (state *GameState) Checksum() string {
|
|
|
|
|
func (state *GameState) Checksum() (string, error) {
|
|
|
|
|
ck := sha1.New()
|
|
|
|
|
cells := make(chan *GameStateCell)
|
|
|
|
|
|
|
|
|
|
go state.Cells(cells)
|
|
|
|
|
cells, err := state.Cells()
|
|
|
|
|
if err != nil {
|
|
|
|
|
return "", err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for cell := range cells {
|
|
|
|
|
fmt.Fprintf(ck, "%v", cell.Value)
|
|
|
|
|
}
|
|
|
|
|
return fmt.Sprintf("%x", ck.Sum(nil))
|
|
|
|
|
return fmt.Sprintf("%x", ck.Sum(nil)), nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func NewGameOfLife(height, width int) *GameOfLife {
|
|
|
|
@ -197,50 +229,50 @@ func (game *GameOfLife) EvaluateGeneration() error {
|
|
|
|
|
genScore := NewGenerationScoreCard(height, width)
|
|
|
|
|
genScore.Calculate(game.State)
|
|
|
|
|
|
|
|
|
|
for y := 0; y < height; y++ {
|
|
|
|
|
for x := 0; x < width; x++ {
|
|
|
|
|
score, err := genScore.Get(x, y)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
cells, err := genScore.Cells()
|
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
curState, err := game.State.Get(x, y)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
for cell := range cells {
|
|
|
|
|
score := cell.Value
|
|
|
|
|
|
|
|
|
|
if curState == 1 {
|
|
|
|
|
curState, err := game.State.Get(cell.X, cell.Y)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if score < 2 {
|
|
|
|
|
err = game.State.Deaden(x, y)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
if curState == 1 {
|
|
|
|
|
|
|
|
|
|
if score == 2 || score == 3 {
|
|
|
|
|
continue
|
|
|
|
|
if score < 2 {
|
|
|
|
|
err = game.State.Deaden(cell.X, cell.Y)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if score > 3 {
|
|
|
|
|
err = game.State.Deaden(x, y)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
continue
|
|
|
|
|
if score == 2 || score == 3 {
|
|
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if score > 3 {
|
|
|
|
|
err = game.State.Deaden(cell.X, cell.Y)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
} else if curState == 0 {
|
|
|
|
|
|
|
|
|
|
if score == 3 {
|
|
|
|
|
err = game.State.Enliven(x, y)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
continue
|
|
|
|
|
} else {
|
|
|
|
|
continue
|
|
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
} else if curState == 0 {
|
|
|
|
|
|
|
|
|
|
if score == 3 {
|
|
|
|
|
err = game.State.Enliven(cell.X, cell.Y)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
continue
|
|
|
|
|
} else {
|
|
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
@ -264,13 +296,13 @@ func (game *GameOfLife) ImportRandomState() error {
|
|
|
|
|
|
|
|
|
|
randState := NewGameState(height, width)
|
|
|
|
|
|
|
|
|
|
for y := 0; y < height; y++ {
|
|
|
|
|
for x := 0; x < width; x++ {
|
|
|
|
|
err := randState.Set(x, y, rand.Intn(2))
|
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
cells, err := randState.Cells()
|
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for cell := range cells {
|
|
|
|
|
cell.SetValue(rand.Intn(2))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return game.ImportState(randState)
|
|
|
|
@ -280,6 +312,10 @@ func (game *GameOfLife) String() string {
|
|
|
|
|
return fmt.Sprintf("%s\n", game.State)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (cell *GameStateCell) SetValue(value int) {
|
|
|
|
|
cell.cols[cell.X] = value
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (state *GameState) String() string {
|
|
|
|
|
var rows []string
|
|
|
|
|
|
|
|
|
@ -319,42 +355,37 @@ func NewGenerationScoreCard(height, width int) *GenerationScoreCard {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (genScore *GenerationScoreCard) Calculate(state *GameState) error {
|
|
|
|
|
stateHeight, stateWidth := state.Height(), state.Width()
|
|
|
|
|
for y := 0; y < stateHeight; y++ {
|
|
|
|
|
for x := 0; x < stateWidth; x++ {
|
|
|
|
|
value, err := state.Get(x, y)
|
|
|
|
|
stateWidth := state.Width()
|
|
|
|
|
stateCells, err := state.Cells()
|
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for stateCell := range stateCells {
|
|
|
|
|
if stateCell.Value == 0 {
|
|
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for _, neighbor := range neighbors {
|
|
|
|
|
xTarget := stateCell.X + neighbor.X
|
|
|
|
|
yTarget := stateCell.Y + neighbor.Y
|
|
|
|
|
|
|
|
|
|
if xTarget < 0 || yTarget < 0 {
|
|
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if xTarget+1 > stateWidth || yTarget+1 > stateWidth {
|
|
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
curScore, err := genScore.Get(xTarget, yTarget)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if value == 1 {
|
|
|
|
|
for yOffset := -1; yOffset < 2; yOffset++ {
|
|
|
|
|
for xOffset := -1; xOffset < 2; xOffset++ {
|
|
|
|
|
xTarget := x + xOffset
|
|
|
|
|
yTarget := y + yOffset
|
|
|
|
|
|
|
|
|
|
if xTarget < 0 || yTarget < 0 {
|
|
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if xTarget+1 > stateWidth || yTarget+1 > stateWidth {
|
|
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if xTarget == x && yTarget == y {
|
|
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
curScore, err := genScore.Get(xTarget, yTarget)
|
|
|
|
|
if err != nil {
|
|
|
|
|
continue // assume out of bounds, which is stinky
|
|
|
|
|
}
|
|
|
|
|
err = genScore.Set(xTarget, yTarget, curScore+1)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
err = genScore.Set(xTarget, yTarget, curScore+1)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|