Plugging stuff into command-line runner

including adding random initial state generation.
This commit is contained in:
Dan Buch 2012-12-09 23:20:43 -05:00
parent 32095ef057
commit 00b750e2b0
3 changed files with 130 additions and 18 deletions

View File

@ -2,6 +2,7 @@ package conway_test
import ( import (
"fmt" "fmt"
"strings"
"testing" "testing"
) )
@ -12,15 +13,21 @@ import (
var TEST_SIMPLE_INITIAL_STATE = NewGameState(16, 16) var TEST_SIMPLE_INITIAL_STATE = NewGameState(16, 16)
func init() { func init() {
tsis := TEST_SIMPLE_INITIAL_STATE s := TEST_SIMPLE_INITIAL_STATE
tsis.Set(0, 0, 1) s.Set(0, 0, 1)
tsis.Set(0, 1, 1) s.Set(0, 1, 1)
tsis.Set(0, 2, 1) s.Set(0, 2, 1)
tsis.Set(1, 0, 1) s.Set(1, 0, 1)
tsis.Set(1, 1, 1) s.Set(1, 1, 1)
tsis.Set(2, 2, 1) s.Set(2, 2, 1)
tsis.Set(3, 0, 1) s.Set(3, 0, 1)
tsis.Set(3, 2, 1) s.Set(3, 2, 1)
s.Set(13, 13, 1)
s.Set(13, 14, 1)
s.Set(14, 14, 1)
s.Set(14, 15, 1)
s.Set(15, 13, 1)
s.Set(15, 15, 1)
} }
func TestLiveCellWithFewerThanTwoLiveNeighborsDies(t *testing.T) { func TestLiveCellWithFewerThanTwoLiveNeighborsDies(t *testing.T) {
@ -179,6 +186,60 @@ func TestGameStateCanImportState(t *testing.T) {
} }
} }
func TestGameOfLifeCanImportRandomState(t *testing.T) {
game := NewGameOfLife(16, 16)
err := game.ImportRandomState()
if err != nil {
t.Error(err)
return
}
zeroCount := 0
for y := 0; y < 16; y++ {
for x := 0; x < 16; x++ {
value, err := game.State.Get(x, y)
if err != nil {
t.Error(err)
return
}
if value == 0 {
zeroCount++
}
}
}
if zeroCount == 256 {
t.Fail()
}
}
func TestGameStateCanDisplayItself(t *testing.T) {
state := NewGameState(16, 16)
err := state.Import(TEST_SIMPLE_INITIAL_STATE)
if err != nil {
t.Error(err)
return
}
grid := fmt.Sprintf("%s\n", state)
lenGrid := len(grid)
if lenGrid < 256 {
t.Errorf("%v < 256", lenGrid)
return
}
nLines := strings.Count(grid, "\n")
if nLines < 16 {
t.Errorf("%v < 16 (lines)", nLines)
return
}
}
func TestNewGameStatesAreAllDead(t *testing.T) { func TestNewGameStatesAreAllDead(t *testing.T) {
state := NewGameState(4, 4) state := NewGameState(4, 4)
for x := 0; x < 3; x++ { for x := 0; x < 3; x++ {

View File

@ -1,5 +1,27 @@
package main package main
import (
"fmt"
"os"
"time"
)
import (
. "github.com/meatballhat/box-o-sand/conway/go"
)
func main() { func main() {
return game := NewGameOfLife(40, 40)
err := game.ImportRandomState()
if err != nil {
fmt.Fprintf(os.Stderr, "WHAT IN FAIL?: %v\n", err)
os.Exit(2)
}
ticks := time.Tick(1 * time.Second)
for now := range ticks {
fmt.Printf("\n\n%v\n", now)
fmt.Println(game)
game.EvaluateGeneration()
}
} }

View File

@ -3,6 +3,7 @@ package conway
import ( import (
"errors" "errors"
"fmt" "fmt"
"math/rand"
"strings" "strings"
) )
@ -117,8 +118,8 @@ func (game *GameOfLife) EvaluateGeneration() error {
genScore := NewGenerationScoreCard(height, width) genScore := NewGenerationScoreCard(height, width)
genScore.Calculate(game.State) genScore.Calculate(game.State)
for y := 0; y < height-1; y++ { for y := 0; y < height; y++ {
for x := 0; x < width-1; x++ { for x := 0; x < width; x++ {
score, err := genScore.Get(x, y) score, err := genScore.Get(x, y)
if err != nil { if err != nil {
return err return err
@ -172,18 +173,46 @@ func (game *GameOfLife) ImportState(state *GameState) error {
return game.State.Import(state) return game.State.Import(state)
} }
func (game *GameOfLife) String() string { func (game *GameOfLife) ImportRandomState() error {
var rows []string
height := game.State.Height() height := game.State.Height()
width := game.State.Width() width := game.State.Width()
if height < 0 || width < 0 {
errStr := fmt.Sprintf("current game has invalid dimensions! %vx%v",
height, width)
return errors.New(errStr)
}
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
}
}
}
return game.ImportState(randState)
}
func (game *GameOfLife) String() string {
return fmt.Sprintf("%s\n", game.State)
}
func (state *GameState) String() string {
var rows []string
height := state.Height()
width := state.Width()
for y := 0; y < height; y++ { for y := 0; y < height; y++ {
var cells []string var cells []string
for x := 0; x < width; x++ { for x := 0; x < width; x++ {
stringVal := "X" stringVal := "X"
value, err := game.State.Get(x, y) value, err := state.Get(x, y)
if err != nil { if err != nil {
return "" return ""
} }
@ -212,8 +241,8 @@ func NewGenerationScoreCard(height, width int) *GenerationScoreCard {
func (genScore *GenerationScoreCard) Calculate(state *GameState) error { func (genScore *GenerationScoreCard) Calculate(state *GameState) error {
stateHeight, stateWidth := state.Height(), state.Width() stateHeight, stateWidth := state.Height(), state.Width()
for y := 0; y < stateHeight-1; y++ { for y := 0; y < stateHeight; y++ {
for x := 0; x < stateWidth-1; x++ { for x := 0; x < stateWidth; x++ {
value, err := state.Get(x, y) value, err := state.Get(x, y)
if err != nil { if err != nil {
return err return err