@gamepark/rules-api
Version:
API to implement the rules of a board game
25 lines • 1.21 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.applyAutomaticMoves = void 0;
var loops_util_1 = require("./loops.util");
var random_util_1 = require("./random.util");
function applyAutomaticMoves(rules, moves, preprocessMove) {
if (moves === void 0) { moves = []; }
(0, loops_util_1.loopWithFuse)(function () {
var _a;
if (moves.length === 0 && rules.getAutomaticMoves) {
moves.push.apply(moves, rules.getAutomaticMoves());
}
var move = moves.shift();
if (!move)
return false;
var randomizedMove = (0, random_util_1.hasRandomMove)(rules) ? rules.randomize(move) : move;
if (preprocessMove)
preprocessMove(randomizedMove);
var consequences = (_a = rules.play(JSON.parse(JSON.stringify(randomizedMove)))) !== null && _a !== void 0 ? _a : [];
moves.unshift.apply(moves, consequences);
return true;
}, { errorFn: function () { return new Error("Infinite loop detected while applying move consequences: ".concat(JSON.stringify(moves), ")")); } });
}
exports.applyAutomaticMoves = applyAutomaticMoves;
//# sourceMappingURL=automatic-moves.util.js.map