Skip to content

Commit

Permalink
deck: Implement unique identification of cards by assigning an id to …
Browse files Browse the repository at this point in the history
…each card

Created an id as card-type-color-value
To uniquely identify the same type color value card, I have also created a sameCardCount array that will to uniquely identify these card
Finnaly created a unique id card-type-color-value-sameCardCount[id]
Changed id typed to String in types.d.ts
  • Loading branch information
sethdivyansh committed May 30, 2024
1 parent 1a69f77 commit 91d4931
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 2 deletions.
9 changes: 8 additions & 1 deletion backend/uno-game-engine/deck.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ const values = [
];
const specialCards = ['wild', 'draw4'];
const deck = [];
const sameCardCount = []; // to keep track of same cards in assigning unique id to each card

/**
* In a standard UNO deck, there are 108 cards. Here's the breakdown:
Expand Down Expand Up @@ -57,7 +58,13 @@ export default function getShuffledCardDeck(): Array<UNOCard> {
*/
function makeCard(type: CardType, color: CardColor, value: CardValue): UNOCard {
//todo: Implement unique identification of cards by assigning an id to each card
return { type, color, value, id: undefined };
let id = `card-${type}-${color}-${value}`;

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

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

/**
Expand Down
2 changes: 1 addition & 1 deletion backend/uno-game-engine/types.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,5 @@ type UNOCard = {
type: CardType;
color: CardColor;
value: CardValue;
id: undefined;
id: String;
};

0 comments on commit 91d4931

Please sign in to comment.