public
Created

tabular version of life translated from http://cafaq.cafaq.com/lifefaq/index.php

  • Download Gist
life_table.coffee
CoffeeScript
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242
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

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.