|
|
@ -14,20 +14,16 @@ const (
|
|
|
|
LIVE_CELL = " "
|
|
|
|
LIVE_CELL = " "
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
type gameGridRow struct {
|
|
|
|
|
|
|
|
Cols []int
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
type GameState struct {
|
|
|
|
type GameState struct {
|
|
|
|
Rows []*gameGridRow
|
|
|
|
Rows []*GameStateRow
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func NewGameState(height, width int) *GameState {
|
|
|
|
func NewGameState(height, width int) *GameState {
|
|
|
|
state := &GameState{}
|
|
|
|
state := &GameState{}
|
|
|
|
for i := 0; i < height; i++ {
|
|
|
|
for i := 0; i < height; i++ {
|
|
|
|
row := &gameGridRow{}
|
|
|
|
row := &GameStateRow{Y: i}
|
|
|
|
for j := 0; j < width; j++ {
|
|
|
|
for j := 0; j < width; j++ {
|
|
|
|
row.Cols = append(row.Cols, 0)
|
|
|
|
row.Cells = append(row.Cells, 0)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
state.Rows = append(state.Rows, row)
|
|
|
|
state.Rows = append(state.Rows, row)
|
|
|
|
}
|
|
|
|
}
|
|
|
@ -42,15 +38,17 @@ func (state *GameState) Width() int {
|
|
|
|
if len(state.Rows) < 1 {
|
|
|
|
if len(state.Rows) < 1 {
|
|
|
|
return -1
|
|
|
|
return -1
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return len(state.Rows[0].Cols)
|
|
|
|
return len(state.Rows[0].Cells)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func (state *GameState) GetRow(y int) (*gameGridRow, error) {
|
|
|
|
func (state *GameState) GetRow(y int) *GameStateRow {
|
|
|
|
if y+1 > len(state.Rows) {
|
|
|
|
lenRows := len(state.Rows)
|
|
|
|
return nil, errors.New("y coordinate is out of bounds!")
|
|
|
|
|
|
|
|
|
|
|
|
if y+1 > lenRows {
|
|
|
|
|
|
|
|
return state.GetRow(y % lenRows)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return state.Rows[y], nil
|
|
|
|
return state.Rows[y]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func (state *GameState) Set(x, y, value int) error {
|
|
|
|
func (state *GameState) Set(x, y, value int) error {
|
|
|
@ -59,31 +57,25 @@ func (state *GameState) Set(x, y, value int) error {
|
|
|
|
return errors.New(errMsg)
|
|
|
|
return errors.New(errMsg)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
row, err := state.GetRow(y)
|
|
|
|
row := state.GetRow(y)
|
|
|
|
if err != nil {
|
|
|
|
|
|
|
|
return err
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if x+1 > len(row.Cols) {
|
|
|
|
if x+1 > len(row.Cells) {
|
|
|
|
errMsg := fmt.Sprintf("x coordinate %v is out of bounds!", x)
|
|
|
|
errMsg := fmt.Sprintf("x coordinate %v is out of bounds!", x)
|
|
|
|
return errors.New(errMsg)
|
|
|
|
return errors.New(errMsg)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
row.Cols[x] = value
|
|
|
|
row.Cells[x] = value
|
|
|
|
return nil
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func (state *GameState) Get(x, y int) (int, error) {
|
|
|
|
func (state *GameState) Get(x, y int) (int, error) {
|
|
|
|
row, err := state.GetRow(y)
|
|
|
|
row := state.GetRow(y)
|
|
|
|
if err != nil {
|
|
|
|
|
|
|
|
return -1, err
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if len(row.Cols) < x+1 {
|
|
|
|
if len(row.Cells) < x+1 {
|
|
|
|
return -1, errors.New("x coordinate is out of bounds!")
|
|
|
|
return -1, errors.New("x coordinate is out of bounds!")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return row.Cols[x], nil
|
|
|
|
return row.Cells[x], nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func (state *GameState) Enliven(x, y int) error {
|
|
|
|
func (state *GameState) Enliven(x, y int) error {
|
|
|
@ -96,7 +88,7 @@ func (state *GameState) Deaden(x, y int) error {
|
|
|
|
|
|
|
|
|
|
|
|
func (state *GameState) Import(other *GameState) (err error) {
|
|
|
|
func (state *GameState) Import(other *GameState) (err error) {
|
|
|
|
for y, row := range other.Rows {
|
|
|
|
for y, row := range other.Rows {
|
|
|
|
for x, cell := range row.Cols {
|
|
|
|
for x, cell := range row.Cells {
|
|
|
|
if err = state.Set(x, y, cell); err != nil {
|
|
|
|
if err = state.Set(x, y, cell); err != nil {
|
|
|
|
return err
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
@ -121,10 +113,7 @@ func (state *GameState) Cells() (<-chan *GameStateCell, error) {
|
|
|
|
width := state.Width()
|
|
|
|
width := state.Width()
|
|
|
|
|
|
|
|
|
|
|
|
for y := 0; y < height; y++ {
|
|
|
|
for y := 0; y < height; y++ {
|
|
|
|
row, err := state.GetRow(y)
|
|
|
|
row := state.GetRow(y)
|
|
|
|
if err != nil {
|
|
|
|
|
|
|
|
panic(err)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
for x := 0; x < width; x++ {
|
|
|
|
for x := 0; x < width; x++ {
|
|
|
|
value, err := state.Get(x, y)
|
|
|
|
value, err := state.Get(x, y)
|
|
|
@ -132,10 +121,10 @@ func (state *GameState) Cells() (<-chan *GameStateCell, error) {
|
|
|
|
panic(err)
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
c <- &GameStateCell{
|
|
|
|
c <- &GameStateCell{
|
|
|
|
Value: value,
|
|
|
|
Value: value,
|
|
|
|
X: x,
|
|
|
|
X: x,
|
|
|
|
Y: y,
|
|
|
|
Y: y,
|
|
|
|
cols: row.Cols,
|
|
|
|
cellmates: row.Cells,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|