Skip to content

Commit

Permalink
backend:deck.ts
Browse files Browse the repository at this point in the history
This commit involves one preperatory commit : adding wild color to CardColors

Added deck generation function to initialize the deck according to given constraints

The function generates cards and push them the deck array generating cards of each color and also wild cards

Also have checked the deck composition with jest tests

Fixes :#1
  • Loading branch information
Abhishek committed Jun 2, 2024
2 parents b0ca51d + a4574ab commit 1154a98
Showing 1 changed file with 13 additions and 0 deletions.
13 changes: 13 additions & 0 deletions backend/uno-game-engine/deck.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,13 @@ export function makeCard(
if (!sameCardCount[id]) sameCardCount[id] = 0;
sameCardCount[id]++; // increment the count of same cards to assign unique id

const uid = `${id}-${sameCardCount[id]}`;
return { type, color, value, id: uid };
const id = `card-${type}-${color}-${value}`;

if (!sameCardCount[id]) sameCardCount[id] = 0;
sameCardCount[id]++; // increment the count of same cards to assign unique id

const uid = `${id}-${sameCardCount[id]}`;
return { type, color, value, id: uid };
}
Expand All @@ -98,4 +105,10 @@ export function shuffle(deck: Array<UNOCard>) {
const j = Math.floor(Math.random() * i);
[deck[i], deck[j]] = [deck[j], deck[i]];
}
export function shuffle(deck: Array<UNOCard>) {
// Fisher-Yates shuffle algorithm to shuffle card deck
for (let i = deck.length - 1; i > 0; i--) {
const j = Math.floor(Math.random() * i);
[deck[i], deck[j]] = [deck[j], deck[i]];
}
}

0 comments on commit 1154a98

Please sign in to comment.