-
Notifications
You must be signed in to change notification settings - Fork 0
/
gol.js
105 lines (97 loc) · 3.36 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
var intervalID;
document.addEventListener('DOMContentLoaded', function(){
document.querySelector("#next").addEventListener('click', nextState);
document.querySelector("#play").addEventListener('click', autoplay);
initialiseGrid(20,20);
})
function autoplay(){
console.log(this.innerHTML)
if (this.innerHTML=="Play"){
this.innerHTML="Pause"
intervalID = setInterval(nextState, 100);
}
else{
console.log("clearing")
clearInterval(intervalID);
this.innerHTML="Play"
}
}
function initialiseGrid(width, height){
let table = document.querySelector('.game-table');
for(let y=0;y<height;y++){
let tr = document.createElement("tr");
for(let x=0;x<width;x++){
let td = document.createElement("td");
td.addEventListener('click', cellClicked);
td.dataset.x=x;
td.dataset.y=y;
tr.appendChild(td);
}
table.appendChild(tr);
}
}
function cellClicked(){
//console.log(this);
if(this.classList.contains("alive")){
this.classList.remove("alive");
}
else{
this.classList.add("alive");
}
}
function nextState(){
//console.log("next state")
let table = document.querySelector('.game-table');
//console.log(table.rows)
for(let y=0;y<table.rows.length;y++){
//console.log("y: "+y)
let tr = table.rows[y];
//console.log(tr)
tr.querySelectorAll('td').forEach(function(td){
//console.log(td)
let numAliveNeighbours = countAliveNeighbours(td);
if(td.classList.contains("alive") && (numAliveNeighbours<2 || numAliveNeighbours>3)){
td.classList.add("dying");
}
else if(!td.classList.contains("alive") && numAliveNeighbours==3){
td.classList.add("born");
}
})
}
//console.log("calculated")
for(let y=0;y<table.rows.length;y++){
let tr = table.rows[y];
tr.querySelectorAll('td').forEach(function(td){
if(td.classList.contains("dying")){
td.classList.remove("dying");
td.classList.remove("alive");
}
else if(td.classList.contains("born")){
td.classList.remove("born");
td.classList.add("alive");
}
})
}
//console.log("complete")
}
function countAliveNeighbours(cell){
//console.log(document.querySelector('[data-x="1"][data-y="1"]'))
let centreX = parseInt(cell.dataset.x)
let centreY = parseInt(cell.dataset.y)
let neighbourCoords = [
[[centreX-1,centreY-1],[centreX,centreY-1],[centreX+1,centreY-1]],
[[centreX-1,centreY],[centreX+1,centreY]],
[[centreX-1,centreY+1],[centreX,centreY+1],[centreX+1,centreY+1]]
]
let numAliveNeighbours=0;
for(let y=0;y<neighbourCoords.length;y++){
let row = neighbourCoords[y]
for(let x=0;x<neighbourCoords[y].length;x++){
let neighbour = document.querySelector(`[data-x="${neighbourCoords[y][x][0]}"][data-y="${neighbourCoords[y][x][1]}"]`)
if(neighbour!=null && neighbour.classList.contains("alive")){
numAliveNeighbours++;
}
}
}
return numAliveNeighbours;
}