Skip to content

Instantly share code, notes, and snippets.

@yusugomori
Created April 17, 2012 03:14
Show Gist options
  • Select an option

  • Save yusugomori/2403154 to your computer and use it in GitHub Desktop.

Select an option

Save yusugomori/2403154 to your computer and use it in GitHub Desktop.
Life Game
#
# Life Game by CoffeeScript
# @author yusugomori
# @license http://yusugomori.com/license/mit The MIT License
#
# Usage:
# <body><div id="lifegame"></div></body>
#
# $ ->
# L = new Lifegame()
# L.createAgent()
# L.agentStep()
#
# Options:
# el: '#String' # ID of <div> for creating agent set. Default is '#lifegame'
# cellSize: Int (>=1) # each cell (Grid) size. Default if 15
# liveColor: 'String' # background-color for live cell. Default is '#111'
# dieColor: 'String' # background-color for dead cell. Default is '#fff'
# initialLiveProb: Float # set survival rate for initial cell. Default is 0.3
# stepTime: Int (>=1) # set interval time for each step. Default is 500
#
class Lifegame
constructor: (options={}) ->
@el = options.el or '#lifegame'
@_el = @el.replace(/^\#/,'')
@cellSize = options.cellSize or 15
@liveColor = options.liveColor or '#111'
@dieColor = options.dieColor or '#fff'
@initialLiveProb = options.initialLiveProb or 0.3
@stepTime = options.stepTime or 500
@clientWidth = document.documentElement.clientWidth
@clientHeight = document.documentElement.clientHeight
@colNum = Math.floor(@clientWidth / @cellSize)
@rowNum = Math.floor(@clientHeight / @cellSize)
@cellState = new Array(@rowNum)
for i in [0...@rowNum]
@cellState[i] = new Array(@colNum)
@neighborCellAlive = new Array(@rowNum)
for i in [0...@rowNum]
@neighborCellAlive[i] = new Array(@colNum)
$(@el).css
width: @clientWidth
height: @clientHeight
position: 'fixed'
top: 0
left: 0
'background-color': '#fff'
height: '100%'
width: '100%'
# seed
createAgent: () ->
for i in [0...@rowNum]
for j in [0...@colNum]
id = "#cell_#{i}_#{j}"
_id = id.replace(/^\#/,'')
if Math.random() < @initialLiveProb
color = @liveColor
state = true
else
color = @dieColor
state = false
@cellState[i][j] = state
$(@el).append "<div id=\"#{_id}\"></div>"
$(id).css
width: @cellSize
height: @cellSize
position: 'absolute'
top: i*@cellSize
left: j*@cellSize
'background-color': color
$("#{@el} > div").css
'-webkit-box-sizing': 'border-box'
'-moz-box-sizing': 'border-box'
'-ms-box-sizing': 'border-box'
'-o-box-sizing': 'border-box'
'box-sizing': 'border-box'
'border-style': 'solid'
'border-width': '1px 0 0 1px'
'border-color': '#fbfbfb'
@
# loop
agentStep: () ->
@getNeighborCellAlive()
for i in [0...@rowNum]
for j in [0...@colNum]
if @cellState[i][j] is true
if @neighborCellAlive[i][j] is 2 or @neighborCellAlive[i][j] is 3
color = @liveColor
else
@cellState[i][j] = false
color = @dieColor
else
if @neighborCellAlive[i][j] is 3
@cellState[i][j] = true
color = @liveColor
else
color = @dieColor
$("#cell_#{i}_#{j}").css
'background-color': color
setTimeout =>
@agentStep()
, @stepTime
getNeighborCellAlive: () ->
for i in [0...@rowNum]
for j in [0...@colNum]
@neighborCellAlive[i][j] = 0
for i in [0...@rowNum]
_i = (i - 1 + @rowNum) % @rowNum
i_ = (i + 1) % @rowNum
for j in [0...@colNum]
_j = (j - 1 + @colNum) % @colNum
j_ = (j + 1) % @colNum
for h in [_i, i, i_]
for k in [_j, j, j_]
if h is i and k is j
continue
if @cellState[h][k] is true
@neighborCellAlive[i][j]++
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment