Skip to content

Commit

Permalink
1. Add new methods "GetUnitsInArea" to Game interface.
Browse files Browse the repository at this point in the history
2. Add new methods "IterateUnitsInArea" to Game interface.
3. Rename method "Reset" to "ResetUnits" in Game interface.
  • Loading branch information
messi-yang committed Apr 10, 2022
1 parent 6e0f3db commit 4260bc4
Show file tree
Hide file tree
Showing 10 changed files with 290 additions and 142 deletions.
10 changes: 5 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,8 @@ func cgolNextUnitGenerator(
for j := -1; j < 2; j += 1 {
if !(i == 0 && j == 0) {
// Pay attention to "isCrossBorder", if the adjacent unit in the relative coordinate
// is on other side of the field, "isCrossBorder" will be true.
// So if you want to allow your cells to be able to go through border, ignore "isCrossBorder" here.
// is on other side of the map, "isCrossBorder" will be true.
// So if you want to allow your cells to be able to go beyond border, ignore "isCrossBorder" here.
adjUnit, isCrossBorder := getAdjacentUnit(coord, &ggol.Coordinate{X: i, Y: j})
if adjUnit.Alive && !isCrossBorder {
liveAdjacentCellsCount += 1
Expand Down Expand Up @@ -96,7 +96,7 @@ func cgolNextUnitGenerator(
}

func main() {
// Declare field size.
// Declare size of the map in the game.
size := ggol.Size{Height: 3, Width: 3}
// Initial status of all units.
initialCgolUnit := CgolUnit{Alive: false}
Expand All @@ -117,9 +117,9 @@ func main() {
game.SetUnit(&ggol.Coordinate{X: 1, Y: 1}, &CgolUnit{Alive: true})
game.SetUnit(&ggol.Coordinate{X: 1, Y: 2}, &CgolUnit{Alive: true})

// This will generate next field, the looking of next field is depending on "cgolNextUnitGenerator"
// This will generate next units, the looking of next generation of units is depending on "cgolNextUnitGenerator"
// you just passed in "SetNextUnitGenerator" above.
game.GenerateNextField()
game.GenerateNextUnits()

// Let's see if we generate the next status of the Blinker correctly.
// If it's correct, all units below should have "Alive" attribute as true.
Expand Down
4 changes: 2 additions & 2 deletions develop_tools_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@ func TestHasLiveCellTestUnitsMapsEqual(t *testing.T) {
func testConvertTestUnitsMatricToHasLiveCellTestUnitsMapCaseOne(t *testing.T) {
game, _ := NewGame(&Size{2, 2}, &unitForTest{hasLiveCell: true})
game.SetNextUnitGenerator(defauUnitForTestIterator)
generation := game.GetField()
liveUnitsMap := convertUnitForTestMatrixToUnitsHavingLiveCellForTest(generation)
allUnits := game.GetUnits()
liveUnitsMap := convertUnitForTestMatrixToUnitsHavingLiveCellForTest(allUnits)

expectedMap := unitsHavingLiveCellForTest{{true, true}, {true, true}}

Expand Down
8 changes: 4 additions & 4 deletions example/conways_game_of_life.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,8 @@ func conwaysGameOfLifeNextUnitGenerator(
}
}

func initializeConwaysGameOfLifeField(g ggol.Game[conwaysGameOfLifeUnit]) {
size := g.GetFieldSize()
func initializeConwaysGameOfLifeUnits(g ggol.Game[conwaysGameOfLifeUnit]) {
size := g.GetSize()
for i := 0; i < size.Height; i += 1 {
for j := 0; j < size.Height; j += 1 {
g.SetUnit(&ggol.Coordinate{X: i*5 + 0, Y: j*5 + 0}, &conwaysGameOfLifeUnit{HasLiveCell: true})
Expand All @@ -80,7 +80,7 @@ func executeGameOfLife() {
size := ggol.Size{Width: 50, Height: 50}
game, _ := ggol.NewGame(&size, &initialConwaysGameOfLifeUnit)
game.SetNextUnitGenerator(conwaysGameOfLifeNextUnitGenerator)
initializeConwaysGameOfLifeField(game)
initializeConwaysGameOfLifeUnits(game)

var conwaysGameOfLifePalette = []color.Color{
color.RGBA{0x00, 0x00, 0x00, 0xff},
Expand All @@ -103,7 +103,7 @@ func executeGameOfLife() {
}
images = append(images, newImage)
delays = append(delays, duration)
game.GenerateNextField()
game.GenerateNextUnits()
}

outputGif("output/conways_game_of_life.gif", images, delays)
Expand Down
8 changes: 4 additions & 4 deletions example/game_of_black_and_white.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@ func gameOfBlackAndWhiteNextUnitGenerator(
}
}

func initializeGameOfBlackAndWhiteField(g ggol.Game[gameOfBlackAndWhiteUnit]) {
size := g.GetFieldSize()
func initializeGameOfBlackAndWhiteUnits(g ggol.Game[gameOfBlackAndWhiteUnit]) {
size := g.GetSize()
for x := 0; x < size.Width; x++ {
for y := 0; y < size.Height; y++ {
c := ggol.Coordinate{X: x, Y: y}
Expand All @@ -56,7 +56,7 @@ func executeGameOfBlackAndWhite() {
size := ggol.Size{Width: 50, Height: 50}
game, _ := ggol.NewGame(&size, &initialGameOfBlackAndWhiteUnit)
game.SetNextUnitGenerator(gameOfBlackAndWhiteNextUnitGenerator)
initializeGameOfBlackAndWhiteField(game)
initializeGameOfBlackAndWhiteUnits(game)

var gameOfBlackAndWhitePalette = []color.Color{
color.RGBA{0x00, 0x00, 0x00, 0xff},
Expand All @@ -79,7 +79,7 @@ func executeGameOfBlackAndWhite() {
}
images = append(images, newImage)
delays = append(delays, duration)
game.GenerateNextField()
game.GenerateNextUnits()
}

outputGif("output/game_of_black_and_white.gif", images, delays)
Expand Down
8 changes: 4 additions & 4 deletions example/game_of_king.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,8 @@ func gameOfKingNextUnitGenerator(
return &newUnit
}

func initializeGameOfKingField(g ggol.Game[gameOfKingUnit]) {
size := g.GetFieldSize()
func initializeGameOfKingUnits(g ggol.Game[gameOfKingUnit]) {
size := g.GetSize()
cellsCount := int((size.Width * size.Height) / 2)
for i := 0; i < cellsCount; i += 1 {
g.SetUnit(&ggol.Coordinate{X: rand.Intn(size.Width), Y: rand.Intn(size.Height)}, &gameOfKingUnit{Strength: 1, Direction: 0})
Expand All @@ -74,7 +74,7 @@ func executeGameOfKing() {
size := ggol.Size{Width: 250, Height: 250}
game, _ := ggol.NewGame(&size, &initialGameOfKingUnit)
game.SetNextUnitGenerator(gameOfKingNextUnitGenerator)
initializeGameOfKingField(game)
initializeGameOfKingUnits(game)

var gameOfKingPalette = []color.Color{
color.RGBA{0x00, 0x00, 0x00, 0xff},
Expand Down Expand Up @@ -104,7 +104,7 @@ func executeGameOfKing() {
}
images = append(images, newImage)
delays = append(delays, duration)
game.GenerateNextField()
game.GenerateNextUnits()
}

outputGif("output/game_of_king.gif", images, delays)
Expand Down
26 changes: 13 additions & 13 deletions example/game_of_matrix.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,17 @@ import (
type gameOfMatrixUnit struct {
WordsLength int
CountWords int
// One column can only have a word stream at a time, so we have this count
CountFieldHeight int
// One column (height of game size) can only have a word stream at a time, so we have this count
CountHeight int
}

var initialGameOfMatrixUnit gameOfMatrixUnit = gameOfMatrixUnit{
WordsLength: 0,
CountWords: 0,
CountFieldHeight: 50,
WordsLength: 0,
CountWords: 0,
CountHeight: 50,
}

// A field can only have 20 word of streams in total
// A game can only have 20 word of streams in total
var totalWordStreamsCount = 0

func gameOfMatrixNextUnitGenerator(
Expand All @@ -31,11 +31,11 @@ func gameOfMatrixNextUnitGenerator(
) (nextUnit *gameOfMatrixUnit) {
newUnit := *unit
if coord.Y == 0 {
if unit.CountWords == 0 && unit.CountFieldHeight >= 50 && totalWordStreamsCount < 50 {
if unit.CountWords == 0 && unit.CountHeight >= 50 && totalWordStreamsCount < 50 {
if rand.Intn(50) == 1 {
newUnit.WordsLength = 30 + rand.Intn(40)
newUnit.CountWords = 1
newUnit.CountFieldHeight = 0
newUnit.CountHeight = 0
totalWordStreamsCount += 1
}
} else if unit.CountWords < unit.WordsLength {
Expand All @@ -45,7 +45,7 @@ func gameOfMatrixNextUnitGenerator(
newUnit.CountWords = 0
totalWordStreamsCount -= 1
}
newUnit.CountFieldHeight += 1
newUnit.CountHeight += 1
return &newUnit
} else {
prevUnit, _ := getAdjacentUnit(coord, &ggol.Coordinate{X: 0, Y: -1})
Expand All @@ -54,7 +54,7 @@ func gameOfMatrixNextUnitGenerator(
}
}

func initializeGameOfMatrixField(g ggol.Game[gameOfMatrixUnit]) {
func initializeGameOfMatrixUnits(g ggol.Game[gameOfMatrixUnit]) {
// Do nothing
}

Expand All @@ -80,11 +80,11 @@ func executeGameOfMatrix() {
size := ggol.Size{Width: 50, Height: 50}
game, _ := ggol.NewGame(&size, &initialGameOfMatrixUnit)
game.SetNextUnitGenerator(gameOfMatrixNextUnitGenerator)
initializeGameOfMatrixField(game)
initializeGameOfMatrixUnits(game)

previousSteps := 100
for i := 0; i < previousSteps; i += 1 {
game.GenerateNextField()
game.GenerateNextUnits()
}

var gameOfMatrixPalette = []color.Color{
Expand Down Expand Up @@ -116,7 +116,7 @@ func executeGameOfMatrix() {
}
images = append(images, newImage)
delays = append(delays, duration)
game.GenerateNextField()
game.GenerateNextUnits()
}

outputGif("output/game_of_matrix.gif", images, delays)
Expand Down
8 changes: 4 additions & 4 deletions example/game_of_wave.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,9 @@ func gameOfWaveNextUnitGenerator(
}
}

func initializeGameOfWaveField(g ggol.Game[gameOfWaveUnit]) {
func initializeGameOfWaveUnits(g ggol.Game[gameOfWaveUnit]) {
var margin int = 0
size := g.GetFieldSize()
size := g.GetSize()
for x := 0; x < size.Width; x++ {
for y := 0; y < size.Height; y++ {
if y%10 == 0 {
Expand Down Expand Up @@ -65,7 +65,7 @@ func executeGameOfWave() {
size := ggol.Size{Width: 50, Height: 50}
game, _ := ggol.NewGame(&size, &initialGameOfWaveUnit)
game.SetNextUnitGenerator(gameOfWaveNextUnitGenerator)
initializeGameOfWaveField(game)
initializeGameOfWaveUnits(game)

var gameOfWavePalette = []color.Color{
color.RGBA{0x00, 0x00, 0x00, 0xff},
Expand All @@ -88,7 +88,7 @@ func executeGameOfWave() {
}
images = append(images, newImage)
delays = append(delays, duration)
game.GenerateNextField()
game.GenerateNextUnits()
}

outputGif("output/game_of_wave.gif", images, delays)
Expand Down
Loading

0 comments on commit 4260bc4

Please sign in to comment.