[wip] game of life, 1D to 2d
This commit is contained in:
@ -1,13 +1,59 @@
|
||||
// TODO: Hide accumulator inside
|
||||
function evolve(state, acc, rules) {
|
||||
const [x, y, z, ...xs] = state;
|
||||
if (!xs.length) {
|
||||
return acc[acc.length - 1] + acc + acc[0];
|
||||
// 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, '');
|
||||
}
|
||||
|
||||
// create a 2D board from a 1D 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);
|
||||
return createBoardAcc(newState, h - 1, newAcc);
|
||||
}
|
||||
return createBoardAcc(state, height, []).map(
|
||||
(row) => row.split(''))
|
||||
}
|
||||
|
||||
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];
|
||||
}
|
||||
|
||||
const rule = x + y + z;
|
||||
const newAcc = acc.concat(rules[rule]);
|
||||
return evolve(y + z + xs.join(''), newAcc, rules);
|
||||
// the current cell is not included in the result
|
||||
return [
|
||||
getCell(x - 1, y - 1), getCell(x, y - 1),
|
||||
getCell(x + 1, y - 1), getCell(x - 1, y),
|
||||
getCell(x + 1, y), getCell(x - 1, y + 1),
|
||||
getCell(x, y + 1), getCell(x + 1, y - 1),
|
||||
];
|
||||
}
|
||||
|
||||
function getNeighborsSum(cells) {
|
||||
return cells.reduce((cell, acc) => cell + acc, 0);
|
||||
}
|
||||
|
||||
function conwayRules(cell, neighbors) {
|
||||
@ -21,11 +67,23 @@ function conwayRules(cell, neighbors) {
|
||||
return cell;
|
||||
}
|
||||
|
||||
function evolve2d(board, rulesFn) {
|
||||
return board.map((row, x) => row.map((cell, y) => {
|
||||
const neighbors = getCellNeighbors(board, [x, y]);
|
||||
const sum = getNeighborsSum(neighbors);
|
||||
return rulesFn(cell, sum);
|
||||
}));
|
||||
}
|
||||
|
||||
function getDrawingValues(state, acc, cell) {
|
||||
const d = cell.dimension;
|
||||
return Object.keys(state).map(
|
||||
(key) => {
|
||||
const fillStyle = state[key] === '1' ? cell.liveColor : cell.deadColor;
|
||||
const fillStyle = (() => {
|
||||
if (state[key] === '1') return cell.liveColor;
|
||||
return cell.deadColor;
|
||||
})();
|
||||
|
||||
return {
|
||||
move: [key * d, acc * d],
|
||||
fill: [key * d, acc * d, d, d],
|
||||
@ -35,18 +93,18 @@ function getDrawingValues(state, acc, cell) {
|
||||
);
|
||||
}
|
||||
|
||||
function initialState1D(width, initFn, args) {
|
||||
function initialState1d(width, initFn, args) {
|
||||
return [...Array(width)].map(
|
||||
() => initFn(...args).toString(),
|
||||
).join('');
|
||||
}
|
||||
|
||||
function initialState2D(width, height, initFn, args) {
|
||||
function initialState2d(width, height, initFn, args) {
|
||||
return [...Array(height)].map(
|
||||
() => [...Array(width)].map(() => initFn(...args)),
|
||||
);
|
||||
}
|
||||
|
||||
export {
|
||||
evolve, getDrawingValues, initialState1D, initialState2D,
|
||||
getDrawingValues, initialState1d, initialState2d, evolve, evolve2d, conwayRules, createBoard,
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user