explorata/src/modules/automata.js

153 lines
3.9 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-12-01 20:32:19 +01:00
// function createBoard(state, rules, height) {
// function createBoardAcc(s, h, acc) {
// if (h === 0) return acc;
// const newState = evolve1d(s, rules);
// const newAcc = acc.concat([s]);
// return createBoardAcc(newState, h - 1, newAcc);
// }
// return createBoardAcc(state, height, []);
// }
// performance "choke point" in full imperative
2022-01-01 23:11:38 +01:00
function createBoard(state, rules, height) {
2022-12-01 20:32:19 +01:00
var board = [];
let prevState = [];
for (let i = 0; i < height; i++) {
let nextState = [];
if (i == 0) {
nextState = evolve1d(state, rules);
} else {
nextState = evolve1d(prevState, rules);
}
board = board.concat([nextState]);
prevState = nextState;
2022-01-01 23:11:38 +01:00
}
2022-12-01 20:32:19 +01:00
return board;
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),
2022-01-01 23:11:38 +01:00
];
}
// 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-11 17:21:41 +01:00
// Rules : Moore neighborhood
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 23:11:38 +01:00
}
2022-01-01 14:07:34 +01:00
function getDrawingValues(state, acc, cell) {
const d = cell.dimension;
return Object.keys(state).map((key) => {
const fillStyle = (() => {
if (state[key] === "1") return cell.liveColor;
return cell.deadColor;
})();
2022-01-01 23:11:38 +01:00
return {
move: [key * d, acc * d],
fill: [key * d, acc * d, d, d],
fillStyle,
};
});
2022-01-01 14:07:34 +01:00
}
2022-01-11 13:16:42 +01:00
// Populates the first state with a single living cell in the center
function create1dStateOneCell(width) {
return [...Array(width)].map((cell, index) => {
if (index === width / 2 || index === width + 1 / 2) return 1;
return 0;
});
2022-01-11 13:16:42 +01:00
}
// Populates the first state of a 1D CA with cells returned
// by initFn
2022-01-11 13:16:42 +01:00
function create1dState(width, initFn, args) {
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-11 13:16:42 +01:00
function create2dState(width, height, initFn, args) {
return [...Array(height)].map(() =>
[...Array(width)].map(() => initFn(...args))
2022-01-01 14:07:34 +01:00
);
}
export {
getDrawingValues,
create1dState,
create2dState,
createBoard,
create1dStateOneCell,
conwayRules,
evolve1d,
evolve2d,
2022-01-01 14:07:34 +01:00
};