Courses/CS 461/Winter 2006/Firouz Bharthania/week3
From CSWiki
[edit] Game of Life(unlimited world)
Game of Life
Here I tried to create a game of life but with no limitation for it's world.
There were two parts I hade to implement.
- being able to move the NetLogo over the virtual unlimited world
- process the game of life for each cell in the virtual world
Instead of having a two dimensinal array to represent the virtual world I placed all the alive cells as hidden turtles on the NetLogo board and as the user scrolls the world window over the virtual world the appropriate turtles set their place and show up.
For processing the game of life, the alive cells(turtles) are sorted horizontally, vertically and diagonally, and then I calculate the number of neighbor for each alive cell. As I didn't have time to finish theis part to find the number of neighbors for the death cells I have commented out the call to these procedures.
The above version of program is optimized toward memory usage and there is no need to represent the entire board as a tow dimensional array, hence it lets you to have a much much bigger world.
As I tried to implement another version of this program which uses a two dimensional nested list data structure, I came up with the following utility procedures wich makes it easy to work with the defined data structure.
sets a value for into the cell at row and col the world (data structure) expands as much as is needed. It means if we represent our live cells as turtles and add them to our data structure as they are born, the datastructre expands it's boundry if it has to in order to place the new born cell.
to set-cell [row col cell-value] if(row > ((length grid) - 1)) [ repeat (row - (length grid - 1)) [set grid lput [] grid] ] if(col > ((length item row grid) - 1)) [ repeat (col - (length (item row grid) - 1)) [set grid replace-item row grid (lput "" (item col item row grid))] ] set grid replace-item row grid (replace-item col (item row grid) cell-value) end
returns the value (turtle) at the cell row and col
to-report get-cell [row col] if(col > (length grid - 1)) [report ""] if(row > ((length item col grid) - 1)) [report ""] report item row (item col grid) end
the procedure accepts a string (command), then replaces all the words "cell" in the command with the actual cell and runs it.
to ask-cells[func]
let row 0
let col 0
repeat length grid [
repeat length item row grid [
run replace-var func "(item col (item row grid))"
set row (row + 1)
set col (col + 1)
]
]
end
to-report replace-var [src-str sub-str]
let res ""
loop [
let pos position src-str sub-str
if(pos = 0) [report word res src-str]
set res word res (substring src-str 0 pos)
set src-str substring src-str (pos + 4) (length src-str)
]
end
It's usage example can be:
In adjust_display procedure in the first version of the program we had:
ask turtles [ if(x > west and x < east and y > south and y < north) [ setxy (x - cor-x) (y - cor-y) showturtle ] ]
Now we can have something like this in our new version:
ask-cells "if(x-of cell > west and x-of cell < east and y-of cell > south and y-of cell < north) [" + "ask cell [setxy (x-of cell - cor-x) (y-of cell - cor-y)] " + " showturtle " + "]"

