UNPKG

@gamepark/rules-api

Version:

API to implement the rules of a board game

25 lines 1.21 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.applyAutomaticMoves = void 0; var RandomMove_1 = require("./RandomMove"); var LoopWithFuse_1 = require("./LoopWithFuse"); function applyAutomaticMoves(rules, moves, preprocessMove) { if (moves === void 0) { moves = []; } (0, LoopWithFuse_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, RandomMove_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=ApplyAutomaticMoves.js.map