@idealic/poker-engine
Version:
Professional poker game engine and hand evaluator with built-in iterator utilities
51 lines • 1.83 kB
JavaScript
import { Game } from './';
import { getPlayerIndex } from '../utils/position';
import { cloneState } from '../utils/state';
import { deal, showCards } from './dealer';
import { createTable, isShowdown } from './table';
/**
* Orchestrates a poker game by managing dealer actions and player responses.
* Returns a new game state with all actions applied.
*/
export async function croupier(hand, players, game = createTable(hand)) {
// Initialize table state
const actions = [...hand.actions];
// Keep running until showdown
while (true) {
// Get the current player index
const currentPlayerIndex = getPlayerIndex(game);
// If it's dealer's turn (-1), handle dealer actions
if (currentPlayerIndex === -1) {
const action = deal(game);
if (action) {
game = Game.applyAction(game, action);
actions.push(action);
}
else {
// End of the game
break;
}
// If we're in showdown after dealer actions and no more cards to show, end hand
if (isShowdown(game) && !showCards(game)) {
break;
}
// Continue to next iteration
continue;
}
// Handle player action
const currentPlayer = game.players[currentPlayerIndex];
// Get player function
const player = players[currentPlayerIndex];
if (!player) {
throw new Error(`No player function found for position ${currentPlayerIndex}`);
}
const action = await player(cloneState(game), currentPlayer);
game = Game.applyAction(game, action);
actions.push(action);
}
return {
...hand,
actions,
};
}
//# sourceMappingURL=croupier.js.map