-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
110 lines (96 loc) · 2.8 KB
/
index.html
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
<html>
<head>
<title>Life's</title>
<style type="text/css">
#tela { font-family: "Courier New", Courier, monospace }
#telaC { position: absolute; left: 300; top: 100;}
</style>
<script type="text/javascript">
var sX = 40, sY = 140, timer = 50;
var cells = new Array(sX+2);
function init() {
var x, y;
// init
for(x=0; x<sX+2; x++) {
cells[x] = new Array(sY+2);
}
// initial draw
for(x=0; x<sX+2; x++) {
for(y=0; y<sY+2; y++) {
cells[x][y] = (x==20||y==70||(x==10&&y==10)||(x==11&&y==11)||(x==11&&y==10)||(x==12&&y==10)) ? 1 : 0;
}
}
document.getElementById('telaC').width = sX * 2;
document.getElementById('telaC').height = sY * 2;
frame();
};
function geracao() {
var x, y;
var alive, neighbours;
var newCells = new Array(sX + 2);
newCells[0] = new Array(sY+2);
newCells[sX] = new Array(sY+2);
for(x=1; x<sX; x++) {
newCells[x] = new Array(sY+2);
for(y=1; y<sY; y++) {
neighbours = 0;
if(cells[x-1][y-1]) neighbours++;
if(cells[x ][y-1]) neighbours++;
if(cells[x+1][y-1]) neighbours++;
if(cells[x-1][y ]) neighbours++;
if(cells[x+1][y ]) neighbours++;
if(cells[x-1][y+1]) neighbours++;
if(cells[x ][y+1]) neighbours++;
if(cells[x+1][y+1]) neighbours++;
alive = cells[x][y];
if(alive) {
if(neighbours < 2 || neighbours > 3) alive = 0; // 0 ou 1 = Solitude, 2 ou 3 = Ideal, 4+ = Craude
else alive = 1;
} else {
if(neighbours == 3) alive = 2; // 3 = birth
}
newCells[x][y] = alive;
}
}
cells = newCells;
}
function desenha() {
var x, y;
var txt = '';
var canvas = document.getElementById("telaC");
var ctx = canvas.getContext("2d");
for(x=1; x<sX; x++) {
for(y=1; y<sY; y++) {
txt += (cells[x][y] == 2) ? '0' : (cells[x][y] ? '*' : '.');
ctx.fillStyle = (cells[x][y] == 2) ? '#FF00FF' : (cells[x][y] ? '#FF0000' : '#000000');
ctx.fillRect(x*2, y*2, x*2+1, y*2+1);
}
txt += "<br>\n";
}
document.getElementById('tela').innerHTML = txt;
}
function frame() {
geracao();
desenha();
setTimeout(reqFrame, timer);
}
function reqFrame() {
animationFrame (frame);
}
window.animationFrame = (function(){
return window.requestAnimationFrame ||
window.webkitRequestAnimationFrame ||
window.mozRequestAnimationFrame ||
window.oRequestAnimationFrame ||
window.msRequestAnimationFrame ||
function(/* function */ callback, /* DOMElement */ element){
window.setTimeout(callback, 1000 / 60);
};
})();
</script>
</head>
<body onload="init()">
<div id='tela'></div>
<canvas id="telaC"></canvas>
</body>
</html>