Created
December 11, 2012 00:07
-
-
Save spencerdeinum/4254564 to your computer and use it in GitHub Desktop.
Conway's game of life
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
start_game = -> | |
canvas = document.getElementById "canvas" | |
context = canvas.getContext "2d" | |
blockHeight = canvas.height / 10 | |
blockWidth = canvas.width / 10 | |
draw_cell = (x, y, alive) -> | |
x = x * blockWidth | |
y = y * blockHeight | |
if alive | |
context.fillRect x, y, blockWidth, blockHeight | |
else | |
context.clearRect x, y, blockWidth, blockHeight | |
cells = create_cells() | |
draw_cells cells, draw_cell | |
setInterval (-> update cells, draw_cell), 500 | |
update = (cells, draw_cell) -> | |
cells = update_cells cells | |
draw_cells cells, draw_cell | |
update_cells = (cells) -> | |
newCells = cells | |
for x in [0..9] | |
for y in [0..9] | |
liveNeighbours = count_live_neighbours cells, x, y | |
if liveNeighbours < 2 | |
newCells[x][y] = 0 | |
else if liveNeighbours == 3 | |
newCells[x][y] = 1 | |
else if liveNeighbours > 3 | |
newCells[x][y] = 0 | |
else | |
newCells[x][y] = cells[x][y] | |
console.log cells | |
console.log newCells | |
newCells | |
draw_cells = (cells, draw_cell) -> | |
for x in [0..9] | |
for y in [0..9] | |
draw_cell x, y, cells[x][y] | |
create_cells = (columns, rows) -> | |
cells = [] | |
cells.push [] for i in [1..10] | |
for column in cells | |
for i in [1..10] | |
column.push Math.floor Math.random() * 2 | |
cells | |
count_live_neighbours = (cells, x, y) -> | |
liveBorderingCells = 0 | |
liveBorderingCells += 1 if cell_above cells, x, y | |
liveBorderingCells += 1 if cell_below cells, x, y | |
liveBorderingCells += 1 if cell_left cells, x, y | |
liveBorderingCells += 1 if cell_right cells, x, y | |
liveBorderingCells += 1 if cell_top_left cells, x, y | |
liveBorderingCells += 1 if cell_top_right cells, x, y | |
liveBorderingCells += 1 if cell_bottom_left cells, x, y | |
liveBorderingCells += 1 if cell_bottom_right cells, x, y | |
liveBorderingCells | |
cell_above = (cells, x, y) -> | |
if cells[x][y - 1] | |
cells[x][y - 1] | |
else | |
false | |
cell_below = (cells, x, y) -> | |
if cells[x][y + 1] | |
cells[x][y + 1] | |
else | |
false | |
cell_left = (cells, x, y) -> | |
if cells[x - 1] | |
cells[x - 1][y] | |
else | |
false | |
cell_right = (cells, x, y) -> | |
if cells[x + 1] | |
cells[x + 1][y] | |
else | |
false | |
cell_top_left = (cells, x, y) -> | |
if cells[x - 1] and cells[x - 1][y - 1] | |
cells[x - 1][y - 1] | |
else | |
false | |
cell_top_right = (cells, x, y) -> | |
if cells[x + 1] and cells[x + 1][y - 1] | |
cells[x + 1][y - 1] | |
else | |
false | |
cell_bottom_left = (cells, x, y) -> | |
if cells[x - 1] and cells[x - 1][y + 1] | |
cells[x - 1][y + 1] | |
else | |
false | |
cell_bottom_right = (cells, x, y) -> | |
if cells[x + 1] and cells[x + 1][y + 1] | |
cells[x + 1][y + 1] | |
else | |
false | |
jQuery -> | |
$("#start_game").on "click", -> | |
start_game() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment