Skip to content

Commit

Permalink
deck: completed function to create and shuffle a UNO card deck.
Browse files Browse the repository at this point in the history
1. Added black to CardColor in types.d.ts.
2. Split special and wild cards into specialValues and wildCardValues arrays.
3. For each color in the colors array:
 - Add two cards for each number (0-9), except one card for '0'.
 - Add two cards for each special value.
 - For 'black', add four cards for each wild card value.

Fixes: #1
  • Loading branch information
sethdivyansh committed Jun 2, 2024
1 parent a4574ab commit 2e30543
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 12 deletions.
2 changes: 1 addition & 1 deletion backend/src/types.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

type CardType = 'number' | 'special' | 'wild';

type CardColor = 'red' | 'blue' | 'green' | 'yellow';
type CardColor = 'red' | 'blue' | 'green' | 'yellow' | 'black';

type SpecialCardNames = 'skip' | 'reverse' | 'draw2' | 'draw4' | 'colchange';
type CardNumbers = '0' | '1' | '2' | '3' | '4' | '5' | '6' | '7' | '8' | '9';
Expand Down
36 changes: 25 additions & 11 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'];
const values = [
const numValues: Array<CardNumbers> = [
'0',
'1',
'2',
Expand All @@ -10,12 +10,10 @@ const values = [
'7',
'8',
'9',
'skip',
'reverse',
'draw2',
];
const specialCards = ['wild', 'draw4'];
const deck = [];
const specialValues: Array<SpecialCardNames> = ['skip', 'reverse', 'draw2'];
const wildCardValue: Array<SpecialCardNames> = ['colchange', 'draw4'];
const deck: Array<UNOCard> = [];

Check failure on line 16 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

/**
Expand All @@ -42,11 +40,27 @@ const sameCardCount = []; // to keep track of same cards in assigning unique id
@returns {Array} deck - An array of 108 UNO cards.
*/
export function getShuffledCardDeck(): Array<UNOCard> {
const deck = [];
// todo: Implement the card generation logic
// dummy code:
// deck.push(makeCard('special', 'wild', 'wild'))
// deck.push(makeCard('number', 'red', '0'))
const deck: Array<UNOCard> = [];

colors.forEach((color) => {
numValues.forEach((value) => {
deck.push(makeCard('number', color, value));
if (value !== '0') {
deck.push(makeCard('number', color, value));
}
});
specialValues.forEach((value) => {
deck.push(makeCard('special', color, value));
deck.push(makeCard('special', color, value));
});
if (color === 'black') {
wildCardValue.forEach((value) => {
for (let i = 0; i < 4; i++) {
deck.push(makeCard('wild', color, value));
}
});
}
});

shuffle(deck);
return deck;
Expand Down

0 comments on commit 2e30543

Please sign in to comment.