Ensuring the game can display itself
albeit in very simple string form.
This commit is contained in:
parent
ffb8d1e43c
commit
32095ef057
@ -1,6 +1,7 @@
|
|||||||
package conway_test
|
package conway_test
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"fmt"
|
||||||
"testing"
|
"testing"
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -113,6 +114,24 @@ func TestDeadCellWithExactlyThreeLiveNeighborsBecomesAlive(t *testing.T) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestGameOfLifeCanDisplayItself(t *testing.T) {
|
||||||
|
game := NewGameOfLife(16, 16)
|
||||||
|
|
||||||
|
err := game.ImportState(TEST_SIMPLE_INITIAL_STATE)
|
||||||
|
if err != nil {
|
||||||
|
t.Error(err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
grid := fmt.Sprintf("%s\n", game)
|
||||||
|
|
||||||
|
gridLen := len(grid)
|
||||||
|
if gridLen < 256 {
|
||||||
|
t.Errorf("%v < 256", gridLen)
|
||||||
|
t.Fail()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func TestNewGameOfLifeHasCorrectDimensions(t *testing.T) {
|
func TestNewGameOfLifeHasCorrectDimensions(t *testing.T) {
|
||||||
game := NewGameOfLife(16, 16)
|
game := NewGameOfLife(16, 16)
|
||||||
if game.State.Height() != 16 || game.State.Width() != 16 {
|
if game.State.Height() != 16 || game.State.Width() != 16 {
|
||||||
|
@ -3,6 +3,7 @@ package conway
|
|||||||
import (
|
import (
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"strings"
|
||||||
)
|
)
|
||||||
|
|
||||||
type GameOfLife struct {
|
type GameOfLife struct {
|
||||||
@ -171,6 +172,32 @@ func (game *GameOfLife) ImportState(state *GameState) error {
|
|||||||
return game.State.Import(state)
|
return game.State.Import(state)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (game *GameOfLife) String() string {
|
||||||
|
var rows []string
|
||||||
|
|
||||||
|
height := game.State.Height()
|
||||||
|
width := game.State.Width()
|
||||||
|
|
||||||
|
for y := 0; y < height; y++ {
|
||||||
|
var cells []string
|
||||||
|
for x := 0; x < width; x++ {
|
||||||
|
stringVal := "X"
|
||||||
|
|
||||||
|
value, err := game.State.Get(x, y)
|
||||||
|
if err != nil {
|
||||||
|
return ""
|
||||||
|
}
|
||||||
|
|
||||||
|
if value == 0 {
|
||||||
|
stringVal = "_"
|
||||||
|
}
|
||||||
|
cells = append(cells, stringVal)
|
||||||
|
}
|
||||||
|
rows = append(rows, strings.Join(cells, ""))
|
||||||
|
}
|
||||||
|
return strings.Join(rows, "\n")
|
||||||
|
}
|
||||||
|
|
||||||
func NewGenerationScoreCard(height, width int) *GenerationScoreCard {
|
func NewGenerationScoreCard(height, width int) *GenerationScoreCard {
|
||||||
genScore := &GenerationScoreCard{}
|
genScore := &GenerationScoreCard{}
|
||||||
for i := 0; i < height; i++ {
|
for i := 0; i < height; i++ {
|
||||||
|
Loading…
Reference in New Issue
Block a user