the getNeighbors and isOutOfBounds function works fine as far as my tests go but for some reason the game never works as intended and i did the rules according to this:
- Any live cell with two or three live neighbours survives.
- Any dead cell with three live neighbours becomes a live cell.
- All other live cells die in the next generation. Similarly, all other dead cells stay dead.
this is the advance() function of my game of life attempt:
var grid : MutableList<MutableList<Boolean>> // initialised in the constructor
fun advance()
{
var iter = grid.iterator()
var toSet = MutableList(grid.size) { i-> iter.next()}
var dieAmount = 0
var reviveAmount = 0
for( col in 0 until grid.size)
{
for( row in 0 until grid[0].size )
{
var neighbors = getNeighborCount(grid, col, row)
if(!(neighbors == 2 || neighbors == 3))
{
toSet[col][row] = false
dieAmount++
}
if(neighbors == 3 && !grid[col][row])
{
toSet[col][row] = true
reviveAmount++
}
else
toSet[col][row] = false
}
}
//println("dieAmount: $dieAmount surviveAmount: $reviveAmount")
grid = toSet
}
and my functions:
fun getNeighborCount(grid: MutableList<MutableList<Boolean>>, column:Int, row:Int) : Int
{
var toReturn = 0
for(minorColumn in -1..1)
for(minorRow in -1..1)
if(!isOverBounds(grid,column + minorColumn, row +minorRow) && !(minorColumn == 0 && minorRow == 0))
{
if(grid[column+minorColumn][row + minorRow])
{
toReturn++
}
}
return toReturn
}
fun <T> isOverBounds(args: MutableList<MutableList<T>>, colCheck: Int, rowCheck: Int) : Boolean
{
if(args.size < colCheck+1 ||colCheck < 0) return true
if(args[0].size<rowCheck+1||rowCheck < 0) return true
return false
}
the game is initially at this state:
advancement 1:
and the sole cell disappears in the second
another example:
advancement 1:
advancement 2:




