explorata/src/modules/automata.js

123 lines
3.2 KiB
JavaScript
Raw Normal View History

// 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;
}
2022-01-01 23:11:38 +01:00
// get the next evolution of a 1D CA initial state
2022-01-02 16:45:03 +01:00
function evolve1d(state, rules) {
function getCell(index) {
const safeIndex = guard(index, state);
return state[safeIndex];
2022-01-01 23:11:38 +01:00
}
const newState = state.map((_, x) => {
const cells = [
getCell(x - 1),
getCell(x),
getCell(x + 1)];
return rules[cells.join('')];
});
return newState.map(Number);
2022-01-01 23:11:38 +01:00
}
// create a 2D board from a 1D CA initial state
2022-01-01 23:11:38 +01:00
function createBoard(state, rules, height) {
function createBoardAcc(s, h, acc) {
if (h === 0) return acc;
2022-01-02 16:45:03 +01:00
const newState = evolve1d(s, rules);
const newAcc = acc.concat([s]);
2022-01-01 23:11:38 +01:00
return createBoardAcc(newState, h - 1, newAcc);
}
return createBoardAcc(state, height, []);
2022-01-01 23:11:38 +01:00
}
// Find the neighbor of a given cell in a 2D CA board
2022-01-01 23:11:38 +01:00
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) {
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),
];
}
// Sums the value of a cell's neighbors
2022-01-01 23:11:38 +01:00
function getNeighborsSum(cells) {
return cells.reduce((cell, acc) => cell + acc, 0);
2022-01-01 14:07:34 +01:00
}
// Get the next evolution of a cell according to
// Conway's game of life rules
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;
}
// get the next evolution of a 2D CA initial state
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,
};
},
);
}
// Populates the first state of a 1D CA with cells returned
// by initFn
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),
);
2022-01-01 14:07:34 +01:00
}
// Populates the first state of a 2D CA with cells returned
// by initFn
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),
),
2022-01-01 14:07:34 +01:00
);
}
export {
2022-01-02 16:45:03 +01:00
getDrawingValues, initialState1d, initialState2d, evolve1d, evolve2d, conwayRules, createBoard,
2022-01-01 14:07:34 +01:00
};