You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
box-o-sand/conway/game_state.go

188 lines
3.4 KiB

package conway
import (
"crypto/sha1"
"errors"
"fmt"
"math"
"math/rand"
"strings"
)
const (
DEAD_CELL = "■"
LIVE_CELL = " "
)
type GameState struct {
Rows []*GameStateRow
}
func NewGameState(height, width int) *GameState {
state := &GameState{}
for i := 0; i < height; i++ {
row := &GameStateRow{Y: i}
for j := 0; j < width; j++ {
row.Cells = append(row.Cells, 0)
}
state.Rows = append(state.Rows, row)
}
return state
}
func (state *GameState) Height() int {
return len(state.Rows)
}
func (state *GameState) Width() int {
if len(state.Rows) < 1 {
return -1
}
return len(state.Rows[0].Cells)
}
func (state *GameState) GetRow(y int) *GameStateRow {
lenRows := len(state.Rows)
if y+1 > lenRows {
return state.GetRow(y % lenRows)
}
return state.Rows[y]
}
func (state *GameState) Set(x, y, value int) error {
if x < 0 || y < 0 {
errMsg := fmt.Sprintf("coordinates (%v, %v) are out of bounds!", x, y)
return errors.New(errMsg)
}
row := state.GetRow(y)
if x+1 > len(row.Cells) {
errMsg := fmt.Sprintf("x coordinate %v is out of bounds!", x)
return errors.New(errMsg)
}
row.Cells[x] = value
return nil
}
func (state *GameState) Get(x, y int) (int, error) {
row := state.GetRow(y)
if len(row.Cells) < x+1 {
return -1, errors.New("x coordinate is out of bounds!")
}
return row.Cells[x], nil
}
func (state *GameState) Enliven(x, y int) error {
return state.Set(x, y, 1)
}
func (state *GameState) Deaden(x, y int) error {
return state.Set(x, y, 0)
}
func (state *GameState) Import(other *GameState) (err error) {
for y, row := range other.Rows {
for x, cell := range row.Cells {
if err = state.Set(x, y, cell); err != nil {
return err
}
}
}
return nil
}
func (state *GameState) Cells() (<-chan *GameStateCell, error) {
height := state.Height()
width := state.Width()
if height < 0 || width < 0 {
return nil, errors.New("state has invalid dimensions!")
}
cells := make(chan *GameStateCell)
go func(state *GameState, c chan *GameStateCell) {
defer close(c)
height := state.Height()
width := state.Width()
for y := 0; y < height; y++ {
row := state.GetRow(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,
cellmates: row.Cells,
}
}
}
}(state, cells)
return (<-chan *GameStateCell)(cells), nil
}
func (state *GameState) Mutate() error {
randX := rand.Intn(state.Width())
randY := rand.Intn(state.Height())
curVal, err := state.Get(randX, randY)
if err != nil {
return err
}
err = state.Set(randX, randY, int(math.Abs(float64(curVal-1))))
return nil
}
func (state *GameState) Checksum() (string, error) {
ck := sha1.New()
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)), nil
}
func (state *GameState) String() string {
var rows []string
height := state.Height()
width := state.Width()
for y := 0; y < height; y++ {
var cells []string
for x := 0; x < width; x++ {
stringVal := DEAD_CELL
value, err := state.Get(x, y)
if err != nil {
return ""
}
if value == 1 {
stringVal = LIVE_CELL
}
cells = append(cells, stringVal)
}
rows = append(rows, strings.Join(cells, ""))
}
return strings.Join(rows, "\n")
}