I needed something to do, so I wrote Conway's Game of life. It runs really slowly, and it's tiny (the window.), but it works properly! ^.^
If anyone has any ideas on how to make it run faster, please let me know. I'm thinking of using pic.new and pic.scale to make it bigger, but then I'd need to create variables for each generation's picture and scaled picture...
I'm also thinking of trying to write a 3D version using vpython, but that'll be really slow and confusing
Oh well, here's the code
Turing: |
% Conway's Game of life!
% Coded by Lapsus Antepedis in 2005
var life : int := Window.Open ("graphics:200;200, title:Conway's Game of Life, noscrollbars")
View.Set ("offscreenonly")
%var stats : int := Window.Open ("graphics:300;400, title:Stats")
%% Some code for loading a picture to start with
% var pictor : int := Pic.FileNew ("conway.bmp")
% Pic.Draw (pictor, 0, 0, picCopy)
% Pic.Free (pictor)
type organism :
record
populated : boolean
buddies : int
end record
var cell : array 0 .. maxx, 0 .. maxy of organism
%var alive, died, born : int := 0
proc drawcells
% alive := 0
Window.SetActive (life )
for x : 0 .. maxx
for y : 0 .. maxy
if cell (x, y ).populated then
drawdot (x, y, 7)
% alive += 1
else
drawdot (x, y, 0)
end if
end for
end for
View.Update
end drawcells
proc buddycount
% empties array
for x : 0 .. maxx
for y : 0 .. maxy
cell (x, y ).buddies := 0
end for
end for
%counts buddies
for x : 2 .. maxx - 2
for y : 2 .. maxy - 2
if cell (x + 1, y ).populated then
cell (x, y ).buddies + = 1
end if
if cell (x - 1, y ).populated then
cell (x, y ).buddies + = 1
end if
if cell (x, y + 1).populated then
cell (x, y ).buddies + = 1
end if
if cell (x, y - 1).populated then
cell (x, y ).buddies + = 1
end if
if cell (x + 1, y + 1).populated then
cell (x, y ).buddies + = 1
end if
if cell (x - 1, y - 1).populated then
cell (x, y ).buddies + = 1
end if
if cell (x + 1, y - 1).populated then
cell (x, y ).buddies + = 1
end if
if cell (x - 1, y + 1).populated then
cell (x, y ).buddies + = 1
end if
end for
end for
end buddycount
proc cycle
% died := 0
% born := 0
for x : 0 .. maxx
for y : 0 .. maxy
%if populated
if cell (x, y ).populated then
%with less than 2 buddies
if cell (x, y ).buddies <= 1 then
%die
cell (x, y ).populated := false
% died += 1
%with more than 3 buddies
elsif cell (x, y ).buddies >= 4 then
%die
cell (x, y ).populated := false
% died += 1
%if 2 or 3 buddies
else
%live
cell (x, y ).populated := true
end if
end if
%if has 3 buddies
if cell (x, y ).buddies = 3 then
%live
cell (x, y ).populated := true
% born += 1
end if
end for
end for
end cycle
%% Some more code for starting with a picture
% for x : 0 .. maxx
% for y : 0 .. maxy
% if whatdotcolour (x, y) not= 0 then
% cell (x, y).populated := true
% else
% cell (x, y).populated := false
% end if
% end for
% end for
% Code for generating Random Start
for x : 0 .. maxx
for y : 0 .. maxy
if Rand.Int (1, 4) = 1 then
cell (x, y ).populated := true
else
cell (x, y ).populated := false
end if
end for
end for
%Window.SetActive (stats)
%locate (1, 1)
% put "Alive | Born | Died | Change"
% put "------+-------+-------+-------"
loop
drawcells
% Window.SetActive (stats)
% put alive : 6, "| ", born : 6, "| ", died : 6, "| ", born - died : 6
buddycount
cycle
end loop
|
|