LOGO-L> Conway's Game of Life
Author Message
LOGO-L> Conway's Game of Life

This example is quite a bit like the PLATE.LGO example.
My "average" function is the core of the "simulation".
It uses information from it's neighbors to determine the cell's
next state. I chose color for the data in each cell it
could be anything you want.

I also chose to use colored blocks to visually represent
the data in each. Conveniently the data WAS color. You can
do anything you want with graphics at each cell, color, line
thickness, Label, BarGraph what ever. There can also be multiple
pieces of information at each cell (like 2 Bars). You also consider
2 independent plots side by side.

Note that the boundary conditions are often where a lot of work
is involved and I cheated (see comments) to avoid complications.
One solution is to "wrap" the grid. For example if your examining cell
[10, 20] of a 20 x 20 grid you look at neighbors [9, 20], [11, 20],
[10, 19] and [10, 21]. But [10, 21] does not exist so you wrap to
[10, 1]. You can use a simple modulus function to do this. I cheated
by making the array bigger than the actual problem. These choices
do effect how well you simulation runs. Some use wrap, some put
special conditions to handle all the boundaries.

To run the example below try:

simulate 20

to average :myarray :x :y
; Average each of the 3 elements in the cell point to by :x :y
localmake "avgitem1 0
make "avgitem1 :avgitem1 + item 1 mditem (list :x   :y  ) :myarray
make "avgitem1 :avgitem1 + item 1 mditem (list :x+1 :y  ) :myarray
make "avgitem1 :avgitem1 + item 1 mditem (list :x   :y+1) :myarray
make "avgitem1 :avgitem1 + item 1 mditem (list :x-1 :y  ) :myarray
make "avgitem1 :avgitem1 + item 1 mditem (list :x   :y-1) :myarray

localmake "avgitem2 0
make "avgitem2 :avgitem2 + item 2 mditem (list :x   :y  ) :myarray
make "avgitem2 :avgitem2 + item 2 mditem (list :x+1 :y  ) :myarray
make "avgitem2 :avgitem2 + item 2 mditem (list :x   :y+1) :myarray
make "avgitem2 :avgitem2 + item 2 mditem (list :x-1 :y  ) :myarray
make "avgitem2 :avgitem2 + item 2 mditem (list :x   :y-1) :myarray

localmake "avgitem3 0
make "avgitem3 :avgitem3 + item 3 mditem (list :x   :y  ) :myarray
make "avgitem3 :avgitem3 + item 3 mditem (list :x+1 :y  ) :myarray
make "avgitem3 :avgitem3 + item 3 mditem (list :x   :y+1) :myarray
make "avgitem3 :avgitem3 + item 3 mditem (list :x-1 :y  ) :myarray
make "avgitem3 :avgitem3 + item 3 mditem (list :x   :y-1) :myarray

; output the average data as a 3 element list cell
output (list :avgitem1/5 :avgitem2/5 :avgitem3/5)
end

to initarray :gridx :gridy
; Init the array and include one cell below and one cell above
; to simplify boundary conditions. For example if we have a grid
; 20x20 the indices will be 0-21 by 0-21.
; We init the cell with a random Red, Random Green and Random Blue
localmake "myarray (mdarray (list :gridx+2 :gridy+2) 0)
repeat :gridx+2 ~
[
make "x repcount-1
repeat :gridy+2 ~
[
make "y repcount-1
mdsetitem (list :x :y) :myarray (list random 255 random 255 random 255)
]
]
output :myarray
end

to simulate :iterations
local "x
local "y
local "cell
; Define Size of a Plotted Cell
localmake "sizex 10
localmake "sizey 10
; Define the size of the Grid
localmake "gridx 10
localmake "gridy 10
; Init the Grid with Random Colors
localmake "myarray initarray :gridx :gridy
pu
ht
; We only index 1-20 here
repeat :iterations ~
[
repeat :gridx ~
[
make "x repcount
repeat :gridy ~
[
make "y repcount
; Our "simulation" here is simply an average function based on the cell's neighbors.
make "cell average :myarray :x :y
; Update the cell
mdsetitem (list :x :y) :myarray :cell
; Now plot the cell
setfc :cell
setxy :sizex*:x :sizey*:y
bitblock :sizex :sizey
]
]
]
end

Quote:

> Hi,

> I'm trying to write the source-code for Conway's Game of Life.
> I'm using MSWLogo.
> I want to use a grid (say 20 x20). How can I:
> a. randomly or at will change the contents of one cell in the grid?
> b. how can the turtle change the contents of the cells while the programming
> is running.

> If any one knows where to find a sample code would also be helpful.
> Harrie

> ---------------------------------------------------------------

--
===============================================================

http://www.*-*-*.com/
The www page contains some very powerful educational software.
Our single most important investment is our kids.
---------------------------------------------------------------

Thu, 05 Jul 2001 03:00:00 GMT

 Page 1 of 1 [ 1 post ]

Relevant Pages