Created
January 21, 2019 18:02
-
-
Save ToniTonish/e24c46fb935db6776e8ba9b96673bf29 to your computer and use it in GitHub Desktop.
conway.js, creato dal Prof. Andrea Pollini ( https://www.youtube.com/channel/UC0Ia5TK7poHITGYcKv3tlsA )
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let screen = document.getElementById("screen"); | |
screen.width = 600; | |
screen.height = 400; | |
let ctx = screen.getContext("2d"); | |
let zoomLevelUI = document.getElementById("zoomLevelUI"); | |
let zoomLevel = 1; | |
function setZoomLevel() { | |
zoomLevelUI.innerText = zoomLevel; | |
} | |
setZoomLevel(); | |
class Cell { | |
constructor(x, y) { | |
this.x = x; | |
this.y = y; | |
} | |
getNeighboursXY() { | |
let points = []; | |
for (let i = this.x - 1; i <= this.x + 1; i++) { | |
for (let j = this.y - 1; j <= this.y + 1; j++) { | |
if (i !== this.x || j !== this.y) { | |
points.push({ x: i, y: j }) | |
} | |
} | |
} | |
return points; | |
} | |
} | |
function neighboursOf(x, y) { | |
let points = []; | |
for (let i = x - 1; i <= x + 1; i++) { | |
for (let j = y - 1; j <= y + 1; j++) { | |
if (i !== x || j !== y) { | |
points.push({ x: i, y: j }) | |
} | |
} | |
} | |
return points; | |
} | |
class Grid { | |
constructor() { | |
this.cells = []; | |
} | |
add(cell) { | |
this.cells.push(cell); | |
} | |
// rule1(aliveCount, pos) {} | |
// rule2(aliveCount, pos) {} | |
// rule3(aliveCount, pos) {} | |
// //(rule4(aliveCount, pos) {} | |
// *.###. | |
// *.#+#. | |
// ..###. | |
update() { | |
let newCells = []; | |
this.cells.forEach((cell, pos) => { | |
//console.log(cell, pos) | |
const neigh = cell.getNeighboursXY(); | |
const alive = this.query(neigh); | |
if (alive < 2) { // rule 1 | |
//console.log("RULE 1"); | |
} else if (alive === 2 || alive === 3) { | |
newCells.push(cell); | |
//console.log("RULE 2"); | |
} else if (alive > 3) { | |
//console.log("RULE 3"); | |
} | |
//console.log("neigh=", neigh) | |
//rule4 | |
neigh.forEach(cell => { | |
let n = neighboursOf(cell.x, cell.y); | |
let neighAlive = this.query(n); | |
//console.log("n alive", neighAlive); | |
if (neighAlive === 3) { | |
newCells.push(new Cell(cell.x, cell.y)); | |
//console.log("RULE 4", cell.x, cell.y) | |
} | |
}); | |
}); | |
/** | |
* @Tonish | |
* | |
* Aggiunto codice per la rimozione dei duplicati. | |
* Questo portava il programma ad una crescita | |
* esponenziale delle operazioni. | |
*/ | |
newCells = newCells.filter((point, index, self) => | |
index === self.findIndex((p) => ( | |
p.x === point.x && p.y === point.y | |
)) | |
) | |
this.cells = [...newCells]; | |
} | |
query(locations) { | |
let alive = []; | |
locations.forEach(({ x, y }) => { | |
let p = this.cells.find((c) => { | |
return c.x === x && c.y === y; | |
}) | |
if (p) alive.push(p); | |
}); | |
//console.log(alive); | |
return alive.length; | |
} | |
draw(ctx) { | |
const { width, height } = screen; | |
ctx.clearRect(0, 0, width, height); | |
//ctx.scale(zoomLevel * 0.1, zoomLevel * 0.1); | |
this.cells.forEach(cell => { | |
ctx.fillRect(cell.x, cell.y, zoomLevel, zoomLevel); | |
}); | |
} | |
} | |
let grid = new Grid(); | |
for (let k = 0; k < 1500; k++) | |
grid.add(new Cell(Math.floor(50 + Math.random() * 100), Math.floor(50 + Math.random() * 100))); | |
//grid.add(new Cell(50, 51)); | |
//grid.add(new Cell(50, 52)); | |
//grid.add(new Cell(51, 51)); | |
//grid.add(new Cell(52, 50)); | |
window.addEventListener("keydown", (e) => { | |
if (e.key === '+') zoomLevel += 1; | |
if (e.key === '-') { | |
zoomLevel -= 1; | |
if (zoomLevel < 1) zoomLevel = 1; | |
} | |
setZoomLevel(); | |
}); | |
function draw() { | |
grid.update(); | |
grid.draw(ctx); | |
//setTimeout(requestAnimationFrame, 1000, draw); | |
requestAnimationFrame(draw); | |
//setTimeout(draw, 100); | |
} | |
//setInterval(draw, 1000); | |
requestAnimationFrame(draw); | |
//draw() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Grazie per la correzione del bug! Un contributo importante! Segnalerò la cosa nella prossima live ringraziandoti pubblicamente! 💯