Skip to content

Commit

Permalink
1. Simplify the way of initializing units.
Browse files Browse the repository at this point in the history
  • Loading branch information
messi-yang committed Jun 3, 2022
1 parent f00e8ae commit b5f6a03
Show file tree
Hide file tree
Showing 10 changed files with 191 additions and 156 deletions.
20 changes: 16 additions & 4 deletions develop_tools.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,18 @@ var initialUnitForTest unitForTest = unitForTest{
hasLiveCell: false,
}

func generateInitialUnitMatrixForTest(width int, height int, unit unitForTest) *[][]unitForTest {
unitMatrix := make([][]unitForTest, width)
for x := 0; x < width; x += 1 {
unitMatrix[x] = make([]unitForTest, height)
for y := 0; y < height; y += 1 {
unitMatrix[x][y] = unit
}
}

return &unitMatrix
}

func defauUnitForTestIterator(coord *Coordinate, unit *unitForTest, getAdjacentUnit AdjacentUnitGetter[unitForTest]) *unitForTest {
newUnit := *unit

Expand Down Expand Up @@ -54,12 +66,12 @@ func areTwoUnitsHavingLiveCellForTestEqual(a unitsHavingLiveCellForTest, b units
return true
}

func convertUnitForTestMatrixToUnitsHavingLiveCellForTest(g [][]*unitForTest) *unitsHavingLiveCellForTest {
func convertUnitForTestMatrixToUnitsHavingLiveCellForTest(g *[][]unitForTest) *unitsHavingLiveCellForTest {
gMap := make(unitsHavingLiveCellForTest, 0)
for x := 0; x < len(g); x++ {
for x := 0; x < len(*g); x++ {
gMap = append(gMap, []bool{})
for y := 0; y < len(g[x]); y++ {
gMap[x] = append(gMap[x], g[x][y].hasLiveCell)
for y := 0; y < len((*g)[x]); y++ {
gMap[x] = append(gMap[x], (*g)[x][y].hasLiveCell)
}
}

Expand Down
5 changes: 4 additions & 1 deletion develop_tools_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package ggol

import (
"fmt"
"testing"
)

Expand Down Expand Up @@ -32,9 +33,11 @@ func TestHasLiveCellTestUnitsMapsEqual(t *testing.T) {
}

func testConvertTestUnitsMatricToHasLiveCellTestUnitsMapCaseOne(t *testing.T) {
game, _ := NewGame(&Size{2, 2}, &unitForTest{hasLiveCell: true})
uniMatrix := generateInitialUnitMatrixForTest(2, 2, unitForTest{hasLiveCell: true})
game, _ := NewGame(uniMatrix)
game.SetNextUnitGenerator(defauUnitForTestIterator)
allUnits := game.GetUnits()
fmt.Println(allUnits)
liveUnitsMap := convertUnitForTestMatrixToUnitsHavingLiveCellForTest(allUnits)

expectedMap := unitsHavingLiveCellForTest{{true, true}, {true, true}}
Expand Down
20 changes: 16 additions & 4 deletions example/conways_game_of_life.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,18 @@ func conwaysGameOfLifeNextUnitGenerator(
}
}

func initializeConwaysGameOfLifeUnits(g ggol.Game[conwaysGameOfLifeUnit]) {
func generateInitialConwaysGameOfLifeUnits(width int, height int, unit conwaysGameOfLifeUnit) *[][]conwaysGameOfLifeUnit {
units := make([][]conwaysGameOfLifeUnit, width)
for x := 0; x < width; x += 1 {
units[x] = make([]conwaysGameOfLifeUnit, height)
for y := 0; y < height; y += 1 {
units[x][y] = unit
}
}
return &units
}

func setConwaysGameOfLifeUnits(g ggol.Game[conwaysGameOfLifeUnit]) {
size := g.GetSize()
for i := 0; i < size.Height; i += 1 {
for j := 0; j < size.Height; j += 1 {
Expand All @@ -77,10 +88,11 @@ func drawConwaysGameOfLifeUnit(coord *ggol.Coordinate, unit *conwaysGameOfLifeUn
}

func executeGameOfLife() {
size := ggol.Size{Width: 50, Height: 50}
game, _ := ggol.NewGame(&size, &initialConwaysGameOfLifeUnit)
initialUnits := generateInitialConwaysGameOfLifeUnits(50, 50, initialConwaysGameOfLifeUnit)
game, _ := ggol.NewGame(initialUnits)
size := game.GetSize()
game.SetNextUnitGenerator(conwaysGameOfLifeNextUnitGenerator)
initializeConwaysGameOfLifeUnits(game)
setConwaysGameOfLifeUnits(game)

var conwaysGameOfLifePalette = []color.Color{
color.RGBA{0x00, 0x00, 0x00, 0xff},
Expand Down
20 changes: 16 additions & 4 deletions example/game_of_black_and_white.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,18 @@ func gameOfBlackAndWhiteNextUnitGenerator(
}
}

func initializeGameOfBlackAndWhiteUnits(g ggol.Game[gameOfBlackAndWhiteUnit]) {
func generateInitialGameOfBlackAndWhiteUnit(width int, height int, unit gameOfBlackAndWhiteUnit) *[][]gameOfBlackAndWhiteUnit {
units := make([][]gameOfBlackAndWhiteUnit, width)
for x := 0; x < width; x += 1 {
units[x] = make([]gameOfBlackAndWhiteUnit, height)
for y := 0; y < height; y += 1 {
units[x][y] = unit
}
}
return &units
}

func setGameOfBlackAndWhiteUnits(g ggol.Game[gameOfBlackAndWhiteUnit]) {
size := g.GetSize()
for x := 0; x < size.Width; x++ {
for y := 0; y < size.Height; y++ {
Expand All @@ -53,10 +64,11 @@ func drawGameOfBlackAndWhiteUnit(coord *ggol.Coordinate, unit *gameOfBlackAndWhi
}

func executeGameOfBlackAndWhite() {
size := ggol.Size{Width: 50, Height: 50}
game, _ := ggol.NewGame(&size, &initialGameOfBlackAndWhiteUnit)
units := generateInitialGameOfBlackAndWhiteUnit(50, 50, initialGameOfBlackAndWhiteUnit)
game, _ := ggol.NewGame(units)
size := game.GetSize()
game.SetNextUnitGenerator(gameOfBlackAndWhiteNextUnitGenerator)
initializeGameOfBlackAndWhiteUnits(game)
setGameOfBlackAndWhiteUnits(game)

var gameOfBlackAndWhitePalette = []color.Color{
color.RGBA{0x00, 0x00, 0x00, 0xff},
Expand Down
16 changes: 14 additions & 2 deletions example/game_of_king.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,17 @@ func gameOfKingNextUnitGenerator(
return &newUnit
}

func generateInitialGameOfKingUnit(width int, height int, unit gameOfKingUnit) *[][]gameOfKingUnit {
units := make([][]gameOfKingUnit, width)
for x := 0; x < width; x += 1 {
units[x] = make([]gameOfKingUnit, height)
for y := 0; y < height; y += 1 {
units[x][y] = unit
}
}
return &units
}

func initializeGameOfKingUnits(g ggol.Game[gameOfKingUnit]) {
size := g.GetSize()
cellsCount := int((size.Width * size.Height) / 2)
Expand All @@ -71,8 +82,9 @@ func drawGameOfKingUnit(coord *ggol.Coordinate, unit *gameOfKingUnit, blockSize
}

func executeGameOfKing() {
size := ggol.Size{Width: 250, Height: 250}
game, _ := ggol.NewGame(&size, &initialGameOfKingUnit)
initialUnits := generateInitialGameOfKingUnit(250, 250, initialGameOfKingUnit)
game, _ := ggol.NewGame(initialUnits)
size := game.GetSize()
game.SetNextUnitGenerator(gameOfKingNextUnitGenerator)
initializeGameOfKingUnits(game)

Expand Down
16 changes: 14 additions & 2 deletions example/game_of_matrix.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,17 @@ func gameOfMatrixNextUnitGenerator(
}
}

func generateInitialGameOfMatrixUnit(width int, height int, unit gameOfMatrixUnit) *[][]gameOfMatrixUnit {
units := make([][]gameOfMatrixUnit, width)
for x := 0; x < width; x += 1 {
units[x] = make([]gameOfMatrixUnit, height)
for y := 0; y < height; y += 1 {
units[x][y] = unit
}
}
return &units
}

func initializeGameOfMatrixUnits(g ggol.Game[gameOfMatrixUnit]) {
// Do nothing
}
Expand All @@ -77,8 +88,9 @@ func drawGameOfMatrixUnit(coord *ggol.Coordinate, unit *gameOfMatrixUnit, blockS
}

func executeGameOfMatrix() {
size := ggol.Size{Width: 50, Height: 50}
game, _ := ggol.NewGame(&size, &initialGameOfMatrixUnit)
initialUnits := generateInitialGameOfMatrixUnit(50, 50, initialGameOfMatrixUnit)
game, _ := ggol.NewGame(initialUnits)
size := game.GetSize()
game.SetNextUnitGenerator(gameOfMatrixNextUnitGenerator)
initializeGameOfMatrixUnits(game)

Expand Down
16 changes: 14 additions & 2 deletions example/game_of_wave.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,17 @@ func gameOfWaveNextUnitGenerator(
}
}

func generateInitialGameOfWaveUnit(width int, height int, unit gameOfWaveUnit) *[][]gameOfWaveUnit {
units := make([][]gameOfWaveUnit, width)
for x := 0; x < width; x += 1 {
units[x] = make([]gameOfWaveUnit, height)
for y := 0; y < height; y += 1 {
units[x][y] = unit
}
}
return &units
}

func initializeGameOfWaveUnits(g ggol.Game[gameOfWaveUnit]) {
var margin int = 0
size := g.GetSize()
Expand Down Expand Up @@ -62,8 +73,9 @@ func drawGameOfWaveUnit(coord *ggol.Coordinate, unit *gameOfWaveUnit, blockSize
}

func executeGameOfWave() {
size := ggol.Size{Width: 50, Height: 50}
game, _ := ggol.NewGame(&size, &initialGameOfWaveUnit)
initialUnits := generateInitialGameOfWaveUnit(50, 50, initialGameOfWaveUnit)
game, _ := ggol.NewGame(initialUnits)
size := game.GetSize()
game.SetNextUnitGenerator(gameOfWaveNextUnitGenerator)
initializeGameOfWaveUnits(game)

Expand Down
Loading

0 comments on commit b5f6a03

Please sign in to comment.