working 2D CA functions + canvas draws in one go
This commit is contained in:
@ -1,43 +1,45 @@
|
||||
// get the next evolution of a 1D CA
|
||||
function evolve(state, rules) {
|
||||
function evolveAcc(s, acc) {
|
||||
const [x, y, z, ...xs] = s;
|
||||
if (!xs.length) {
|
||||
return acc[acc.length - 1] + acc + acc[0];
|
||||
}
|
||||
|
||||
const rule = x + y + z;
|
||||
const newAcc = acc.concat(rules[rule]);
|
||||
return evolveAcc(y + z + xs.join(''), newAcc);
|
||||
}
|
||||
return evolveAcc(state, '');
|
||||
// handles negative index and index bigger than its array length
|
||||
function guard(index, array) {
|
||||
if (index > (array.length - 1)) return 0;
|
||||
if (index < 0) return (array.length - 1);
|
||||
return index;
|
||||
}
|
||||
|
||||
// create a 2D board from a 1D initial state
|
||||
// get the next evolution of a 1D CA initial state
|
||||
function evolve(state, rules) {
|
||||
function getCell(index) {
|
||||
const safeIndex = guard(index, state);
|
||||
return state[safeIndex];
|
||||
}
|
||||
const newState = state.map((_, x) => {
|
||||
const cells = [
|
||||
getCell(x - 1),
|
||||
getCell(x),
|
||||
getCell(x + 1)];
|
||||
return rules[cells.join('')];
|
||||
});
|
||||
|
||||
return newState.map(Number);
|
||||
}
|
||||
|
||||
// create a 2D board from a 1D CA initial state
|
||||
function createBoard(state, rules, height) {
|
||||
function createBoardAcc(s, h, acc) {
|
||||
if (h === 0) return acc;
|
||||
const newState = evolve(state, rules);
|
||||
const newAcc = acc.concat(state);
|
||||
const newState = evolve(s, rules);
|
||||
const newAcc = acc.concat([s]);
|
||||
return createBoardAcc(newState, h - 1, newAcc);
|
||||
}
|
||||
return createBoardAcc(state, height, []).map(
|
||||
(row) => row.split(''))
|
||||
return createBoardAcc(state, height, []);
|
||||
}
|
||||
|
||||
// Find the neighbor of a given cell in a 2D CA board
|
||||
function getCellNeighbors(board, cellCoordinates) {
|
||||
const [x, y] = cellCoordinates;
|
||||
const rowLength = board[0].length; // caca?
|
||||
|
||||
// handles board edges where the cell is missing neighbors
|
||||
function getCell(xx, yy) {
|
||||
// handles negative index and index bigger than its array length
|
||||
function guard(index, array) {
|
||||
if (index > array.length - 1) return 0;
|
||||
if (index < 0) return array.length - 1;
|
||||
return index;
|
||||
}
|
||||
|
||||
const safeX = guard(xx, board);
|
||||
const safeY = guard(yy, rowLength);
|
||||
return board[safeX][safeY];
|
||||
@ -52,10 +54,13 @@ function getCellNeighbors(board, cellCoordinates) {
|
||||
];
|
||||
}
|
||||
|
||||
// Sums the value of a cell's neighbors
|
||||
function getNeighborsSum(cells) {
|
||||
return cells.reduce((cell, acc) => cell + acc, 0);
|
||||
}
|
||||
|
||||
// Get the next evolution of a cell according to
|
||||
// Conway's game of life rules
|
||||
function conwayRules(cell, neighbors) {
|
||||
// loneliness rule
|
||||
if (cell === 1 && neighbors < 2) return 0;
|
||||
@ -67,6 +72,7 @@ function conwayRules(cell, neighbors) {
|
||||
return cell;
|
||||
}
|
||||
|
||||
// get the next evolution of a 2D CA initial state
|
||||
function evolve2d(board, rulesFn) {
|
||||
return board.map((row, x) => row.map((cell, y) => {
|
||||
const neighbors = getCellNeighbors(board, [x, y]);
|
||||
@ -93,15 +99,21 @@ function getDrawingValues(state, acc, cell) {
|
||||
);
|
||||
}
|
||||
|
||||
// Populates the first state of a 1D CA with cells returned
|
||||
// by initFn
|
||||
function initialState1d(width, initFn, args) {
|
||||
return [...Array(width)].map(
|
||||
() => initFn(...args).toString(),
|
||||
).join('');
|
||||
() => initFn(...args),
|
||||
);
|
||||
}
|
||||
|
||||
// Populates the first state of a 2D CA with cells returned
|
||||
// by initFn
|
||||
function initialState2d(width, height, initFn, args) {
|
||||
return [...Array(height)].map(
|
||||
() => [...Array(width)].map(() => initFn(...args)),
|
||||
() => [...Array(width)].map(
|
||||
() => initFn(...args),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user