html记录之康威生命游戏(一)
第一版代码内容,有待优化的代码。
html记录之康威生命游戏(二)
html记录之康威生命游戏(三)
生命游戏是康威发明的,一种基于元胞自动机下的一种特殊规则,这个规则很简单,但是很强大,这个规则仿佛赋予了静态的东西有了生命一样在运动,有生存和死亡。
生命游戏规则:
每个细胞的生或者死均由周围的8个细胞决定,如果周围生命过多,将会因为资源不足而死亡,如果周围生命过少将会因为孤独而死亡,只有周围生命刚刚好才能处于生的状态。
规则一:如果周围细胞数大于3或者小于2,将会因为资源不足或者孤独而死。
规则二:如果周围刚好有3个细胞,如果原来是生则不变,如果是死将变为生,即为生命的诞生。
规则三:如果周围刚好有2个细胞,那么无论是生是死都不发生任何改变。
实现代码如下:
<html>
<head>
<meta charset="utf-8">
<meta name="author" content="hwq">
<title>康威生命游戏</title>
<style>
*{
box-sizing: border-box;
}
#content{
width: 1502px;
height: 902px;
border: 1px solid #666;
margin: 0;
position: relative;
}
#zanting{
margin: 0 auto;
}
#restart{
margin: 0 auto;
}
.cell{
border: 1px solid #999;
position: absolute;
}
</style>
</head>
<body>
<button id="zanting">开始暂停</button>
<button id="restart">初始化</button>
<div id="content">
</div>
<script>
console.log("初始化生命游戏");
let zanting = document.getElementById('zanting');//获取暂停按钮dom元素
let restart = document.getElementById('restart');//获取重新开始dom元素
let content = document.getElementById("content");//获取容器dom元素
let width = content.clientWidth;//容器宽度
let height = content.clientHeight;//容器高度
let sizeLeft = 250;//横向格子数
let sizeTop = 150;//纵向格子数
let sizeLeftCal = sizeLeft - 1;//横向坐标上限
let sizeTopCal = sizeTop - 1;//纵向坐标上限
let cellWidth = width/sizeLeft;//单个格子宽度
let cellHeight = height/sizeTop;//单个格子高度
let lifeCount = 0;//存活计数
let clickStatus = true;//是否可点击,开始之后不再点亮格子
let lifeReal = {};//保存所有存活格子,存活放入,死亡移除
let lifeRealJudge = {};//保存所有需要判断周围生存数量的格子
let lifeDead = {};//保存所有需要死亡的格子,死亡放入
let generation = 1;//演化代数
console.log("width:" + width);
console.log("height:" + height);
console.log("sizeLeft:" + sizeLeft);
console.log("sizeTop:" + sizeTop);
console.log("cellWidth:" + cellWidth);
console.log("cellHeight:" + cellHeight);
//格子实时信息0死1生
let cellArray = new Array();
//格子周围实时数量信息(用来判断下一轮生和死的状态)
//也可以不保存实时数量,采用周围8个格子的方式判断生和死
let cellLife = new Array();
//初始化
createCells();
//新建格子
function createCells(){
for(let i=0; i<sizeLeft; i++){
cellArray[i] = new Array();
cellLife[i] = new Array();
for(let j=0; j<sizeTop; j++){
createCell(i, j);
cellArray[i][j] = 0;
cellLife[i][j] = 0;
}
}
}
//新建格子
function createCell(i, j){
let div = document.createElement("div");
div.id = "cell-" + i + "-" + j;
div.style.width = cellWidth;
div.style.height = cellHeight;
div.style.left = (cellWidth*i) + "px";
div.style.top = (cellHeight*j) + "px";
// div.style.fontSize = 5;
// div.innerHTML = i + "" + j;
div.setAttribute("class", "cell");
div.addEventListener("click", colorChange, false);//绑定格子颜色转换事件
content.appendChild(div);
}
//格子颜色转换事件
function colorChange(){
if(!clickStatus){
return;
}
console.log(this.id);
let arr = this.id.split("-");
let i = arr[1] * 1;
let j = arr[2] * 1;
if(this.style.backgroundColor){
this.style.backgroundColor = "";
if(cellArray[i][j] == 1){
lifeNumber(i, j, -1, cellLife);
lifeCount--;
delete lifeReal["cell-" + i + "-" + j];
// delete lifeRealJudge["cell-" + i + "-" + j];
// delete lifeRealJudge["cell-" + i + "-" + (j-1)];
// delete lifeRealJudge["cell-" + i + "-" + (j+1)];
// delete lifeRealJudge["cell-" + (i-1) + "-" + j];
// delete lifeRealJudge["cell-" + (i-1) + "-" + (j-1)];
// delete lifeRealJudge["cell-" + (i-1) + "-" + (j+1)];
// delete lifeRealJudge["cell-" + (i+1) + "-" + j];
// delete lifeRealJudge["cell-" + (i+1) + "-" + (j-1)];
// delete lifeRealJudge["cell-" + (i+1) + "-" + (j+1)];
}
cellArray[i][j] = 0;
}else{
this.style.backgroundColor = "#333";
if(cellArray[i][j] == 0){
lifeNumber(i, j, 1, cellLife);
lifeCount++;
lifeReal["cell-" + i + "-" + j] = 1;
// lifeRealJudge["cell-" + i + "-" + j] = 1;
// lifeRealJudge["cell-" + i + "-" + (j-1)] = 1;
// lifeRealJudge["cell-" + i + "-" + (j+1)] = 1;
// lifeRealJudge["cell-" + (i-1) + "-" + j] = 1;
// lifeRealJudge["cell-" + (i-1) + "-" + (j-1)] = 1;
// lifeRealJudge["cell-" + (i-1) + "-" + (j+1)] = 1;
// lifeRealJudge["cell-" + (i+1) + "-" + j] = 1;
// lifeRealJudge["cell-" + (i+1) + "-" + (j-1)] = 1;
// lifeRealJudge["cell-" + (i+1) + "-" + (j+1)] = 1;
}
cellArray[i][j] = 1;
}
}
//初始化
restart.addEventListener("click", reStart, false);
function reStart(){
if(!(typeof timer === "undefined")){
clearInterval(timer);//清空循环控制器
}
clickStatus = true;
generation = 0;
lifeReal = {};
lifeRealJudge = {};
for(let i=0; i<sizeLeft; i++){
cellArray[i] = new Array();
cellLife[i] = new Array();
for(let j=0; j<sizeTop; j++){
cellArray[i][j] = 0;
cellLife[i][j] = 0;
document.getElementById("cell-" + i + "-" + j).style.backgroundColor = "";
}
}
}
//开始生命游戏
zanting.addEventListener("click", start, false);
function start(){
// let cellLifeNew = new Array();
// for(let i=0; i<sizeLeft; i++){
// cellLifeNew[i] = new Array();
// for(let j=0; j<sizeTop; j++){
// console.log("life game");
// let num = cellLife[i][j];
// if(num == 3) {
// cellArray[i][j] = 1;
// lifeNumber(i, j, 1);
// }else if(num > 3 || num < 2){
// cellArray[i][j] = 0;
// lifeNumber(i, j, -1);
// }
// }
// }
console.log("开始生命游戏");
if(lifeCount == 0){
clickStatus = true;
console.log("没有存活的生命体,请先激活");
return;
}
clickStatus = false;//开始生命游戏之后不能点亮格子
var count = 0;
let swap = false;
let cellLifeNew = new Array();
for(let x=0; x<sizeLeft; x++){
cellLifeNew[x] = new Array();
for(let y=0; y<sizeTop; y++){
cellLifeNew[x][y] = cellLife[x][y];
}
}
let arr;
let k;
let q;
//获取所有需要判断的格子
let keys = Object.keys(lifeReal);
for(let con=0; con<keys.length; con++){
arr = keys[con].split("-");
k = arr[1] * 1;
q = arr[2] * 1;
lifeRealJudge["cell-" + k + "-" + q] = 1;
lifeRealJudge["cell-" + k + "-" + (q-1)] = 1;
lifeRealJudge["cell-" + k + "-" + (q+1)] = 1;
lifeRealJudge["cell-" + (k-1) + "-" + q] = 1;
lifeRealJudge["cell-" + (k-1) + "-" + (q-1)] = 1;
lifeRealJudge["cell-" + (k-1) + "-" + (q+1)] = 1;
lifeRealJudge["cell-" + (k+1) + "-" + q] = 1;
lifeRealJudge["cell-" + (k+1) + "-" + (q-1)] = 1;
lifeRealJudge["cell-" + (k+1) + "-" + (q+1)] = 1;
}
let lifeKeys = Object.keys(lifeRealJudge);
let length = lifeKeys.length;
let num;
var timer = setInterval(() => {
// console.log("1");
// k = Math.floor(count / sizeLeft);
// q = Math.floor(count % sizeTop);
if(lifeCount == 0){//没有存活
clearInterval(timer);//清空循环控制器
clickStatus = true;
console.log("没有存活结束,当前第" + generation + "代");
generation = 0;
return;
}
if(count >= length){
if(swap == false){
clearInterval(timer);//清空循环控制器
clickStatus = true;
console.log("没有变换结束,当前第" + generation + "代");
generation = 0;
return;
}
cellLife = cellLifeNew;
cellLifeNew = new Array();
for(let x=0; x<sizeLeft; x++){
cellLifeNew[x] = new Array();
for(let y=0; y<sizeTop; y++){
cellLifeNew[x][y] = cellLife[x][y];
}
}
//重新渲染格子
let keysDead = Object.keys(lifeDead);
for(let con=0; con<keysDead.length; con++){
arr = keysDead[con].split("-");
k = arr[1] * 1;
q = arr[2] * 1;
document.getElementById("cell-" + k + "-" + q).style.backgroundColor = "";
}
lifeDead = {};
//获取所有需要判断的格子
lifeRealJudge = {};
let keys = Object.keys(lifeReal);
console.log("lifeReal.size=" + keys.length);
for(let con=0; con<keys.length; con++){
arr = keys[con].split("-");
k = arr[1] * 1;
q = arr[2] * 1;
lifeRealJudge["cell-" + k + "-" + q] = 1;
lifeRealJudge["cell-" + k + "-" + (q-1)] = 1;
lifeRealJudge["cell-" + k + "-" + (q+1)] = 1;
lifeRealJudge["cell-" + (k-1) + "-" + q] = 1;
lifeRealJudge["cell-" + (k-1) + "-" + (q-1)] = 1;
lifeRealJudge["cell-" + (k-1) + "-" + (q+1)] = 1;
lifeRealJudge["cell-" + (k+1) + "-" + q] = 1;
lifeRealJudge["cell-" + (k+1) + "-" + (q-1)] = 1;
lifeRealJudge["cell-" + (k+1) + "-" + (q+1)] = 1;
document.getElementById("cell-" + k + "-" + q).style.backgroundColor = "#333";
}
lifeKeys = Object.keys(lifeRealJudge);
length = lifeKeys.length;
console.log("lifeRealJudge.size=" + length);
count = 0;
swap = false;
console.log("结束一轮,当前第" + generation + "代");
generation++;
}
if(count >= lifeKeys.length){
clearInterval(timer);
clickStatus = true;
console.log("出现异常,请检查");
return;
}
arr = lifeKeys[count].split("-");
k = arr[1] * 1;
q = arr[2] * 1;
// if(k == sizeLeft){
// if(swap == false){
// clickStatus = true;
// clearInterval(timer);//清空循环控制器
// console.log("没有变换结束");
// }
// count = 0;
// k = Math.floor(count / sizeLeft);
// swap = false;
// cellLife = cellLifeNew;
// cellLifeNew = new Array();
// for(let x=0; x<sizeLeft; x++){
// cellLifeNew[x] = new Array();
// }
// console.log("结束一轮");
// }
if(k < 0 || k >= sizeLeft || q < 0 || q >= sizeTop){
count++;
return;
}
num = cellLife[k][q];
// console.log("cell-" + k + "-" + q + "周围存活数量为:" + num);
if(num == 3) {
if(cellArray[k][q] == 0){
lifeNumber(k, q, 1, cellLifeNew);
lifeCount++;
swap = true;
lifeReal["cell-" + k + "-" + q] = 1;
// lifeRealJudge["cell-" + k + "-" + q] = 1;
// lifeRealJudge["cell-" + k + "-" + (q-1)] = 1;
// lifeRealJudge["cell-" + k + "-" + (q+1)] = 1;
// lifeRealJudge["cell-" + (k-1) + "-" + q] = 1;
// lifeRealJudge["cell-" + (k-1) + "-" + (q-1)] = 1;
// lifeRealJudge["cell-" + (k-1) + "-" + (q+1)] = 1;
// lifeRealJudge["cell-" + (k+1) + "-" + q] = 1;
// lifeRealJudge["cell-" + (k+1) + "-" + (q-1)] = 1;
// lifeRealJudge["cell-" + (k+1) + "-" + (q+1)] = 1;
cellArray[k][q] = 1;
// document.getElementById("cell-" + k + "-" + q).style.backgroundColor = "#333";
}
}else if(num > 3 || num < 2){
if(cellArray[k][q] == 1){
lifeNumber(k, q, -1, cellLifeNew);
lifeCount--;
swap = true;
delete lifeReal["cell-" + k + "-" + q];
lifeDead["cell-" + k + "-" + q] = 1;
// delete lifeRealJudge["cell-" + k + "-" + q];
// console.log("删除了cell-" + k + "-" + q);
// delete lifeRealJudge["cell-" + k + "-" + (q-1)];
// delete lifeRealJudge["cell-" + k + "-" + (q+1)];
// delete lifeRealJudge["cell-" + (k-1) + "-" + q];
// delete lifeRealJudge["cell-" + (k-1) + "-" + (q-1)];
// delete lifeRealJudge["cell-" + (k-1) + "-" + (q+1)];
// delete lifeRealJudge["cell-" + (k+1) + "-" + q];
// delete lifeRealJudge["cell-" + (k+1) + "-" + (q-1)];
// delete lifeRealJudge["cell-" + (k+1) + "-" + (q+1)];
cellArray[k][q] = 0;
// document.getElementById("cell-" + k + "-" + q).style.backgroundColor = "";
}
}
count++;
}, 0);
}
//生存数量计算
function lifeNumber(i, j, inc, cellLifeArray){
i = i * 1;
j = j * 1;
// console.log("cell-" + i + "-" + j + "生存数量计算,inc:" + inc);
if(i !=0 && j != 0 && i != sizeLeftCal && j != sizeTopCal){//如果不为边缘格子,这个判断范围最大,只判断一次的期望值高
cellLifeArray[i-1][j] = cellLifeArray[i-1][j] * 1 + inc;//左
cellLifeArray[i-1][j-1] = cellLifeArray[i-1][j-1] * 1 + inc;//左上
cellLifeArray[i-1][j+1] = cellLifeArray[i-1][j+1] * 1 + inc;//左下
cellLifeArray[i][j-1] = cellLifeArray[i][j-1] * 1 + inc;//上
cellLifeArray[i][j+1] = cellLifeArray[i][j+1] * 1 + inc;//下
cellLifeArray[i+1][j] = cellLifeArray[i+1][j] * 1 + inc;//右
cellLifeArray[i+1][j-1] = cellLifeArray[i+1][j-1] * 1 + inc;//右上
cellLifeArray[i+1][j+1] = cellLifeArray[i+1][j+1] * 1 + inc;//右下
// console.log("cellLifeArray[" + (i-1) + "][" + j + "]=" + cellLifeArray[i-1][j]);
// console.log("cellLifeArray[" + (i-1) + "][" + (j-1) + "]=" + cellLifeArray[i-1][j-1]);
// console.log("cellLifeArray[" + (i-1) + "][" + (j+1) + "]=" + cellLifeArray[i-1][j+1]);
// console.log("cellLifeArray[" + i + "][" + (j-1) + "]=" + cellLifeArray[i][j-1]);
// console.log("cellLifeArray[" + i + "][" + (j+1) + "]=" + cellLifeArray[i][j+1]);
// console.log("cellLifeArray[" + (i+1) + "][" + j + "]=" + cellLifeArray[i+1][j]);
// console.log("cellLifeArray[" + (i+1) + "][" + (j-1) + "]=" + cellLifeArray[i+1][j-1]);
// console.log("cellLifeArray[" + (i+1) + "][" + (j+1) + "]=" + cellLifeArray[i+1][j+1]);
}else if(i == 0 && j != 0 && j != sizeTopCal){//如果为左边一片,只有上、右上、右、右下、下
cellLifeArray[i][j-1] = cellLifeArray[i][j-1] * 1 + inc;//上
cellLifeArray[i][j+1] = cellLifeArray[i][j+1] * 1 + inc;//下
cellLifeArray[i+1][j] = cellLifeArray[i+1][j] * 1 + inc;//右
cellLifeArray[i+1][j-1] = cellLifeArray[i+1][j-1] * 1 + inc;//右上
cellLifeArray[i+1][j+1] = cellLifeArray[i+1][j+1] * 1 + inc;//右下
}else if(j == 0 && i != 0 && i != sizeLeftCal){//如果为上边一片,只有左、左下、下、右下、右
cellLifeArray[i-1][j] = cellLifeArray[i-1][j] * 1 + inc;//左
cellLifeArray[i-1][j+1] = cellLifeArray[i-1][j+1] * 1 + inc;//左下
cellLifeArray[i][j+1] = cellLifeArray[i][j+1] * 1 + inc;//下
cellLifeArray[i+1][j] = cellLifeArray[i+1][j] * 1 + inc;//右
cellLifeArray[i+1][j+1] = cellLifeArray[i+1][j+1] * 1 + inc;//右下
}else if(i == sizeLeftCal && j != 0 && j != sizeTopCal){//如果为右边一片,只有上、左上、左、左下、下
cellLifeArray[i-1][j] = cellLifeArray[i-1][j] * 1 + inc;//左
cellLifeArray[i-1][j-1] = cellLifeArray[i-1][j-1] * 1 + inc;//左上
cellLifeArray[i-1][j+1] = cellLifeArray[i-1][j+1] * 1 + inc;//左下
cellLifeArray[i][j-1] = cellLifeArray[i][j-1] * 1 + inc;//上
cellLifeArray[i][j+1] = cellLifeArray[i][j+1] * 1 + inc;//下
}else if(j == sizeTopCal && i != 0 && i != sizeLeftCal){//如果为下边一片,只有左、左上、上、右上、右
cellLifeArray[i-1][j] = cellLifeArray[i-1][j] * 1 + inc;//左
cellLifeArray[i-1][j-1] = cellLifeArray[i-1][j-1] * 1 + inc;//左上
cellLifeArray[i][j-1] = cellLifeArray[i][j-1] * 1 + inc;//上
cellLifeArray[i+1][j] = cellLifeArray[i+1][j] * 1 + inc;//右
cellLifeArray[i+1][j-1] = cellLifeArray[i+1][j-1] * 1 + inc;//右上
}else if(i == 0 && j == 0){//如果为左上角,只有右、右下、下
// cellLifeArray[0][1] = cellLifeArray[0][1] + inc;
// cellLifeArray[1][0] = cellLifeArray[1][0] + inc;
// cellLifeArray[1][1] = cellLifeArray[1][1] + inc;
cellLifeArray[i+1][j] = cellLifeArray[i+1][j] * 1 + inc;//右
cellLifeArray[i+1][j+1] = cellLifeArray[i+1][j+1] * 1 + inc;//右下
cellLifeArray[i][j+1] = cellLifeArray[i][j+1] * 1 + inc;//下
}else if(i == sizeLeftCal && j == 0){//如果为右上角,只有左、左下、下
cellLifeArray[i-1][j] = cellLifeArray[i-1][j] * 1 + inc;//左
cellLifeArray[i-1][j+1] = cellLifeArray[i-1][j+1] * 1 + inc;//左下
cellLifeArray[i][j+1] = cellLifeArray[i][j+1] * 1 + inc;//下
}else if(i == 0 && j == sizeTopCal){//如果为左下角,只有上、右上、右
cellLifeArray[i][j-1] = cellLifeArray[i][j-1] * 1 + inc;//上
cellLifeArray[i+1][j-1] = cellLifeArray[i+1][j-1] * 1 + inc;//右上
cellLifeArray[i+1][j] = cellLifeArray[i+1][j] * 1 + inc;//右
}else if(i == sizeLeftCal && j == sizeTopCal){//如果为右下角,只有上、左上、左
cellLifeArray[i][j-1] = cellLifeArray[i][j-1] * 1 + inc;//上
cellLifeArray[i-1][j-1] = cellLifeArray[i-1][j-1] * 1 + inc;//左上
cellLifeArray[i-1][j] = cellLifeArray[i-1][j] * 1 + inc;//左
}
}
</script>
</body>
</html>
var foo = 'bar';
预览效果如下:
仅做记录,第一版代码因为每个判断都是定时器,后面格子多了会很慢