@gamepark/rules-api
Version:
API to implement the rules of a board game
19 lines • 878 B
JavaScript
import { loopWithFuse } from './loops.util';
import { hasRandomMove } from './random.util';
export function applyAutomaticMoves(rules, moves = [], preprocessMove) {
loopWithFuse(() => {
if (moves.length === 0 && rules.getAutomaticMoves) {
moves.push(...rules.getAutomaticMoves());
}
const move = moves.shift();
if (!move)
return false;
const randomizedMove = hasRandomMove(rules) ? rules.randomize(move) : move;
if (preprocessMove)
preprocessMove(randomizedMove);
const consequences = rules.play(JSON.parse(JSON.stringify(randomizedMove))) ?? [];
moves.unshift(...consequences);
return true;
}, { errorFn: () => new Error(`Infinite loop detected while applying move consequences: ${JSON.stringify(moves)})`) });
}
//# sourceMappingURL=automatic-moves.util.js.map