Created
April 3, 2022 11:31
-
-
Save akovantsev/b0c47aa197f82c6a8afd9a853205fdbd to your computer and use it in GitHub Desktop.
game of life
This file contains 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
<!DOCTYPE html> | |
<html lang="en"> | |
<head> | |
<meta charset="UTF-8"> | |
<title>Title</title> | |
<style type="text/css"> | |
row { | |
display: block; | |
height: 5px; | |
} | |
cell { | |
width: 5px; | |
height: 5px; | |
display: inline-block; | |
background-color: whitesmoke; | |
} | |
</style> | |
</head> | |
<body> | |
<div id="field"></div> | |
<style id="paint"></style> | |
<script type="application/javascript"> | |
const W = 250; | |
const H = 150; | |
const X = 2000; | |
const T = 200; | |
let field = document.getElementById("field") | |
let style = document.getElementById("paint") | |
let IDS = ""; | |
let zeroes = function () { | |
const ZEROES = []; | |
for (let h=0; h<H; h++) { | |
ZEROES[h] = []; | |
for (let w=0; w<W; w++) { | |
ZEROES[h][w] = 0; | |
} | |
} | |
return ZEROES; | |
} | |
let id = function (h, w) { return "id" + h + "-" + w; } | |
let paint = function () { | |
style.innerText = "foo" + IDS + " {background-color: darkgray}"; | |
IDS = ""; | |
} | |
for (let h=0; h<H; h++) { | |
let row = document.createElement("row"); | |
field.appendChild(row); | |
for (let w=0; w<W; w++) { | |
let cell = document.createElement("cell"); | |
cell.setAttribute("id", id(h, w)); | |
row.appendChild(cell); | |
} | |
} | |
let next = function (NOW, NEXT, h, w) { | |
// const _h = (H + h - 1) % H | |
// const h_ = ( h + 1) % H | |
// const _w = (W + w - 1) % W | |
// const w_ = ( w + 1) % W | |
const _h = (h - 1) | |
const h_ = (h + 1) | |
const _w = (w - 1) | |
const w_ = (w + 1) | |
const sum = NOW[_h][_w] + | |
NOW[_h][w] + | |
NOW[_h][w_] + | |
NOW[h] [_w] + | |
NOW[h] [w_] + | |
NOW[h_][_w] + | |
NOW[h_][w] + | |
NOW[h_][w_]; | |
if (NOW[h][w]) { | |
if (sum === 2 || sum === 3) { | |
IDS = IDS + ",#" + id(h,w); | |
NEXT[h][w] = 1; | |
} else { | |
NEXT[h][w] = 0; | |
} | |
} else { | |
if (sum === 3) { | |
IDS = IDS + ",#" + id(h,w); | |
NEXT[h][w] = 1; | |
} else { | |
NEXT[h][w] = 0; | |
} | |
} | |
} | |
const NOW = zeroes(); | |
for (let x=0;x<X;x++) { | |
let h = Math.floor(Math.random() * H); | |
let w = Math.floor(Math.random() * W); | |
IDS = IDS + ",#" + id(h,w); | |
NOW[h][w] = 1; | |
} | |
let step = function (NOW) { | |
const NEXT = zeroes(); | |
paint() | |
for (let h=1; h<H-1; h++) { | |
for (let w=1; w<W-1; w++) { | |
next(NOW, NEXT, h, w); | |
} | |
} | |
setTimeout(function (){step(NEXT)}, T); | |
} | |
step(NOW); | |
</script> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment