Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

All-in command implementation #33

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion src/player-interaction.js
Original file line number Diff line number Diff line change
Expand Up @@ -124,20 +124,24 @@ class PlayerInteraction {
// Returns an array of strings
static getAvailableActions(player, previousActions) {
let actions = _.values(previousActions);
let betActions = _.filter(actions, a => a.name === 'bet' || a.name === 'raise');
let betActions = _.filter(actions, a => a.name === 'bet'
|| a.name === 'raise' || a.name === 'all-in');
let hasBet = betActions.length > 0;

let availableActions = [];

if (player.hasOption) {
availableActions.push('check');
availableActions.push('raise');
availableActions.push('all-in');
} else if (hasBet) {
availableActions.push('call');
availableActions.push('raise');
availableActions.push('all-in');
} else {
availableActions.push('check');
availableActions.push('bet');
availableActions.push('all-in');
}

// Prevent players from raising when they don't have enough chips.
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should also remove the all-in action when players only have enough chips to call, just like we do with raise (it could be made more explicit with "Call (All-in)."

Expand Down Expand Up @@ -195,6 +199,10 @@ class PlayerInteraction {
name = 'raise';
amount = input[1] ? parseInt(input[1]) : NaN;
break;
case 'a':
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

'a' is a little too easy to type by accident. Since this is a nuclear option, let's only look for the strings "all in," "all-in," and possibly "allin."

case 'all-in':
name = 'all-in';
break;
default:
return null;
}
Expand Down
14 changes: 10 additions & 4 deletions src/pot-manager.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,10 @@ class PotManager {
let index = this.pots.indexOf(this.currentPot);
this.pots.splice(index, 1);
}
this.currentPot = {
participants: participants,
amount: amount

this.currentPot = {
participants: participants,
amount: amount
};

this.pots.push(this.currentPot);
Expand Down Expand Up @@ -87,6 +87,12 @@ class PotManager {
action.amount = this.currentBet;
this.updateChipsAndPot(player, action);
break;
case 'all-in':
// All-in does not specify an amount, but wagers all of the player's chips.
let previousWager = player.lastAction ? player.lastAction.amount : 0;
action.amount = player.chips + previousWager;
this.currentBet = this.updateChipsAndPot(player, action);
break;
case 'bet':
case 'raise':
this.correctInvalidBets(player, action);
Expand Down
5 changes: 3 additions & 2 deletions src/texas-holdem.js
Original file line number Diff line number Diff line change
Expand Up @@ -288,6 +288,7 @@ class TexasHoldem {
break;
case 'bet':
case 'raise':
case 'all-in':
this.onPlayerBet(player, roundEnded);
break;
}
Expand Down Expand Up @@ -378,8 +379,8 @@ class TexasHoldem {
if (player.chips === 0) {
player.isAllIn = true;
}
let playersWhoCanCall = _.filter(this.players,

let playersWhoCanCall = _.filter(this.players,
p => p.isInHand && !p.isBettor && p.chips > 0);
if (playersWhoCanCall.length === 0) {
let result = { isHandComplete: false };
Expand Down