Author Topic: Game Of Life in EGSL  (Read 2728 times)

Tomaaz

  • Guest
Game Of Life in EGSL
« on: October 26, 2013, 04:26:13 PM »
Here is a small implementation of Conway's Game of Life written in EGSL.

f - faster
s - slower
esc - quit

Code: [Select]
openwindow (600, 630, 32, "Life")
cell = createimage(10, 10)
startimagedraw(cell)
colour(0, 0, 0)
fillbox(0, 10, 10, 10)
colour(200, 200, 200)
fillcircle (5, 5, 4)
stopimagedraw(cell)

old={}
new={}

math.randomseed(os.time())

for x = 1, 60 do
old[x] = {}
for y = 1, 60 do
old[x][y] = 0
end
end

for x = 1, 60 do
new[x] = {}
for y = 1, 60 do
new[x][y] = 0
end
end

for a = 1, 1000 do
x = math.random(59)
y = math.random(59)
old[x][y] = 1
end

step = 0
speed = 250

repeat
step = step + 1
colour(0, 0, 0)
fillbox(0, 0, 599, 629)
colour(255, 255, 255)
drawtext(5, 620, "Step: ")
drawtext(50, 620, step)

for x = 1, 60 do
for y = 1, 60 do
if (old[x][y] == 1) then
putimage (x * 10, y * 10, cell)
end
end
end
redraw()

for x = 1, 60 do
for y = 1, 60 do
status = 0
a = x - 1
b = y - 1
if (a > 0) and (b > 0) and (old[a][b] == 1) then
status = status + 1
end
a = x
b = y - 1
if (b > 0) and (old[a][b] == 1) then
status = status + 1
end
a = x + 1
b = y - 1
if (a < 61) and (b > 0) and (old[a][b] == 1) then
status = status + 1
end
a = x + 1
b = y
if (a < 61) and (old[a][b] == 1) then
status = status + 1
end
a = x + 1
b = y + 1
if (a < 61) and (b < 61) and (old[a][b] == 1) then
status = status + 1
end
a = x
b = y + 1
if (b < 61) and (old[a][b] == 1) then
status = status + 1
end
a = x - 1
b = y + 1
if (a > 0) and (b < 61) and (old[a][b] == 1) then
status = status + 1
end
a = x - 1
b = y
if (a > 0) and (old[a][b] == 1) then
status = status + 1
end
if old[x][y] == 1 then
if (status == 2) or (status == 3) then
new[x][y] = 1
else
new[x][y] = 0
end
else
if (status == 3) then
new[x][y] = 1
else
new[x][y] = 0
end
end
end
end

for x = 1, 60 do
for y = 1, 60 do
old[x][y] = new[x][y]
end
end
wait(speed)
key = getkey()
if key == 102 and speed > 50 then
speed = math.floor(speed / 2)
end
if key == 115 and speed < 1000 then
speed = speed * 2
end
until key == 27
« Last Edit: October 26, 2013, 04:28:49 PM by Tomaaz »

Cybermonkey

  • Administrator
  • *****
  • Posts: 0
Re: Game Of Life in EGSL
« Reply #1 on: October 26, 2013, 04:30:16 PM »
Nice example. The interesting thing is that most games of life end with death.  ;)