-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
182 lines (140 loc) · 4.4 KB
/
script.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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
function Cell() {
this.el = document.createElement("td");
this.alive = (function () {
if (Math.round(Math.random())) {
this.el.className = "alive";
return true
} else {
this.el.className = "dead";
return false
}
}.call(this));
}
function TableRow() {
this.el = document.createElement("tr");
}
function GameBoard() {
this.el = document.createElement("table");
this.el.className = "game-board";
this.deg = 0;
}
GameBoard.prototype.render = function () {
document.body.appendChild(this.el);
}
GameBoard.prototype.createGameBoard = function (gridSize) {
//if gridSize is not passed it will be given a random number between 5 and 24
var gridSize = gridSize || Math.floor(20 * Math.random()) + 5,
i = 0;
for (i; i < gridSize; i++) {
var row = new TableRow();
for (var j = 0; j < gridSize; j++) {
var cell = new Cell();
cell.el.dataset.cell = j;
row.el.appendChild(cell.el);
}
row.el.dataset.row = i;
this.el.appendChild(row.el);
}
}
GameBoard.prototype.tickTable = function() {
var table = this.el;
tds = table.getElementsByTagName("td");
for (var el in tds){
tick(tds[el]);
}
setTimeout(GameBoard.prototype.tickTable.bind(this),1500);
}
GameBoard.prototype.turnGameBoard = function () {
this.deg+=3;
this.el.style.transform = "rotate("+this.deg+"deg)";
setTimeout(GameBoard.prototype.turnGameBoard.bind(this),200);
}
//function that counts the number of "alive" neighbors a cell has
function numAliveNeighbors(td) {
var counter = 0,
rowNumber = Number(td.parentNode.dataset.row),
cellNumber = Number(td.dataset.cell);
//top-left neighbor
var topLeft = document.body.querySelector("tr[data-row ='" + [rowNumber - 1] + "'] td[data-cell='" + [cellNumber - 1] + "']");
if (!topLeft){
counter+0
}
else if (topLeft.className === "alive") {
counter++;
}
//top neighbor
var top = document.body.querySelector("tr[data-row ='" + [rowNumber - 1] + "'] td[data-cell='" + cellNumber + "']");
if (!top){
counter+0
}
else if (top.className === "alive") {
counter++;
}
// top-right neighbor
var topRight = document.body.querySelector("tr[data-row ='" + [rowNumber - 1] + "'] td[data-cell='" + [cellNumber + 1] + "']");
if (!topRight){
counter+0
}
else if (topRight.className === "alive") {
counter++;
}
// left neighbor
var left = document.body.querySelector("tr[data-row ='" + rowNumber + "'] td[data-cell='" + [cellNumber - 1] + "']");
if (!left){
counter+0
}
else if (left.className === "alive") {
counter++;
}
// right neighbor
var right = document.body.querySelector("tr[data-row ='" + rowNumber + "'] td[data-cell='" + [cellNumber + 1] + "']");
if (!right){
counter+0
}
else if (right.className === "alive") {
counter++;
}
// bottom-left neighbor
var bottomLeft = document.body.querySelector("tr[data-row ='" + [rowNumber + 1] + "'] td[data-cell='" + [cellNumber - 1] + "']");
if (!bottomLeft){
counter+0
}
else if (bottomLeft.className === "alive") {
counter++;
}
// bottom neighbor
var bottom = document.body.querySelector("tr[data-row ='" + [rowNumber + 1] + "'] td[data-cell='" + cellNumber + "']");
if (!bottom){
counter+0
}
else if (bottom.className === "alive") {
counter++;
}
//bottom-right neighbor
var bottomRight = document.body.querySelector("tr[data-row ='" + [rowNumber + 1] + "'] td[data-cell='" + [cellNumber + 1] + "']");
if (!bottomRight){
counter+0
}
else if (bottomRight.className === "alive") {
counter++;
}
return counter;
}
//function which sets the rules for a single cell tick
function tick (el){
if (el.className === "alive"){
if (numAliveNeighbors(el) < 2 ||numAliveNeighbors(el)>3) {
el.className = "dead";
}
} else if
(el.className==="dead"){
if (numAliveNeighbors(el)===3){
el.className = "alive";
}
}
}
var testTable = new GameBoard();
testTable.createGameBoard();
testTable.render();
setTimeout(GameBoard.prototype.turnGameBoard.bind(testTable),200);
setTimeout(GameBoard.prototype.tickTable.bind(testTable),1500);