-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdish.coffee
65 lines (51 loc) · 1.56 KB
/
dish.coffee
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
class Dish
constructor: (@life) ->
evolve: () ->
evolveCell = (count, row, col, life) ->
if (life[row][col] is 1 and count > 1 and count < 4) or (life[row][col] is 0 and count is 3)
return 1
else
return 0
###
creating an empty 2d array
###
newLife = []
newLife.push [0] for i in [[email protected]]
newLife[row][col] = evolveCell(this.livingNeigbours(row, col), row, col, @life) for col in [0..@life[0].length-1] for row in [[email protected] - 1]
new Dish newLife
livingNeigbours: (row, col) ->
isCellAlive = (ro, co, life) ->
if (ro < 0 or co < 0) or (ro is row and co is col) or (ro > (life.length - 1) or co > (life[0].length - 1))
return 0
else
return life[ro][co]
count = 0
count = count + isCellAlive((row + rowOffset), (col + colOffset), @life) for colOffset in [-1,0,1] for rowOffset in [-1,0,1]
return count
toString: () ->
console.log @life
dish = new Dish [
[0,0,0,0,0],
[0,0,0,0,0],
[0,1,1,1,0],
[0,0,0,0,0],
[0,0,0,0,0],
]
dish.evolve().toString()
dish = new Dish [
[ 0,1,0,0,0,0,0,0,0,0,0,0],
[ 0,0,1,0,0,0,0,0,0,0,0,0],
[ 1,1,1,0,0,0,0,0,0,0,0,0],
[ 0,0,0,0,0,0,0,0,0,0,0,0],
[ 0,0,0,0,0,0,0,0,0,0,0,0],
[ 0,0,0,0,0,0,0,0,0,0,0,0],
[ 0,0,0,0,0,0,0,0,0,0,0,0],
[ 0,0,0,0,0,0,0,0,0,0,0,0],
[ 0,0,0,0,0,0,0,0,0,0,0,0],
[ 0,0,0,0,0,0,0,0,0,0,0,0],
[ 0,0,0,0,0,0,0,0,0,0,0,0]
]
dish = dish.evolve()
dish.toString()
dish = dish.evolve()
dish.toString()