Skip to content

Instantly share code, notes, and snippets.

@victusfate
Created June 25, 2011 12:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save victusfate/1046449 to your computer and use it in GitHub Desktop.
Save victusfate/1046449 to your computer and use it in GitHub Desktop.
tabular version of life translated from http://cafaq.cafaq.com/lifefaq/index.php
cell_width = 4
cell_height = 4
width = 96
height = 96
delay = 1
life = new Array(width)
all_td = null
running = false
threads = 0
request_stop = false
paused = false
get_element_by_id = (name) ->
document.getElementById name
class Cell
constructor: (@age=0,@old_age=1,@neighbours=0) ->
SetCellImage = (x, y) ->
v = life[x][y].age
table_cell = all_td[x + width * y]
if v == 0
table_cell.style.background = "#E0E0E0"
else
table_cell.style.background = "#000000"
count_all_neighbours = ->
x = 0
while x < width
life[x][0].neighbours = count_neighbours(x, 0)
life[x][height - 1].neighbours = count_neighbours(x, height - 1)
x++
y = 1
while y < height - 1
life[0][y].neighbours = count_neighbours(0, y)
life[width - 1][y].neighbours = count_neighbours(width - 1, y)
y++
x = 1
while x < width - 1
y = 1
while y < height - 1
life[x][y].neighbours = count_neighbours(x, y)
y++
x++
count_neighbours = (x, y) ->
result = 0
checkx = x - 1
while checkx <= x + 1
checky = y - 1
while checky <= y + 1
cx = (if (checkx < 0) then width - 1 else (if (checkx >= width) then 0 else checkx))
cy = (if (checky < 0) then height - 1 else (if (checky >= height) then 0 else checky))
result += life[cx][cy].age if (cx != x) or (cy != y)
checky++
checkx++
result
count_neighbours_fast = (x, y) ->
result = life[x - 1][y - 1].age
result += life[x - 1][y].age
result += life[x - 1][y + 1].age
result += life[x][y - 1].age
result += life[x][y + 1].age
result += life[x + 1][y - 1].age
result += life[x + 1][y].age
result += life[x + 1][y + 1].age
result
update_neighbour_counts = (x, y, delta) ->
update_neighbour_count x - 1, y - 1, delta
update_neighbour_count x - 1, y, delta
update_neighbour_count x - 1, y + 1, delta
update_neighbour_count x, y - 1, delta
update_neighbour_count x, y + 1, delta
update_neighbour_count x + 1, y - 1, delta
update_neighbour_count x + 1, y, delta
update_neighbour_count x + 1, y + 1, delta
update_neighbour_count = (x, y, delta) ->
if x < 0
x = width - 1
else x = 0 if x >= width
if y < 0
y = height - 1
else y = 0 if y >= height
current = life[x][y]
current.neighbours = current.neighbours + delta
update_all = ->
x = 0
while x < width
y = 0
while y < height
update x, y
y++
x++
update = (x, y) ->
current = life[x][y]
current.old_age = current.age
if current.age == 0
current.age = 1 if current.neighbours == 3
else
current.age = 0 if (current.neighbours > 3) or (current.neighbours < 2)
render_updated = ->
x = 0
while x < width
y = 0
while y < height
current = life[x][y]
unless current.old_age == current.age
SetCellImage x, y
update_neighbour_counts x, y, current.age - current.old_age
y++
x++
life_cycle = ->
if running or (threads > 1)
threads--
return
running = true
update_all()
render_updated()
if request_stop
request_stop = false
threads--
else
setTimeout "life_cycle()", delay
running = false
window.life_cycle = life_cycle
render_all = ->
x = 0
while x < width
y = 0
while y < height
current = life[x][y]
current.old_age = current.age ^ 1
SetCellImage x, y
y++
x++
count_all_neighbours()
life_setup = ->
console.log "got into life setup ok"
table = get_element_by_id("life_table")
# table = $("#life_table").first()
console.log "got element by id "+table
x = 0
while x < width
life[x] = new Array(height)
x++
y = 0
while y < height
row = "<tr>"
x = 0
while x < width
row = row+"<td style=\"background:#E0E0E0\" id=\"" + x + "\" name=\"" + y + "\"+ \" WIDTH=\"" + cell_width + "\" HEIGHT=\"" + cell_height + "><font size=\"-6\"></font></td>"
life[x][y] = new Cell()
x++
row = row+"</tr>"
$("#life_table").append row
y++
all_td = table.getElementsByTagName("td")
console.log "got element by tag "+all_td
window.life_setup = life_setup
life_click = ->
e = event.srcElement
return if e.id == "table"
return unless e.id?
return if e.id == ""
x = eval(e.id)
y = eval(e.name)
current = life[x][y]
current.age = 1
SetCellImage x, y
update_neighbour_counts x, y, current.age - current.old_age
window.life_click = life_click
life_clear = ->
x = 0
while x < width
y = 0
while y < height
life[x][y].age = 0
y++
x++
render_all()
life_randomise = ->
x = 0
while x < width
y = 0
while y < height
if (life[x][y].age == 0) and (Math.floor(Math.random() * 5) == 0)
life[x][y].age = 1
else
life[x][y].age = 0
y++
x++
render_all()
window.life_randomise = life_randomise
life_pause = ->
paused = not paused
life_start = ->
unless paused
if threads < 1
threads++
count_all_neighbours()
life_cycle()
request_stop = false
window.life_start = life_start
life_stop = ->
request_stop = true unless paused
window.life_stop = life_stop
###
FPSCounter = (ctx) ->
@t = new Date().getTime() / 1000.0
@n = 0
@fps = 0.0
@draw = ->
@n++
if @n == 10
@n = 0
t = new Date().getTime() / 1000.0
@fps = Math.round(100 / (t - @t)) / 10
@t = t
ctx.fillStyle = "white"
ctx.fillText "FPS: " + @fps, 1, 15
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment