update last board, new rules
This commit is contained in:
parent
beab487429
commit
2d57acd0a5
@ -22,6 +22,8 @@
|
|||||||
createBoard,
|
createBoard,
|
||||||
conwayRules,
|
conwayRules,
|
||||||
overpopulationRules,
|
overpopulationRules,
|
||||||
|
lonelinessRules,
|
||||||
|
threebornRules,
|
||||||
evolve2d,
|
evolve2d,
|
||||||
} from "../modules/automata.js";
|
} from "../modules/automata.js";
|
||||||
import { getRandomInt, sleep } from "../modules/common.js";
|
import { getRandomInt, sleep } from "../modules/common.js";
|
||||||
@ -38,6 +40,8 @@
|
|||||||
available2dRules: {
|
available2dRules: {
|
||||||
conway: conwayRules,
|
conway: conwayRules,
|
||||||
overpopulation: overpopulationRules,
|
overpopulation: overpopulationRules,
|
||||||
|
loneliness: lonelinessRules,
|
||||||
|
threeborn: threebornRules,
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
},
|
},
|
||||||
@ -89,9 +93,10 @@
|
|||||||
mounted() {
|
mounted() {
|
||||||
this.canvas = Object.freeze(document.getElementById("board-canvas"));
|
this.canvas = Object.freeze(document.getElementById("board-canvas"));
|
||||||
this.workCanvas = Object.freeze(document.getElementById("work-canvas"));
|
this.workCanvas = Object.freeze(document.getElementById("work-canvas"));
|
||||||
this.ctx = this.canvas.getContext("2d");
|
this.ctx = this.canvas.getContext("2d", { willReadFrequently: true });
|
||||||
this.workCtx = this.workCanvas.getContext("2d");
|
this.workCtx = this.workCanvas.getContext("2d", {
|
||||||
this.workCanvas.setAttribute("willReadFrequently", true);
|
willReadFrequently: true,
|
||||||
|
});
|
||||||
this.canvasWidth = this.canvas.parentElement.clientWidth;
|
this.canvasWidth = this.canvas.parentElement.clientWidth;
|
||||||
this.canvasHeight = this.canvas.parentElement.clientHeight;
|
this.canvasHeight = this.canvas.parentElement.clientHeight;
|
||||||
this.setBoardWidth();
|
this.setBoardWidth();
|
||||||
@ -135,7 +140,7 @@
|
|||||||
this.drawCanvas(this.lastBoard);
|
this.drawCanvas(this.lastBoard);
|
||||||
this.toggleStop();
|
this.toggleStop();
|
||||||
},
|
},
|
||||||
// draw 2D automaton on the canvas (currently only the game of life)
|
// draw 2D automaton on the canvas in a loop
|
||||||
draw2d(board) {
|
draw2d(board) {
|
||||||
if (!this.canDraw) return;
|
if (!this.canDraw) return;
|
||||||
const draw2dNext = async (b) => {
|
const draw2dNext = async (b) => {
|
||||||
@ -145,6 +150,7 @@
|
|||||||
this.available2dRules[this.selected2dRules.id]
|
this.available2dRules[this.selected2dRules.id]
|
||||||
);
|
);
|
||||||
this.drawCanvas(b, this.cellProperties);
|
this.drawCanvas(b, this.cellProperties);
|
||||||
|
this.lastBoard = Object.freeze(newBoard);
|
||||||
await sleep(this.refreshRate);
|
await sleep(this.refreshRate);
|
||||||
draw2dNext(newBoard);
|
draw2dNext(newBoard);
|
||||||
};
|
};
|
||||||
|
@ -91,6 +91,28 @@ function conwayRules(cell, neighbors) {
|
|||||||
return cell;
|
return cell;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// variation on the game of life's rules,
|
||||||
|
// where the "three live neighbors" rule is ignored
|
||||||
|
function threebornRules(cell, neighbors) {
|
||||||
|
// loneliness rule
|
||||||
|
if (cell === 1 && neighbors < 2) return 0;
|
||||||
|
// overpopulation rule
|
||||||
|
if (cell === 1 && neighbors > 3) return 0;
|
||||||
|
// born when three live neighbors rule
|
||||||
|
return cell;
|
||||||
|
}
|
||||||
|
|
||||||
|
// variation on the game of life's rules,
|
||||||
|
// where the loneliness rule is ignored
|
||||||
|
function lonelinessRules(cell, neighbors) {
|
||||||
|
// overpopulation rule
|
||||||
|
if (cell === 1 && neighbors > 3) return 0;
|
||||||
|
// born when three live neighbors rule
|
||||||
|
if (cell === 0 && neighbors === 3) return 1;
|
||||||
|
// the cell remains the same if none apply
|
||||||
|
return cell;
|
||||||
|
}
|
||||||
|
|
||||||
// variation on the game of life's rules,
|
// variation on the game of life's rules,
|
||||||
// where the overpopulation rule is ignored
|
// where the overpopulation rule is ignored
|
||||||
function overpopulationRules(cell, neighbors) {
|
function overpopulationRules(cell, neighbors) {
|
||||||
@ -160,6 +182,8 @@ export {
|
|||||||
create1dStateOneCell,
|
create1dStateOneCell,
|
||||||
conwayRules,
|
conwayRules,
|
||||||
overpopulationRules,
|
overpopulationRules,
|
||||||
|
lonelinessRules,
|
||||||
|
threebornRules,
|
||||||
evolve1d,
|
evolve1d,
|
||||||
evolve2d,
|
evolve2d,
|
||||||
};
|
};
|
||||||
|
@ -104,6 +104,18 @@ const preset2dRules = [
|
|||||||
description:
|
description:
|
||||||
"Variation on Conway's Game of Life *without* the overpopulation rule",
|
"Variation on Conway's Game of Life *without* the overpopulation rule",
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
id: "loneliness",
|
||||||
|
name: "Loneliness variation",
|
||||||
|
description:
|
||||||
|
"Variation on Conway's Game of Life *without* the loneliness rule",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
id: "threeborn",
|
||||||
|
name: "Three lives variation",
|
||||||
|
description:
|
||||||
|
"Variation on Conway's Game of Life *without* the 'three live neighbors' rule",
|
||||||
|
},
|
||||||
];
|
];
|
||||||
|
|
||||||
export { presetRuleset, initialStates, preset2dRules };
|
export { presetRuleset, initialStates, preset2dRules };
|
||||||
|
Loading…
Reference in New Issue
Block a user