box-o-sand/conway/game_state.go

207 lines
3.8 KiB
Go
Raw Normal View History

2012-12-12 03:50:47 +00:00
package conway
import (
"crypto/sha1"
"errors"
"fmt"
"image"
"image/color"
"image/draw"
2012-12-12 03:50:47 +00:00
"math"
"math/rand"
"strings"
)
const (
2012-12-26 15:58:42 +00:00
deadCell = "■"
liveCell = " "
2012-12-12 03:50:47 +00:00
)
type GameStateCell struct {
Value int `json:"v"`
X int `json:"x"`
Y int `json:"y"`
}
type GameStateRow struct {
Y int `json:"y"`
Cells []*GameStateCell `json:"c"`
}
2012-12-12 03:50:47 +00:00
type GameState struct {
Rows []*GameStateRow `json:"r"`
2012-12-12 03:50:47 +00:00
}
func NewGameState(height, width int) *GameState {
state := &GameState{}
for i := 0; i < height; i++ {
row := &GameStateRow{Y: i}
2012-12-12 03:50:47 +00:00
for j := 0; j < width; j++ {
row.Cells = append(row.Cells, &GameStateCell{
Value: 0,
X: j,
Y: i,
})
2012-12-12 03:50:47 +00:00
}
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)
2012-12-12 03:50:47 +00:00
}
func (state *GameState) GetRow(y int) *GameStateRow {
lenRows := len(state.Rows)
2012-12-16 21:04:54 +00:00
if y < 0 {
return state.GetRow(lenRows + y)
}
if y+1 > lenRows {
return state.GetRow(y % lenRows)
2012-12-12 03:50:47 +00:00
}
return state.Rows[y]
2012-12-12 03:50:47 +00:00
}
func (state *GameState) Set(x, y, value int) {
cell := state.Get(x, y)
cell.Value = value
2012-12-12 03:50:47 +00:00
}
func (state *GameState) Get(x, y int) *GameStateCell {
row := state.GetRow(y)
lenCells := len(row.Cells)
2012-12-12 03:50:47 +00:00
2012-12-16 21:04:54 +00:00
if x < 0 {
return state.Get(lenCells+x, row.Y)
}
if x+1 > lenCells {
return state.Get(x%lenCells, row.Y)
2012-12-12 03:50:47 +00:00
}
return row.Cells[x]
2012-12-12 03:50:47 +00:00
}
func (state *GameState) Enliven(x, y int) {
state.Set(x, y, 1)
2012-12-12 03:50:47 +00:00
}
func (state *GameState) Deaden(x, y int) {
state.Set(x, y, 0)
2012-12-12 03:50:47 +00:00
}
func (state *GameState) Import(other *GameState) {
2012-12-12 03:50:47 +00:00
for y, row := range other.Rows {
for x, cell := range row.Cells {
state.Set(x, y, cell.Value)
2012-12-12 03:50:47 +00:00
}
}
}
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++ {
for x := 0; x < width; x++ {
c <- state.Get(x, y)
2012-12-12 03:50:47 +00:00
}
}
}(state, cells)
return (<-chan *GameStateCell)(cells), nil
}
func (state *GameState) Mutate() {
2012-12-12 03:50:47 +00:00
randX := rand.Intn(state.Width())
randY := rand.Intn(state.Height())
cell := state.Get(randX, randY)
state.Set(randX, randY, int(math.Abs(float64(cell.Value-1))))
2012-12-12 03:50:47 +00:00
}
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++ {
2012-12-26 15:58:42 +00:00
stringVal := deadCell
2012-12-12 03:50:47 +00:00
cell := state.Get(x, y)
if cell.Value == 1 {
2012-12-26 15:58:42 +00:00
stringVal = liveCell
2012-12-12 03:50:47 +00:00
}
cells = append(cells, stringVal)
}
rows = append(rows, strings.Join(cells, ""))
}
return strings.Join(rows, "\n")
}
func (state *GameState) Image(xMult, yMult int) (*image.Gray, error) {
img := image.NewGray(image.Rect(0, 0,
(state.Height()*xMult)+1, (state.Width()*yMult)+1))
draw.Draw(img, img.Bounds(), &image.Uniform{color.Gray16{0xff}},
image.ZP, draw.Src)
cells, err := state.Cells()
if err != nil {
return nil, err
}
black := &image.Uniform{color.Black}
white := &image.Uniform{color.White}
for cell := range cells {
color := white
if cell.Value == 1 {
color = black
}
square := image.Rect((cell.X*xMult)+1, (cell.Y*yMult)+1,
(cell.X*xMult)+xMult, (cell.Y*yMult)+yMult)
draw.Draw(img, square, color, image.ZP, draw.Src)
}
return img, nil
}