From 91d493118ac6708afa7ae007ce67f4a7a2248783 Mon Sep 17 00:00:00 2001 From: Divyansh Seth Date: Tue, 28 May 2024 17:11:16 +0530 Subject: [PATCH] deck: Implement unique identification of cards by assigning an id to 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 --- backend/uno-game-engine/deck.ts | 9 ++++++++- backend/uno-game-engine/types.d.ts | 2 +- 2 files changed, 9 insertions(+), 2 deletions(-) diff --git a/backend/uno-game-engine/deck.ts b/backend/uno-game-engine/deck.ts index 6d10732..f3b0e60 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: @@ -57,7 +58,13 @@ export default function getShuffledCardDeck(): Array { */ 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 }; } /** diff --git a/backend/uno-game-engine/types.d.ts b/backend/uno-game-engine/types.d.ts index 9d18a0d..990a94f 100644 --- a/backend/uno-game-engine/types.d.ts +++ b/backend/uno-game-engine/types.d.ts @@ -20,5 +20,5 @@ type UNOCard = { type: CardType; color: CardColor; value: CardValue; - id: undefined; + id: String; };