-
Notifications
You must be signed in to change notification settings - Fork 1
/
game.of.life.js
149 lines (133 loc) · 3.36 KB
/
game.of.life.js
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
(function () {
// Game defines the 2D world in which the game
// plays out
var _ = self.Game = function (width, height) {
var rows = new Array(width)
, columns
for (var i = 0; i < rows.length; i++) {
columns = rows[i] = new Array(height)
}
this.previousBoard = []
this.board = rows
this.height = columns.length // columns
this.width = rows.length // rows
}
// create life on the grid
_.prototype = {
setBoard: function(rows) {
this.board = rows
this.width = rows.length
this.height = rows[0].length
}
, getClone: function () {
var clone = []
for (var w = 0; w < this.width; w++) {
clone.push(new Array(this.height))
for (var h = 0; h < this.height; h++) {
clone[w][h] = this.board[w][h]
}
}
return clone
}
, spawn: function (x, y) {
if (y < this.height && x < this.width) {
this.board[x][y] = true
} else {
throw new Error("Invalid grid position")
}
}
// is a location on the grid alive?
, isAlive: function (x, y, isPrev) {
var alive = false
, currentBoard = isPrev ? this.previousBoard : this.board
if (y > -1 && x > -1 && y < currentBoard.length && x < currentBoard[0].length) {
alive = currentBoard[x][y]
}
return alive
}
// how many neighbours at this location
, aliveNeighbours: function (x, y, isPrev) {
var me = this
, sum = 0
, left = x - 1
, right = x + 1
, top = y + 1
, bottom = y - 1
// the positions around this point in the
// grid where neighbours can be found
, neighbours = [
[left, y]
, [left, top]
, [x, top]
, [right, top]
, [right, y]
, [right, bottom]
, [x, bottom]
, [left, bottom]
]
neighbours.forEach(function (n) {
if(me.isAlive(n[0], n[1], isPrev)) {
sum += 1
}
})
return sum
}
, kill: function(x, y) {
this.board[x][y] = false
}
, search: function(args){
var me = this
// cover x and y poisitions incrementally
for (var w = 0; w < this.width; w++) {
for (var h = 0; h < this.height; h++) {
if(typeof args.each == 'function') {
args.each(w, h)
}
}
}
}
, next: function() {
var neighbours, me = this
this.previousBoard = me.getClone()
function checkCell(xx, yy){
neighbours = me.aliveNeighbours(xx, yy, true)
if(me.isAlive(xx,yy)){
if(neighbours < 2 || neighbours > 3) {
me.kill(xx, yy)
}
} else {
if (neighbours === 3) {
me.spawn(xx, yy)
}
}
}
me.search({each: checkCell})
}
, draw: function () {
var table = document.createElement('table')
, checkboxes = []
table.setAttribute('id', 'grid')
for (var h = 0; h < this.height; h++) {
var row = document.createElement('tr')
row.classList.add('row')
checkboxes[h] = []
for (var w = 0; w < this.width; w++) {
var cell = document.createElement('td')
, input = document.createElement('input')
cell.classList.add('cell')
input.type = "checkbox"
input.setAttribute('data-x', w)
input.setAttribute('data-y', h)
checkboxes[h][w] = input
if(this.isAlive(w,h)) {
input.setAttribute('checked', 'checked')
}
cell.appendChild(input)
row.appendChild(cell)
}
table.appendChild(row)
}
return {html: table, checkboxes: checkboxes}
}
}
})()