Skip to content

Commit

Permalink
engine: Implement Card Allotment Logic
Browse files Browse the repository at this point in the history
This adds a check to ensure there are enough cards in the deck to allot to all players.
And then distributes the cards to all players from the shuffled deck.

Fixes #3

Signed-off-by: Sagnik Mandal <[email protected]>
  • Loading branch information
criticic committed May 30, 2024
1 parent df8d243 commit b9a92cf
Showing 1 changed file with 8 additions and 4 deletions.
12 changes: 8 additions & 4 deletions backend/uno-game-engine/engine.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,14 @@ export class GameEngine {
this.status = 'READY';
}
allotCards() {
//todo: Implement the card allotment logic: Remove `NUM_CARDS_PER_PLAYER` cards from the deck
// and add them to the player's hand - Do this for each player
// example:
this.players[0].cards = this.cardDeck.splice(0, NUM_CARDS_PER_PLAYER);
if (this.cardDeck.length < this.players.length * NUM_CARDS_PER_PLAYER) {
throw new Error('Not enough cards to distribute');
}

this.players = this.players.map(player => {
player.cards = this.cardDeck.splice(0, NUM_CARDS_PER_PLAYER);
return player;
});
}
addPlayer(player) {
this.players.push(player);
Expand Down

0 comments on commit b9a92cf

Please sign in to comment.