Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

deck: Assigned unique id to each card. #14

Merged
merged 1 commit into from
Jun 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion backend/src/types.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,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
@@ -1,5 +1,5 @@
const colors: Array<CardColor> = ['red', 'yellow', 'green', 'blue'];

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

View workflow job for this annotation

GitHub Actions / eslint-backend

'colors' is assigned a value but never used
const values = [

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

View workflow job for this annotation

GitHub Actions / eslint-backend

'values' is assigned a value but never used
'0',
'1',
'2',
Expand All @@ -14,8 +14,9 @@
'reverse',
'draw2',
];
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,15 +61,20 @@
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 };
}

/**
* This function shuffles the elements of the given array *in place* . The function behaves in a type-agnostic way.
* Time complexity: O(n)
*/
export function shuffle(deck: Array<any>) {

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

View workflow job for this annotation

GitHub Actions / eslint-backend

Unexpected any. Specify a different type
//todo: Implement a generic shuffling algorithm
[deck[0], deck[1]] = [deck[1], deck[0]];
}
Loading