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 28, 2024
1 parent 40be68f commit 47daeac
Showing 1 changed file with 9 additions and 5 deletions.
14 changes: 9 additions & 5 deletions backend/uno-game-engine/engine.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,15 @@ 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);
allotCards() {
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 47daeac

Please sign in to comment.