UNPKG

@gamepark/rules-api

Version:

API to implement the rules of a board game

45 lines 1.4 kB
/** * 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. */ export class Bot { player; constructor(player) { this.player = player; } } /** * 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. */ export class RandomBot extends Bot { Rules; constructor(Rules, player) { super(player); this.Rules = Rules; } run(game) { const 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 */ getLegalMoves(game) { return new this.Rules(game).getLegalMoves(this.player); } } //# sourceMappingURL=Bot.js.map