PDA

View Full Version : MXS Game Of Life


MatanH
06-14-2008, 10:37 AM
Evolution!
Just another toy I've been playing with in my spare time.
Based on John Conway's Game of Life (http://en.wikipedia.org/wiki/Conway%27s_Game_of_Life), but I changed the rules a bit
and wanted to share with you guys my discovery:


http://www.halbertism.com/Stuff/GameOfLife/GameOfLife.jpg (http://www.halbertism.com/Stuff/GameOfLife/GameOfLife_01.ms)
(Click on the Image to save the ms file)


--******************************************************************************************

-- Script: Game of Life
-- Writer: Matan Halberstadt
-- Date: Saturday 13:13, 14 June 2008.
-- Version: 0.1
-- Contact: halbertism@gmail.com

--******************************************************************************************

try destroyDialog roGameOfLife catch ()
rollout roGameOfLife "Game Of Life"
(
-- Local Variable Declerations
------------------------------------------

local boardWidth = 50
local boardHeight = 30
local boxSize = 2
local startSpeed = 0
local boardMatrix
local nextBoardMatrix

-- User Interface
------------------------------------------

bitmap bmBoard "" width:(boardWidth * (boxSize + 1) + 1) height:(boardHeight * (boxSize + 1) + 1)
slider slSpeed "Speed:" range:[0,1000,startSpeed] width:(boardWidth * (boxSize + 1))
slider slThresh "Threshold:" range:[1,1000,1] width:(boardWidth * (boxSize + 1))
spinner spGen "Generation:" range:[0,100000,0] enabled:false type:#integer fieldWidth:50 align:#center
spinner spPop "Population:" range:[0,100000,0] enabled:false type:#integer fieldWidth:50 align:#center
group "Neighbors:" (
spinner spMin "Min:" range:[0,7,1] type:#integer fieldWidth:30 align:#left
spinner spMax "Max:" range:[1,8,4] type:#integer fieldWidth:30 align:#right offset:[0,-20]
)
timer tmClock interval:startSpeed active:false
button bnPlay "Play" width:(boardWidth * (boxSize + 1))
button bnReset "Reset" width:(boardWidth * (boxSize + 1))

-- Functions
------------------------------------------

fn initBoard =
(
boardMatrix = #()
for y = 1 to boardHeight do (
append boardMatrix #()
for x = 1 to boardWidth do (
local val = random 1 10
append boardMatrix[y] (if val == 1 then 1.0 else 0.0)
)
)
)

fn mainFunc x y =
(
if mod x (boxSize + 1) == 0 then white / 3 else (
if mod y (boxSize + 1) == 0 then white / 3 else (
local val = boardMatrix[1 + y / (boxSize + 1)][1 + x / (boxSize + 1)]
if val <= slThresh.value then (
green * val / slThresh.value
) else (
if val <= slThresh.value * 2 then (
val = (val - slThresh.value) / (slThresh.value)
yellow * val + green * (1 - val)
) else (
if val <= slThresh.value * 3 then (
val = (val - slThresh.value * 2) / (slThresh.value)
red * val + yellow * (1 - val)
) else red
)
)
)
)
)

fn updateBoard =
(
local newBitmap = bitmap bmBoard.width bmBoard.height color:white
for y = 1 to bmBoard.height - 1 do (
local pixelArr = #()
for x = 1 to bmBoard.width - 1 do (
local pixelColor = mainFunc x y
append pixelArr pixelColor
) -- end x loop
setPixels newBitmap [1,y] pixelArr
) -- end y loop
bmBoard.bitmap = newBitmap
) -- end updateBoard fn

fn getNeighborsCnt x y =
(
local neighborsCnt = 0
for b = (mod (boardHeight + y - 2) boardHeight) to (mod (boardHeight + y) boardHeight) do (
for a = (mod (boardWidth + x - 2) boardWidth) to (mod (boardWidth + x) boardWidth) do (
if not (a + 1 == x and b + 1 == y) then (
if boardMatrix[b + 1][a + 1] > 0 then neighborsCnt += 1
)
)
)
neighborsCnt
)

fn nextStep =
(
nextBoardMatrix = #()
local pop = 0
for y = 1 to boardHeight do (
append nextBoardMatrix #()
for x = 1 to boardWidth do (
if boardMatrix[y][x] > 0 then pop += 1
append nextBoardMatrix[y] boardMatrix[y][x]
local neighbors = getNeighborsCnt x y
if neighbors > spMin.value and neighbors < spMax.value and not random 1 10 == 1 then (
nextBoardMatrix[y][x] += 1
) else (
nextBoardMatrix[y][x] -= 2
if nextBoardMatrix[y][x] < 0 then nextBoardMatrix[y][x] = 0
)
)
)
boardMatrix = nextBoardMatrix
spGen.value += 1
spPop.value = pop
updateBoard()
)

fn setSpeed val =
(
tmClock.interval = val
)

fn play =
(
if tmClock.active then (
bnPlay.caption = "Play"
tmClock.active = false
) else (
bnPlay.caption = "Pause"
tmClock.active = true
)
)

fn openDialog =
(
createDialog roGameOfLife width:(boardWidth * (boxSize + 1) + 10)
)

fn init =
(
tmClock.active = false
spGen.value = 0
spPop.value = 0
bnPlay.caption = "Play"
initBoard()
updateBoard()
)

fn done =
(
-- cleanup code
gc light:true
)

-- Event Handlers
------------------------------------------

on tmClock tick do nextStep()
on slSpeed changed val do setSpeed val
on bnPlay pressed do play()
on bnReset pressed do init()

on roGameOfLife open do init()
on roGameOfLife close do done()

) -- end of rollout

roGameOfLife.openDialog()

MoonDoggie
06-14-2008, 04:09 PM
that is really cool, I just read all about it on wikipedia.

Kramsurfer
06-17-2008, 03:31 PM
Very interesting... can we get the two player version? ;-)

http://en.wikipedia.org/wiki/Conway%27s_Game_of_Life

Fabiomussarela
06-18-2008, 01:31 PM
Awesome :)

flyingaway
03-03-2009, 03:25 AM
so cool ,but read the code is hard for me , I am a beginner

CGTalk Moderation
03-03-2009, 03:25 AM
This thread has been automatically closed as it remained inactive for 12 months. If you wish to continue the discussion, please create a new thread in the appropriate forum.