@gamepark/rules-api
Version:
API to implement the rules of a board game
67 lines • 2.55 kB
JavaScript
;
var __extends = (this && this.__extends) || (function () {
var extendStatics = function (d, b) {
extendStatics = Object.setPrototypeOf ||
({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; };
return extendStatics(d, b);
};
return function (d, b) {
if (typeof b !== "function" && b !== null)
throw new TypeError("Class extends value " + String(b) + " is not a constructor or null");
extendStatics(d, b);
function __() { this.constructor = d; }
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
};
})();
Object.defineProperty(exports, "__esModule", { value: true });
exports.RandomBot = exports.Bot = void 0;
/**
* A Bot is a player controlled by a machine for a game.
* Used to replace players that quit a game, or for opponents in tutorials.
* Could be used to create AI players.
*/
var Bot = /** @class */ (function () {
function Bot(player) {
this.player = player;
}
return Bot;
}());
exports.Bot = Bot;
/**
* A random bot picks a random move to play amongst all the legal moves.
* Most board games can list efficiently all the legal moves all the time, which allows to have random bot to replace any missing opponent.
*/
var RandomBot = /** @class */ (function (_super) {
__extends(RandomBot, _super);
function RandomBot(Rules, player) {
var _this = _super.call(this, player) || this;
_this.Rules = Rules;
return _this;
}
RandomBot.prototype.run = function (game) {
var moves = this.getLegalMoves(game);
switch (moves.length) {
case 0:
console.warn('Random bot requires at least one legal move from getLegalMoves to run');
return [];
case 1:
return moves;
default:
return [moves[Math.floor(Math.random() * moves.length)]];
}
};
/**
* The legal moves for this player at this game state.
* Can be overridden if you need to further restrict the moves you want the bot to play.
*
* @param game The game state
* @protected
*/
RandomBot.prototype.getLegalMoves = function (game) {
return new this.Rules(game).getLegalMoves(this.player);
};
return RandomBot;
}(Bot));
exports.RandomBot = RandomBot;
//# sourceMappingURL=Bot.js.map