-
Notifications
You must be signed in to change notification settings - Fork 0
/
game.js
142 lines (125 loc) · 4.13 KB
/
game.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
var gameOfLife = {
width: 30,
height: 30, // width and height dimensions of the board
stepInterval: null, // should be used to hold reference to an interval that is "playing" the game
//UI
createAndShowBoard: function() {
// create <table> element
var goltable = document.createElement('tbody');
// build Table HTML
var tablehtml = '';
for (var h = 0; h < this.height; h++) {
tablehtml += `<tr id='row+${h}'>`; //es6 template strings
for (var w = 0; w < this.width; w++) {
tablehtml += `<td data-status='dead' id='${w}-${h}'></td>`; //es6 template strings
}
tablehtml += `</tr>`; //es6 template strings
}
goltable.innerHTML = tablehtml;
// add table to the #board element
var board = document.getElementById('board');
board.appendChild(goltable);
// once html elements are added to the page, attach events to them
this.setupBoardEvents();
},
forEachCell: function(iteratorFunc) {
var cell;
for (var i = 0; i < this.height; i++) {
for (var j = 0; j < this.width; j++) {
cell = document.getElementById(`${j}-${i}`);
iteratorFunc(cell, i, j);
}
}
},
//Game
setupBoardEvents: function() {
var onCellClick = function() {
if (this.dataset.status == 'dead') {
this.className = 'alive';
this.dataset.status = 'alive';
} else {
this.className = 'dead';
this.dataset.status = 'dead';
}
};
var clearCell = function(cell) {
if (gameOfLife.stepInterval) {
console.log(gameOfLife.stepInterval);
clearInterval(gameOfLife.stepInterval);
this.stepInterval = null;
}
gameOfLife.forEachCell(function(cell) {
cell.className = 'dead';
cell.dataset.status = 'dead';
});
};
// putting cells on board
window.board.addEventListener('click', (event) => onCellClick.call(event.target));
//step button
window.step_btn.addEventListener('click', e => this.step())
// autoplay button
window.play_btn.addEventListener('click', (e) => this.enableAutoPlay());
// pause button
window.pause_btn.addEventListener('click', (e) => this.pause());
// clear button
window.clear_btn.addEventListener('click', (e) => clearCell());
// randomize
window.reset_btn.addEventListener('click', (e) => this.randomize());
},
randomize: function() {
gameOfLife.forEachCell(function(cell) {
var rand = Math.round(Math.random());
if (rand === 0) {
cell.className = 'dead';
cell.dataset.status = 'dead';
} else {
cell.className = 'alive';
cell.dataset.status = 'alive';
}
});
},
step: function() {
var queue = [];
gameOfLife.forEachCell(function(cell, i, j) {
var counter = 0;
for (var e = -1; e < 2; e++) {
for (var l = -1; l < 2; l++) {
var neighbor = document.getElementById((i + e) + '-' + (j + l));
if (neighbor && neighbor !== cell) {
if (neighbor.className === 'alive') {
counter++;
}
}
}
}
if (counter === 3) {
queue.push('alive');
}
else if (cell.className === 'alive' && counter === 2) {
queue.push('alive');
}
else {
queue.push('dead');
}
});
gameOfLife.forEachCell(function(cell) {
var status = queue.shift();
if (status === 'alive') {
cell.className = 'alive';
} else {
cell.className = 'dead';
cell.dataset.status = 'dead';
}
});
},
enableAutoPlay: function() {
// Start Auto-Play by running the 'step' function
// automatically repeatedly every fixed time interval
gameOfLife.stepInterval = setInterval(() => this.step(), 200)
},
pause: function() {
clearInterval(gameOfLife.stepInterval)
gameOfLife.stepInterval = null;
}
};
gameOfLife.createAndShowBoard();