Skip to content

Commit

Permalink
Game refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
qjuanp committed Aug 16, 2024
1 parent 23db2d0 commit 00b3d5f
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 18 deletions.
15 changes: 5 additions & 10 deletions game.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,29 +18,24 @@ func (game *GameOfLife) next() GameOfLife {

for rowIndex, row := range game.Board {
for columnIndex, cell := range row {
aliveNeighbors := game.countAliveNeighboards(rowIndex, columnIndex)
aliveNeighbors := game.checkAliveNeighboars(rowIndex, columnIndex)
newBoardState[rowIndex][columnIndex] = newCellState(cell, aliveNeighbors)
}
}

return GameOfLife{newBoardState}
}

func (game *GameOfLife) countAliveNeighboards(row int, column int) uint8 {
func (game *GameOfLife) checkAliveNeighboars(rowIdx int, columnIdx int) uint8 {
var aliveNeighbors uint8 = 0

for r := game.LowerBound(row); r <= game.RowsUpperBoundFor(row); r++ {
for c := game.LowerBound(column); c <= game.ColumnsUpperBoundFor(column); c++ {
// fmt.Printf("on(%d,%d)=%d", r, c, currentBoard[r][c])
// fmt.Println()
if (r != row || c != column) && game.Board[r][c] == ALIVE {
// fmt.Printf("Counted on(%d,%d)=%d", r, c, currentBoard[r][c])
// fmt.Println()
for row, hasNextRow, currentRowIndex, itRows := game.Board.IterateNeighborsOfRow(rowIdx); hasNextRow; row, hasNextRow, currentRowIndex = itRows() {
for cell, hasNextColumns, currentColumnIndex, itColumns := game.Board.IterateNightborsOf(row, columnIdx); hasNextColumns; cell, hasNextColumns, currentColumnIndex = itColumns() {
if (rowIdx != currentRowIndex || columnIdx != currentColumnIndex) && cell == ALIVE {
aliveNeighbors++
}
}
}

return aliveNeighbors
}

Expand Down
48 changes: 40 additions & 8 deletions game_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ import (
)

func detailedErrorResult(t *testing.T, boardResult board.Board, boardExpected board.Board) {
t.Errorf("Board result:\n%s\n", boardResult.ToString())
t.Errorf("Board expected:\n%s\n", boardExpected.ToString())
t.Errorf("Board result:\n%s\n", boardResult.ToNumbers())
t.Errorf("Board expected:\n%s\n", boardExpected.ToNumbers())

for rowIndex, row := range boardResult {
for columnIndex, cell := range row {
Expand Down Expand Up @@ -103,9 +103,9 @@ func TestDeadCellsWithExcatly3NeighborsShouldComeAlive(t *testing.T) {
}

expectedNextGenerationBoard := board.Board{
{DEAD, ALIVE, ALIVE},
{DEAD, ALIVE, ALIVE},
{DEAD, DEAD, DEAD},
{ALIVE, ALIVE, ALIVE},
{ALIVE, ALIVE, ALIVE},
{ALIVE, ALIVE, ALIVE},
}

game := GameOfLife{currentBoard}
Expand All @@ -126,9 +126,9 @@ func TestDeadCellsWithExcatlyAliveNeighborShouldBeDead(t *testing.T) {
}

expectedNextGenerationBoard := board.Board{
{DEAD, ALIVE, DEAD},
{DEAD, ALIVE, DEAD},
{DEAD, ALIVE, DEAD},
{ALIVE, ALIVE, ALIVE},
{ALIVE, ALIVE, ALIVE},
{ALIVE, ALIVE, ALIVE},
}

game := GameOfLife{currentBoard}
Expand All @@ -140,6 +140,38 @@ func TestDeadCellsWithExcatlyAliveNeighborShouldBeDead(t *testing.T) {
}
}

func TestAliveCellsCounterAllDead(t *testing.T) {
currentBoard := board.Board{
{DEAD, DEAD, DEAD},
{DEAD, DEAD, DEAD},
{DEAD, DEAD, DEAD},
}

game := GameOfLife{currentBoard}

aliveCells := game.checkAliveNeighboars(0, 0)

if aliveCells != 0 {
t.Error("Expected `0` alive neighboar cells")
}
}

func TestAliveCellsCounterAllAlive(t *testing.T) {
currentBoard := board.Board{
{ALIVE, ALIVE, ALIVE},
{ALIVE, ALIVE, ALIVE},
{ALIVE, ALIVE, DEAD},
}

game := GameOfLife{currentBoard}

aliveCells := game.checkAliveNeighboars(2, 2)

if aliveCells != 8 {
t.Errorf("Alive cells:%d. Expected `0` alive neighboar cells", aliveCells)
}
}

func BenchmarkIntialization(b *testing.B) {
for i := 1; i < 10; i++ {
boardSize := uint(i * 1000)
Expand Down

0 comments on commit 00b3d5f

Please sign in to comment.