Skip to content

Commit

Permalink
End the game when we have only one player left.
Browse files Browse the repository at this point in the history
  • Loading branch information
CharlieHess committed Aug 2, 2015
1 parent b8205c9 commit a069a9d
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 6 deletions.
28 changes: 22 additions & 6 deletions src/texas-holdem.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ class TexasHoldem {
this.deck = new Deck();
this.smallBlind = 1;
this.bigBlind = this.smallBlind * 2;
this.quitGame = new rx.Subject();
this.gameEnded = new rx.Subject();

// Cache the direct message channels for each player as we'll be using
// them often, and fetching them takes linear time per number of users.
Expand All @@ -48,6 +48,7 @@ class TexasHoldem {
//
// Returns a {Disposable} that will end this game early
start(dealerButton=null, timeBetweenHands=5000) {
this.isRunning = true;
this.dealerButton = dealerButton === null ?
Math.floor(Math.random() * this.players.length) :
dealerButton;
Expand All @@ -56,16 +57,19 @@ class TexasHoldem {
.flatMap(() => this.playHand()
.flatMap(() => rx.Observable.timer(timeBetweenHands, this.scheduler)))
.repeat()
.takeUntil(this.quitGame)
.takeUntil(this.gameEnded)
.subscribe();
}

// Public: Ends the current game immediately and disposes all resources
// associated with the game.
// Public: Ends the current game immediately.
//
// Returns nothing
quit() {
this.quitGame.onNext();
quit(winner) {
if (winner) {
this.channel.send(`Congratulations ${winner.name}, you've won!`);
}
this.gameEnded.onNext(winner);
this.isRunning = false;
}

// Public: Get all players still in the current hand.
Expand Down Expand Up @@ -463,6 +467,18 @@ class TexasHoldem {

handEnded.onNext(true);
handEnded.onCompleted();
this.checkForGameWinner();
}

// Private: If there is only one player with chips left, we've got a winner.
//
// Returns nothing
checkForGameWinner() {
let playersWithChips = _.filter(this.players, p => p.chips > 0);
if (playersWithChips.length === 1) {
let winner = playersWithChips[0];
this.quit(winner);
}
}

// Private: Deals hole cards to each player in the game. To communicate this
Expand Down
18 changes: 18 additions & 0 deletions tests/texas-holdem-spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,24 @@ describe('TexasHoldem', function() {
game.tableFormatter = "\n";
});

it('should end the game when all players have been eliminated', function() {
game.start(0);
scheduler.advanceBy(5000);

messages.onNext({user: 4, text: "Raise 200"});
scheduler.advanceBy(5000);
messages.onNext({user: 5, text: "Call"});
scheduler.advanceBy(5000);
messages.onNext({user: 1, text: "Call"});
scheduler.advanceBy(5000);
messages.onNext({user: 2, text: "Call"});
scheduler.advanceBy(5000);
messages.onNext({user: 3, text: "Call"});
scheduler.advanceBy(5000);

assert(!game.isRunning);
});

it('should handle default bets and raises', function() {
game.start(0);
scheduler.advanceBy(5000);
Expand Down

0 comments on commit a069a9d

Please sign in to comment.