-
Notifications
You must be signed in to change notification settings - Fork 2
/
gol.js
269 lines (233 loc) · 9.33 KB
/
gol.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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
/* game-of-life is a pure JavaScript/HTML/CSS implementation of Conway's Game of Life
* Copyright (C) 2017 Nikolaos Moumoulidis
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
window.onload = function() {
/**
* TODOs:
* - Include buttons that generate popular patterns (preferably drag'n'drop).
* - Make the game more colorful by creating mapping between specific colors and number generations.
*/
function Petritable(w, h)
{
this.width = w;
this.height = h;
this.htmlPetriTable;
this.cellArray = new Array();
this.getWidth = getWidth;
this.getHeight = getHeight;
this.getCell = getCell;
this.buildCellObject = buildCellObject;
this.assignCellToArray = assignCellToArray;
this.buildPetriTable = buildPetriTable;
this.deletePetritable = deletePetritable;
this.syncDrawingStatusToLife = syncDrawingStatusToLife;
this.syncLifeStatusToDrawing = syncLifeStatusToDrawing;
function getWidth() {
return this.width;
}
function getHeight() {
return this.height;
}
function getCell(width, height) {
return this.cellArray[ width + "_" + height ];
}
function buildCellObject(cellHTMLElement, width, height) {
var newCell = new Cell(cellHTMLElement);
newCell.setID(width, height);
newCell.setAttribute("id", width + "_" + height);
newCell.setAttribute("class", "deadcell");
newCell.addEventListener("click",function myfunc() {
if (this.getAttribute("class") == "deadcell") {
this.setAttribute("class", "alivecell");
}
else {
this.setAttribute("class", "deadcell");
}
});
return newCell;
}
function assignCellToArray(cell, width, height) {
this.cellArray[ width + "_" + height ] = cell;
}
function buildPetriTable() {
this.htmlPetriTable = document.createElement("table");
this.htmlPetriTable.setAttribute("class","petritable");
this.htmlPetriTable.setAttribute("id", "petritable");
for (var i = 0; i < this.width ; i++) {
var rowHTMLElement = this.htmlPetriTable.insertRow(i);
for (var j = 0; j < this.height ; j++) {
var cellHTMLElement = rowHTMLElement.insertCell(j);
var newCell = buildCellObject(cellHTMLElement, i, j);
this.assignCellToArray(newCell, i, j);
}
}
document.body.appendChild(this.htmlPetriTable);
}
function deletePetritable() {
document.getElementById("petritable").remove();
}
function syncDrawingStatusToLife() {
for (var i = 0; i < this.width ; i++) {
for (var j = 0; j < this.height ; j++) {
var currentCell = this.getCell(i, j);
if (currentCell.isAlive()) {
currentCell.setAttribute("class", "alivecell");
} else {
currentCell.setAttribute("class", "deadcell");
}
}
}
}
function syncLifeStatusToDrawing() {
for (var i = 0; i < this.width ; i++) {
for (var j = 0; j < this.height ; j++) {
var currentCell = this.getCell(i, j);
if (currentCell.getAttribute("class") == "alivecell") {
currentCell.setAlive(true);
} else {
currentCell.setAlive(false);
}
}
}
}
this.buildPetriTable();
}
function Cell(c)
{
this.element = c;
this.row;
this.column;
this.alive = false;
this.generation = 0;
this.setID = setID;
this.setAttribute = setAttribute;
this.getAttribute = getAttribute;
this.addEventListener = addEventListener;
this.setAlive = setAlive;
this.isAlive = isAlive;
this.getNumberOfNeighbours = getNumberOfNeighbours;
this.getNeighborCoords = getNeighborCoords;
this.isNeighbourAlive = isNeighbourAlive;
function setAttribute(attribute, value) {
this.element.setAttribute(attribute, value);
}
function getAttribute(attribute) {
return this.element.getAttribute(attribute);
}
function addEventListener(action, func) {
this.element.addEventListener(action ,func);
}
function setID(row, col) {
this.row = row;
this.column = col;
}
function setAlive(isAlive) {
this.alive = isAlive;
}
function isAlive() {
return this.alive;
}
function getNumberOfNeighbours(table) {
var num = 0;
var neighborCoords = this.getNeighborCoords();
for (var i = 0; i < neighborCoords.length; i += 2) {
if (this.isNeighbourAlive(i, neighborCoords, table)) {
num++;
}
}
return num;
}
function getNeighborCoords() {
var neighborCoords = new Array();
neighborCoords[0] = this.row - 1; // top row
neighborCoords[1] = this.column; // top col
neighborCoords[2] = this.row + 1; // bottom row
neighborCoords[3] = this.column; // bottom col
neighborCoords[4] = this.row; // left row
neighborCoords[5] = this.column - 1; // left col
neighborCoords[6] = this.row; // right row
neighborCoords[7] = this.column + 1; // right col
neighborCoords[8] = this.row - 1; // top left row
neighborCoords[9] = this.column - 1; // top left col
neighborCoords[10] = this.row - 1; // top right row
neighborCoords[11] = this.column + 1; // top right col
neighborCoords[12] = this.row + 1; // bottom left row
neighborCoords[13] = this.column - 1; // bottom left col
neighborCoords[14] = this.row + 1; // bottom right row
neighborCoords[15] = this.column + 1; // bottom right col
return neighborCoords;
}
function isNeighbourAlive(index, coords, table) {
var dir_r = coords[ index ];
var dir_c = coords[ index + 1 ];
if (dir_r < 0 || dir_c < 0 ||
dir_r > table.getWidth() - 1 ||
dir_c > table.getHeight() - 1) {
return false;
}
if(table.getCell(dir_r, dir_c).getAttribute("class") == "alivecell") {
return true;
}
return false;
}
}
function LifeGen(w, h)
{
this.width = w;
this.height = h;
this.theTable = new Petritable(w,h);
this.lifeAndDeath = lifeAndDeath;
function lifeAndDeath() {
this.theTable.syncDrawingStatusToLife();
for (var i = 0; i < this.width ; i++) {
for (var j = 0; j < this.height ; j++) {
var currentCell = this.theTable.getCell(i, j);
var nOn = currentCell.getNumberOfNeighbours(this.theTable);
if (currentCell.isAlive()) {
if (nOn < 2) {
currentCell.setAlive(false);
} else if (nOn == 2 || nOn == 3) {
currentCell.generation++;
} else {
currentCell.setAlive(false);
}
} else {
if (nOn == 3) {
currentCell.setAlive(true);
}
}
}
}
}
}
var petritableWidth = 80;
var petritableHeight = 130;
var generator = new LifeGen(petritableWidth,petritableHeight);
var intervalId;
var startBtnListener = function() {
generator.theTable.syncLifeStatusToDrawing();
intervalId = setInterval(function() { generator.lifeAndDeath(); }, 50);
this.setAttribute("disabled", true);
}
var resetBtnListener = function() {
generator.theTable.deletePetritable();
clearInterval(intervalId);
document.getElementById("startBtn").removeAttribute("disabled");
generator = new LifeGen(petritableWidth,petritableHeight);
}
document.getElementById("startBtn").addEventListener("click", startBtnListener, false);
document.getElementById("resetBtn").addEventListener("click", resetBtnListener, false);
}