
-----------------------------------
zylum
Mon Oct 25, 2004 7:08 pm

game of life
-----------------------------------
this really simple algorithm was first developed by john conway and what it basically does is simulate cell reproduction in a very simple manner... there are only a few rules which govern this simple organism:

For a space that is 'populated':
Each cell with one or no neighbors dies, as if by loneliness. 
Each cell with four or more neighbors dies, as if by overpopulation. 
Each cell with two or three neighbors survives. 

For a space that is 'empty' or 'unpopulated'
Each cell with three neighbors becomes populated. 

the patterns these cells form are really interesting to look at. after a while you may notice that the cells go into equillibrium and either stop moving or repeat a simple pattern.

%THE GAME OF LIFE%

const maxX := 50    %maxx
const maxY := 50    %maxy
const res := 5      %how big the cells are
const density := 4  %the odds a particular cell will be occupied at the start of the proggy
const border := 10  %the amount of empty cells surrounding the border
const fps := 10   %the amount of frames per second

var t : int

var life : array 0 .. maxX + 1, 0 .. maxY + 1 of int
var temp : array 0 .. maxX + 1, 0 .. maxY + 1 of int

proc loadRandom (d, c : int)
    for x : 0 .. maxX + 1
        for y : 0 .. maxY + 1
            if x > c and x < maxX - c and y > c and y < maxY - c then
                if x = 0 or y = 0 or x = maxX + 1 or y = maxY + 1 then
                    life (x, y) := 0
                elsif Rand.Int (0, d) = 0 then
                    life (x, y) := 1
                else
                    life (x, y) := 0
                end if
            else
                life (x, y) := 0
            end if
            temp (x, y) := life (x, y)
        end for
    end for
end loadRandom

fcn howMany (x : int, y : int) : int
    result temp (x - 1, y + 1) + temp (x, y + 1) + temp (x + 1, y + 1) + temp (x - 1, y) + temp (x + 1, y) + temp (x - 1, y - 1) + temp (x, y - 1) + temp (x + 1, y - 1)
end howMany

proc FPS (fps : int)
    if Time.Elapsed - t < 1000 / fps then
        delay (round ((1000 / fps) - (Time.Elapsed - t)))
    end if
end FPS

loadRandom (density, border)

setscreen ("graphics:" + intstr (maxX * res + 50) + ";" + intstr (maxY * res + 50) + ", offscreenonly, nobuttonbar")
loop
    t := Time.Elapsed
    for x : 1 .. maxX
        for y : 1 .. maxY
            temp (x, y) := life (x, y)
            drawfillbox (x * res + 25 - res, y * res + 25 - res, x * res + 25, y * res + 25, life (x, y))
        end for
    end for
    for x : 1 .. maxX
        for y : 1 .. maxY
            if life (x, y) = 1 then
                if howMany (x, y) = 4 then
                    life (x, y) := 0
                end if
            else
                if howMany (x, y) = 3 then
                    life (x, y) := 1
                end if
            end if
        end for
    end for
    drawbox (25, 25, maxX * res + 25, maxY * res + 25, 7)
    View.Update
    FPS (fps)
end loop


-zylum

-----------------------------------
Catalyst
Mon Oct 25, 2004 9:22 pm


-----------------------------------
nice work :D

you might want to add a way to edit the initial state, since there are really cool things you can do with this

-----------------------------------
gigaman
Tue Oct 26, 2004 7:51 am

familiar
-----------------------------------
I saw this program in a programming contest once

-----------------------------------
zylum
Tue Oct 26, 2004 2:55 pm


-----------------------------------
catalyst: yeah is was thinking about that but it was late last night so i didnt do it  :x  :wink:

-----------------------------------
djlenny_3000
Tue Oct 26, 2004 7:20 pm


-----------------------------------
pretty kolllooking, could it be possible to do better resolution but not make the screen smaller, i went through the code, i got most of it ,sort of, and i looked at your variables and i just dont get it, how would you make it so that it had better res

-----------------------------------
zylum
Tue Oct 26, 2004 10:14 pm


-----------------------------------
if you make the  res constant smaller and the maxX and maxY bigger then you'll get a better resolution... i kept it at a low resolution because my computer sucks  :wink:

-----------------------------------
gigaman
Wed Oct 27, 2004 11:37 am


-----------------------------------
What is the maximum frame rate?

-----------------------------------
zylum
Wed Oct 27, 2004 8:22 pm


-----------------------------------
thats up to how fast your computer is... remember, the program does maxX*maxY calculations per cycle plus it has to draw all the cells so it may go a bit slow... really i added the fps in case you wanted to slow things down  :wink:

-----------------------------------
Mazer
Wed Oct 27, 2004 8:47 pm


-----------------------------------
Very nice for something with turing. Would've been nice if you could have a bigger resolution and still have a good framerate so it could show some things like gliders and stuff. Still, cool stuff.

-----------------------------------
Velocity
Thu Dec 22, 2011 9:55 am

RE:game of life
-----------------------------------
I dont get it, what is it?

-----------------------------------
Insectoid
Thu Dec 22, 2011 10:08 am

Re: RE:game of life
-----------------------------------
I dont get it, what is it?

It's 7 years old is what it is. Try reading the description in the first post?
