From aad959273a925e9b05d73213f4bb73939fbe64e4 Mon Sep 17 00:00:00 2001 From: Divyansh Seth Date: Tue, 28 May 2024 17:11:16 +0530 Subject: [PATCH] deck: Assigned unique id to each card. - Created an ID in the format `card-type-color-value`. - Added a `sameCardCount` array to uniquely identify cards of the same type and color. - Generated a unique ID using the format `card-type-color-value-sameCardCount[id]`. Fixes #4 --- backend/uno-game-engine/deck.ts | 10 ++++++++-- backend/uno-game-engine/types.d.ts | 2 +- 2 files changed, 9 insertions(+), 3 deletions(-) diff --git a/backend/uno-game-engine/deck.ts b/backend/uno-game-engine/deck.ts index c25ebe0..a4aacce 100644 --- a/backend/uno-game-engine/deck.ts +++ b/backend/uno-game-engine/deck.ts @@ -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: @@ -60,8 +61,13 @@ export function makeCard( color: CardColor, value: CardValue ): UNOCard { - //todo: Implement unique identification of cards by assigning an id to each card - return { type, color, value, id: undefined }; + 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 }; } /** diff --git a/backend/uno-game-engine/types.d.ts b/backend/uno-game-engine/types.d.ts index 7c281f2..1b59f86 100644 --- a/backend/uno-game-engine/types.d.ts +++ b/backend/uno-game-engine/types.d.ts @@ -11,5 +11,5 @@ type UNOCard = { type: CardType; color: CardColor; value: CardValue; - id: undefined; + id: string; };