-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
133 lines (111 loc) · 2.8 KB
/
main.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
var c;
var ctx;
var board = [];
var anim; // Interval for stepping through life
var intervalTimer = 100;
window.onload = function () {
// Canvas Setup
canv = document.getElementById("canvas");
ctx = canv.getContext("2d");
board = createBoard(50, 50, false);
drawBoard();
// Cell Toggle.
canv.addEventListener("click", function(event){
var i = parseInt(event.offsetX/10);
var j = parseInt(event.offsetY/10);
board[i][j] = !board[i][j];
drawBoard();
});
// Event listener for speed controller
document.getElementById("speed").addEventListener("change", function(event){
intervalTimer = event.target.value;
setSpeed();
})
}
// User Input functions
function clearBoard(){
board = createBoard(50, 50, false);
drawBoard();
}
function setSpeed() {
clearInterval(anim);
anim = setInterval(function(){
lifeStep();
drawBoard();
}, 1000/intervalTimer);
}
function stopSteps() {
clearInterval(anim);
}
function randomize() {
for (var i = 0; i < 50; i++) {
for (var j = 0; j < 50; j++) {
board[i][j] = (Math.random() > 0.5);
}
}
drawBoard();
}
// Canvas draw handling
function drawBoard() {
var live = 0;
for (var i = 0; i < 50; i++) {
for (var j = 0; j < 50; j++) {
if(board[i][j]) {
ctx.fillStyle="#ff0000";
live++;
} else {
ctx.fillStyle="#000000"
}
ctx.fillRect(i*10 + 1, j*10 + 1, 9, 9);
}
}
document.getElementById("living").innerText = ("Living: " + live);
}
// Board control functions
function createBoard(height, width, value) {
var tempBoard = [];
for (var i = 0; i < height; i++) {
var row = [];
for (var j = 0; j < width; j++) {
row.push(value);
}
tempBoard.push(row);
}
return tempBoard;
}
// Game of life control rules
function findNeighbors(i, j) {
var n = 0;
if(checkValidity(i-1, j-1) && board[i-1][j-1]) n++;
if(checkValidity(i , j-1) && board[i ][j-1]) n++;
if(checkValidity(i+1, j-1) && board[i+1][j-1]) n++;
if(checkValidity(i-1, j ) && board[i-1][j ]) n++;
if(checkValidity(i+1, j ) && board[i+1][j ]) n++;
if(checkValidity(i-1, j+1) && board[i-1][j+1]) n++;
if(checkValidity(i , j+1) && board[i ][j+1]) n++;
if(checkValidity(i+1, j+1) && board[i+1][j+1]) n++;
return n;
}
function checkValidity(i, j){
return (board[i] != undefined && board[i][j] != undefined);
}
function lifeStep() {
var tempBoard = createBoard(50, 50);
for (var i = 0; i < 50; i++) {
for (var j = 0; j < 50; j++) {
var n = findNeighbors(i,j);
if(board[i][j]) {
if(n < 2)
tempBoard[i][j] = false;
else if(n > 3)
tempBoard[i][j] = false;
else
tempBoard[i][j] = true;
} else {
if(n == 3)
tempBoard[i][j] = true;
}
}
}
board = tempBoard;
}