explorata/src/js/automata.js

111 lines
2.9 KiB
JavaScript
Raw Normal View History

2022-01-01 23:11:38 +01:00
// 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];
2022-01-01 14:07:34 +01:00
}
2022-01-01 23:11:38 +01:00
// 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);
2022-01-01 14:07:34 +01:00
}
function conwayRules(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
if (cell === 0 && neighbors === 3) return 1;
// the cell remains the same if none apply
return cell;
}
2022-01-01 23:11:38 +01:00
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);
}));
}
2022-01-01 14:07:34 +01:00
function getDrawingValues(state, acc, cell) {
const d = cell.dimension;
return Object.keys(state).map(
(key) => {
2022-01-01 23:11:38 +01:00
const fillStyle = (() => {
if (state[key] === '1') return cell.liveColor;
return cell.deadColor;
})();
2022-01-01 14:07:34 +01:00
return {
move: [key * d, acc * d],
fill: [key * d, acc * d, d, d],
fillStyle,
};
},
);
}
2022-01-01 23:11:38 +01:00
function initialState1d(width, initFn, args) {
2022-01-01 14:07:34 +01:00
return [...Array(width)].map(
() => initFn(...args).toString(),
).join('');
}
2022-01-01 23:11:38 +01:00
function initialState2d(width, height, initFn, args) {
2022-01-01 14:07:34 +01:00
return [...Array(height)].map(
() => [...Array(width)].map(() => initFn(...args)),
);
}
export {
2022-01-01 23:11:38 +01:00
getDrawingValues, initialState1d, initialState2d, evolve, evolve2d, conwayRules, createBoard,
2022-01-01 14:07:34 +01:00
};