Sneaky Trick #1

Paste the following script into a behaviour, hit play and watch the score come alive:

``````property myArray, mySize,  myLastCell

on beginSprite me

me.mCreateArray(40,40)

me.mRandomStart(mySize[1]*mySize[2]/5)

end

on exitFrame me

me.mPlayLife()

me.mDrawArray(myArray)

go to the frame

end

on mCreateArray (me,  aX, aY)

myArray = []

mySize = [aX, aY]

repeat with y = 1 to aY

myArray[y] = []

repeat with x = 1 to aX

myArray[y][x] = 0

end repeat

end repeat

end

on mRandomStart (me,  numCells)

repeat with cell = 1 to numcells

y = random(mySize[2])

x = random(mySize[1])

myArray[y][x] = NOT(myArray[y][x])

end repeat

end

on mPlayLife me

newState = myArray.duplicate()

repeat with y = 1 to mySize[2]

repeat with x = 1 to mySize[1]

c = me.mGetNeighborCount(x,y)

if c = 2 then

-- no change

else if c = 3 then

newState[x][y] = 1

else

newState[x][y] = 0

end if

end repeat

end repeat

myArray = newState

end

on mGetNeighborCount me, x, y

mx = mySize[1]

case(x) of

1: Nx = [mx, x, x+1]

mx: Nx = [x-1, x, 1]

otherwise

Nx = [x-1, x, x+1]

end case

my = mySize[2]

case(y) of

1: Ny = [my, y, y+1]

my: Ny = [y-1, y, 1]

otherwise

Ny = [y-1, y, y+1]

end case

---

tally = myArray[Nx[1]][Ny[1]] + myArray[Nx[1]][Ny[2]] + myArray[Nx[1]][Ny[3]]

tally = tally + myArray[Nx[2]][Ny[1]] + myArray[Nx[2]][Ny[3]]

tally = tally + myArray[Nx[3]][Ny[1]] + myArray[Nx[3]][Ny[2]] + myArray[Nx[3]][Ny[3]]

return tally

end

on mDrawArray(me,  array)

scoreSeln = []

repeat with y = 1 to array.count

row = array[y]

repeat with x = 1 to row.count

if row[x] then scoreSeln.append([y,y,x,x])

end repeat

end repeat

the scoreSelection = scoreSeln

end``````
First published 15/06/2005