Skip to content

Instantly share code, notes, and snippets.

@zonoise
Created May 15, 2013 22:41
Show Gist options
  • Save zonoise/5588012 to your computer and use it in GitHub Desktop.
Save zonoise/5588012 to your computer and use it in GitHub Desktop.
lifegame coffee
class Matrix
constructor:(@x,@y)->
init_data = false
for y in [0..@y-1]
@data.push(( init_data for i in [0..@x-1]))
data: []
set:(x,y,value)->
if @exist(x,y)
@data[y][x]=value
exist:(x,y)->
if @data[y]? and @data[y][x]?
true
else
false
get:(x,y)->
if @exist(x,y)
@data[y][x]
else
null
get_around:(x,y)->
ary = [
this.get(x-1,y-1),
this.get(x,y-1),
this.get(x+1,y-1),
this.get(x-1,y),
this.get(x+1,y),
this.get(x-1,y+1),
this.get(x,y+1),
this.get(x+1,y+1) ]
count = 0
for i in ary
if i is true
count += 1
count
get_future: (x,y) ->
cell = @get(x,y)
around = @get_around(x,y)
if true == cell
switch around
when 0
'death'
when 1
'death'
when 2
'alive'
when 3
'alive'
else
'death'
else
if 3 == around
'alive'
else
'death'
get_future_all: ->
result = []
for y in [0..@y-1]
result.push((@get_future(x,y) for x in [0..@x-1]))
result
apply_future: (future) ->
for y in [0..@y-1]
for x in [0..@x-1]
if future[y][x] == 'alive'
@set(x,y,true)
else
@set(x,y,false)
next: ->
future = @get_future_all()
@apply_future(future)
disp:()->
buf = ''
for y in [0..@y-1]
for x in [0..@x-1]
if true == @get(x,y)
buf+='■'
else
buf+='□'
buf+='\n'
console.log(buf)
###
a = new Matrix(3,4)
console.log '--disp'
console.log a.disp()
console.log '--exist'
console.log a.exist(1,1)
console.log '--get'
console.log a.get(1,1)
console.log '--set'
console.log a.set(0,0,true)
console.log a.set(1,0,true)
console.log a.set(2,0,true)
console.log '--get_around'
console.log a.get_around(1,1)
console.log a.get_around(1,0)
console.log '--get_next'
console.log a.get_future(1,1)
console.log a.get_future(1,0)
console.log a.get_future_all()
b = new Matrix(4,4)
b.data=[[false,true,false,false],
[false,false,true,true],
[true,true,false,false],
[false,false,true,false]]
b = new Matrix(8,3)
b.data=[[false,true,false,false,false,false,true,false],
[true,true,false,false,false,false,true,true],
[false,true,false,false,false,false,true,false]]
###
blinker = ->
m = new Matrix(5,5)
m.data=[[false,false,false,false,false],
[false,false,false,false,false],
[false,true,true,true,false],
[false,false,false,false,false],
[false,false,false,false,false]]
for i in [0..15]
m.next()
m.disp()
temp_game = ->
m = new Matrix(15,15)
m.data = [
[false,false,false,false,false,false,false,false,false,false,false,false,false,false,false],
[false,false,false,true,true,true,false,false,false,true,true,true,false,false,false],
[false,false,false,false,false,false,false,false,false,false,false,false,false,false,false],
[false,true,false,false,false,false,true,false,true,false,false,false,false,true,false],
[false,true,false,false,false,false,true,false,true,false,false,false,false,true,false],
[false,true,false,false,false,false,true,false,true,false,false,false,false,true,false],
[false,false,false,true,true,true,false,false,false,true,true,true,false,false,false],
[false,false,false,false,false,false,false,false,false,false,false,false,false,false,false],
[false,false,false,true,true,true,false,false,false,true,true,true,false,false,false],
[false,true,false,false,false,false,true,false,true,false,false,false,false,true,false],
[false,true,false,false,false,false,true,false,true,false,false,false,false,true,false],
[false,true,false,false,false,false,true,false,true,false,false,false,false,true,false],
[false,false,false,false,false,false,false,false,false,false,false,false,false,false,false],
[false,false,false,true,true,true,false,false,false,true,true,true,false,false,false],
[false,false,false,false,false,false,false,false,false,false,false,false,false,false,false]
]
for i in [0..15]
m.next()
m.disp()
blinker()
temp_game()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment