fixes empty initial step and board dimensions
This commit is contained in:
parent
a567b565e9
commit
ed4dc5c2eb
@ -47,6 +47,10 @@
|
|||||||
canvasHeight: "canvasHeight",
|
canvasHeight: "canvasHeight",
|
||||||
getReset: "reset",
|
getReset: "reset",
|
||||||
}),
|
}),
|
||||||
|
// used to determine the dimensions of the board
|
||||||
|
max() {
|
||||||
|
return Math.max(this.boardWidth, this.boardHeight);
|
||||||
|
},
|
||||||
},
|
},
|
||||||
watch: {
|
watch: {
|
||||||
getDraw1d(value) {
|
getDraw1d(value) {
|
||||||
@ -76,6 +80,7 @@
|
|||||||
"setBoardWidth",
|
"setBoardWidth",
|
||||||
"setBoardHeight",
|
"setBoardHeight",
|
||||||
]),
|
]),
|
||||||
|
// draws the board on the canvas
|
||||||
drawCanvas(board) {
|
drawCanvas(board) {
|
||||||
const props = this.cellProperties;
|
const props = this.cellProperties;
|
||||||
board.map((row, y) => {
|
board.map((row, y) => {
|
||||||
@ -92,22 +97,22 @@
|
|||||||
});
|
});
|
||||||
});
|
});
|
||||||
},
|
},
|
||||||
|
// create a first state, either a single living cell
|
||||||
|
// at the center or random ones
|
||||||
compute1dInitialState() {
|
compute1dInitialState() {
|
||||||
if (this.initial1dState === "onecell")
|
if (this.initial1dState === "onecell")
|
||||||
return create1dStateOneCell(this.boardWidth);
|
return create1dStateOneCell(this.boardWidth);
|
||||||
return create1dState(this.boardWidth, getRandomInt, [0, 2]);
|
return create1dState(this.boardWidth, getRandomInt, [0, 2]);
|
||||||
},
|
},
|
||||||
|
// draw elementary automaton on the canvas based on selected ruleset
|
||||||
draw1d() {
|
draw1d() {
|
||||||
const initialState = this.compute1dInitialState();
|
const initialState = this.compute1dInitialState();
|
||||||
const board = createBoard(
|
const board = createBoard(initialState, this.ruleset.rules, this.max);
|
||||||
initialState,
|
|
||||||
this.ruleset.rules,
|
|
||||||
this.boardWidth
|
|
||||||
);
|
|
||||||
this.lastBoard = Object.freeze(board);
|
this.lastBoard = Object.freeze(board);
|
||||||
this.drawCanvas(board);
|
this.drawCanvas(board);
|
||||||
this.toggleStop();
|
this.toggleStop();
|
||||||
},
|
},
|
||||||
|
// draw 2D automaton on the canvas (currently only the game of life)
|
||||||
draw2d(board) {
|
draw2d(board) {
|
||||||
if (!this.canDraw) return;
|
if (!this.canDraw) return;
|
||||||
const draw2dNext = async (b) => {
|
const draw2dNext = async (b) => {
|
||||||
@ -119,6 +124,7 @@
|
|||||||
};
|
};
|
||||||
return draw2dNext(board);
|
return draw2dNext(board);
|
||||||
},
|
},
|
||||||
|
// draw 2d automaton from a new state
|
||||||
draw2dNew() {
|
draw2dNew() {
|
||||||
const initialState = create2dState(
|
const initialState = create2dState(
|
||||||
this.boardWidth,
|
this.boardWidth,
|
||||||
@ -130,9 +136,11 @@
|
|||||||
this.lastBoard = Object.freeze(board);
|
this.lastBoard = Object.freeze(board);
|
||||||
this.draw2d(board);
|
this.draw2d(board);
|
||||||
},
|
},
|
||||||
|
// draw 2d automaton from the last known generated board
|
||||||
async draw2dLast() {
|
async draw2dLast() {
|
||||||
if (this.lastBoard != undefined) this.draw2d(this.lastBoard);
|
if (this.lastBoard != undefined) this.draw2d(this.lastBoard);
|
||||||
},
|
},
|
||||||
|
// stop drawing routines and clear the canvas
|
||||||
reset() {
|
reset() {
|
||||||
this.toggleStop();
|
this.toggleStop();
|
||||||
this.ctx.clearRect(0, 0, this.canvas.width, this.canvas.height);
|
this.ctx.clearRect(0, 0, this.canvas.width, this.canvas.height);
|
||||||
|
@ -25,7 +25,6 @@
|
|||||||
emits: ["update-active"],
|
emits: ["update-active"],
|
||||||
computed: {
|
computed: {
|
||||||
isActive() {
|
isActive() {
|
||||||
console.log(this.rowTitle, "vs", this.active);
|
|
||||||
return this.rowTitle == this.active;
|
return this.rowTitle == this.active;
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
|
@ -31,11 +31,12 @@ function evolve1d(state, rules) {
|
|||||||
// }
|
// }
|
||||||
|
|
||||||
// performance "choke point" in full imperative
|
// performance "choke point" in full imperative
|
||||||
function createBoard(state, rules, height) {
|
function createBoard(state, rules, max) {
|
||||||
var board = [];
|
var board = [];
|
||||||
let prevState = [];
|
let prevState = [];
|
||||||
for (let i = 0; i < height; i++) {
|
for (let i = 0; i < max; i++) {
|
||||||
let nextState = [];
|
let nextState = [];
|
||||||
|
// use the passed initial step during first iteration
|
||||||
if (i == 0) {
|
if (i == 0) {
|
||||||
nextState = evolve1d(state, rules);
|
nextState = evolve1d(state, rules);
|
||||||
} else {
|
} else {
|
||||||
@ -121,7 +122,7 @@ function getDrawingValues(state, acc, cell) {
|
|||||||
// Populates the first state with a single living cell in the center
|
// Populates the first state with a single living cell in the center
|
||||||
function create1dStateOneCell(width) {
|
function create1dStateOneCell(width) {
|
||||||
return [...Array(width)].map((cell, index) => {
|
return [...Array(width)].map((cell, index) => {
|
||||||
if (index === width / 2 || index === width + 1 / 2) return 1;
|
if (index === Math.floor(width / 2)) return 1;
|
||||||
return 0;
|
return 0;
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user