-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlife.js
147 lines (122 loc) · 2.8 KB
/
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
N = 100
dead = "dead"
life = "life"
world = []
function initWorld() {
for(var i = 0; i < N; ++i) {
world.push([])
for(var j = 0; j < N; ++j) {
world[i].push(false)
}
}
world[71][32] = true
world[72][33] = true
world[73][31] = true
world[73][32] = true
world[73][33] = true
world[46][42] = true
world[46][43] = true
world[45][42] = true
world[45][43] = true
world[44][52] = true
world[45][52] = true
world[46][52] = true
world[43][53] = true
world[47][53] = true
world[42][54] = true
world[48][54] = true
world[42][55] = true
world[48][55] = true
world[45][56] = true
world[43][57] = true
world[47][57] = true
world[44][58] = true
world[45][58] = true
world[46][58] = true
world[45][59] = true
world[46][62] = true
world[47][62] = true
world[48][62] = true
world[46][63] = true
world[47][63] = true
world[48][63] = true
world[45][64] = true
world[49][64] = true
world[44][66] = true
world[45][66] = true
world[49][66] = true
world[50][66] = true
world[47][76] = true
world[48][76] = true
world[47][77] = true
world[48][77] = true
}
function initWorldLayout() {
var table = '';
table += '<table>'
for(var i = 0; i < N; ++i ) {
table += '<tr>'
for(var j = 0; j < N; ++j) {
table += '<td id="' + i + '_' + j + '" class="' + dead + '"></td>'
}
table += '</tr>'
}
table += '</table>'
$('#content').html(table);
}
function renderWorld() {
for(var i = 0; i < N; ++i ) {
for(var j = 0; j < N; ++j) {
var elem_id = '#' + i + '_' + j
var elem = $(elem_id)
if(world[i][j])
elem.removeClass(dead).addClass(life)
else
elem.removeClass(life).addClass(dead)
}
}
}
function lifeCycle(){
var newWorld = []
for(var i = 0; i < N; ++i ) {
newWorld.push([])
for(var j = 0; j < N; ++j) {
var around = []
if(i < N-1 && j < N-1)
around.push(world[i+1][j+1])
if(j < N-1)
around.push(world[i][j+1])
if(i < N-1)
around.push(world[i+1][j])
if(i > 0 && j < N-1)
around.push(world[i-1][j+1])
if(i < N-1 && j > 0)
around.push(world[i+1][j-1])
if(i > 0 && j > 0)
around.push(world[i-1][j-1])
if(i > 0)
around.push(world[i-1][j])
if(j > 0)
around.push(world[i][j-1])
var count = 0
for(var k = 0; k < around.length; ++k) {
count += (around[k] ? 1 : 0)
}
if(count == 3)
newWorld[i].push(true)
else if((count < 2) || (count > 3))
newWorld[i].push(false)
else
newWorld[i].push(world[i][j])
}
}
world = newWorld
renderWorld();
setTimeout(lifeCycle, 200)
}
$(document).ready(function() {
initWorld();
initWorldLayout();
renderWorld();
window.setTimeout(lifeCycle, 1000);
})