Skip to content

Commit

Permalink
deck: Assigned unique id to each card.
Browse files Browse the repository at this point in the history
- 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 shivansh-bhatnagar18#4
  • Loading branch information
sethdivyansh authored and kuv2707 committed Jun 1, 2024
1 parent 44927c8 commit f05595c
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 3 deletions.
2 changes: 1 addition & 1 deletion backend/src/types.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ type UNOCard = {
type: CardType;
color: CardColor;
value: CardValue;
id: undefined;
id: string;
};

type Player = {
Expand Down
10 changes: 8 additions & 2 deletions 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'];

Check failure on line 17 in backend/uno-game-engine/deck.ts

View workflow job for this annotation

GitHub Actions / eslint-backend

'specialCards' is assigned a value but never used
const deck = [];

Check failure on line 18 in backend/uno-game-engine/deck.ts

View workflow job for this annotation

GitHub Actions / eslint-backend

'deck' is assigned a value but never used
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 @@ -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 };
}

/**
Expand Down

0 comments on commit f05595c

Please sign in to comment.