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.
4. Added test,in deck.test.ts, to check the correct functioning of getShuffledCardDeck()
Fixes: #1
  • Loading branch information
sethdivyansh committed Jun 3, 2024
1 parent 57e2de8 commit e5cfedf
Show file tree
Hide file tree
Showing 3 changed files with 89 additions and 17 deletions.
8 changes: 4 additions & 4 deletions backend/src/types.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@

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

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

type SpecialCardNames = 'skip' | 'reverse' | 'draw2' | 'draw4' | 'colchange';
type CardNumbers = '0' | '1' | '2' | '3' | '4' | '5' | '6' | '7' | '8' | '9';
type SpecialCardName = 'skip' | 'reverse' | 'draw2' | 'draw4' | 'colchange';
type CardNumber = '0' | '1' | '2' | '3' | '4' | '5' | '6' | '7' | '8' | '9';

type CardValue = SpecialCardNames | CardNumbers;
type CardValue = SpecialCardName | CardNumber;

type UNOCard = {
type: CardType;
Expand Down
60 changes: 59 additions & 1 deletion backend/tests/deck.test.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,68 @@
import { shuffle, makeCard } from '../uno-game-engine/deck';
import {
shuffle,
makeCard,
getShuffledCardDeck,
} from '../uno-game-engine/deck';

describe('getShuffledCardDeck', () => {
test('should return an array of 108 cards', () => {
const deck = getShuffledCardDeck();
expect(deck.length).toBe(108);
{
// Check the number of cards of each type
const numberCards = deck.filter((card) => card.type === 'number');
expect(numberCards.length).toBe(76);

const specialCards = deck.filter((card) => card.type === 'special');
expect(specialCards.length).toBe(24);

const wildCards = deck.filter((card) => card.type === 'wild');
expect(wildCards.length).toBe(8);
}
{
// Check the number of cards of red color
const redCards = deck.filter((card) => card.color === 'red');
expect(redCards.length).toBe(25); // 25 * 4 = 100 color cards
}
{
// Check the number of cards of 0 and 1 values
const zeroValueCards = deck.filter((card) => card.value === '0');
expect(zeroValueCards.length).toBe(4);

const oneValueCards = deck.filter((card) => card.value === '1');
expect(oneValueCards.length).toBe(8);
}
{
// Check the number of cards of special type
const skipCards = deck.filter((card) => card.value === 'skip');
expect(skipCards.length).toBe(8);

const draw2Cards = deck.filter((card) => card.value === 'draw2');
expect(draw2Cards.length).toBe(8);

const reverseCards = deck.filter(
(card) => card.value === 'reverse'
);
expect(reverseCards.length).toBe(8);
}
{
// Check the number of cards of wild type
const wildCards = deck.filter((card) => card.value === 'colchange');
expect(wildCards.length).toBe(4);

const draw4Cards = deck.filter((card) => card.value === 'draw4');
expect(draw4Cards.length).toBe(4);
}
});
});

describe('testing deck.ts', () => {
test('makeCard', () => {
const card = makeCard('number', 'blue', '3');
expect(card.color).toBe('blue');
});
});

describe('shuffle function', () => {
test('should change order of elements', () => {
// Create a mock deck
Expand Down
38 changes: 26 additions & 12 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 colors: Array<CardColor> = ['red', 'yellow', 'green', 'blue', 'wild'];
const numValues: Array<CardNumber> = [
'0',
'1',
'2',
Expand All @@ -10,12 +10,9 @@ const values = [
'7',
'8',
'9',
'skip',
'reverse',
'draw2',
];
const specialCards = ['wild', 'draw4'];
const deck = [];
const specialValues: Array<SpecialCardName> = ['skip', 'reverse', 'draw2'];
const wildCardValues: Array<SpecialCardName> = ['colchange', 'draw4'];
const sameCardCount = []; // to keep track of same cards in assigning unique id to each card

/**
Expand All @@ -42,11 +39,28 @@ 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) => {
if (color === 'wild') {
wildCardValues.forEach((value) => {
for (let i = 0; i < 4; i++) {
deck.push(makeCard('wild', color, value));
}
});
} else {
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));
});
}
});

shuffle(deck);
return deck;
Expand Down

0 comments on commit e5cfedf

Please sign in to comment.