Skip to content

Commit

Permalink
fix: handle card redistribution when no cards are leftover
Browse files Browse the repository at this point in the history
  • Loading branch information
RevolutionTech committed Oct 17, 2021
1 parent 3e7c540 commit d58f572
Showing 1 changed file with 4 additions and 1 deletion.
5 changes: 4 additions & 1 deletion src/game/deck.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@ export const shuffleCards = (ctx: Ctx, deck: Letter[]) =>
export const dealCards = (deck: Letter[], numPlayers: number) => {
const extraCardsLeftover = deck.length % numPlayers;
const maxNumCardsPerPlayer = Math.ceil(deck.length / numPlayers);
const minNumCardsPerPlayer = extraCardsLeftover
? maxNumCardsPerPlayer - 1
: maxNumCardsPerPlayer;
const deckMoreCardsPerPlayer = _.slice(
deck,
0,
Expand All @@ -32,6 +35,6 @@ export const dealCards = (deck: Letter[], numPlayers: number) => {
// TODO: Shuffle chunks so that later players do not consistently get fewer cards
return [
..._.chunk(deckMoreCardsPerPlayer, maxNumCardsPerPlayer),
..._.chunk(deckFewerCardsPerPlayer, maxNumCardsPerPlayer - 1),
..._.chunk(deckFewerCardsPerPlayer, minNumCardsPerPlayer),
];
};

0 comments on commit d58f572

Please sign in to comment.