From 2e865b9e737afecc4953a97f44423d41174da7ce Mon Sep 17 00:00:00 2001 From: Divyansh Seth Date: Fri, 31 May 2024 23:47:48 +0530 Subject: [PATCH] deck: completed function to create and shuffle a UNO card deck. 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 --- backend/src/types.d.ts | 8 ++--- backend/tests/deck.test.ts | 60 ++++++++++++++++++++++++++++++++- backend/uno-game-engine/deck.ts | 38 ++++++++++++++------- 3 files changed, 89 insertions(+), 17 deletions(-) diff --git a/backend/src/types.d.ts b/backend/src/types.d.ts index cae437c..a75b4e8 100644 --- a/backend/src/types.d.ts +++ b/backend/src/types.d.ts @@ -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; diff --git a/backend/tests/deck.test.ts b/backend/tests/deck.test.ts index d54e7b3..502a3f9 100644 --- a/backend/tests/deck.test.ts +++ b/backend/tests/deck.test.ts @@ -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 diff --git a/backend/uno-game-engine/deck.ts b/backend/uno-game-engine/deck.ts index 8a6c19e..5571f8a 100644 --- a/backend/uno-game-engine/deck.ts +++ b/backend/uno-game-engine/deck.ts @@ -1,5 +1,5 @@ -const colors: Array = ['red', 'yellow', 'green', 'blue']; -const values = [ +const colors: Array = ['red', 'yellow', 'green', 'blue', 'wild']; +const numValues: Array = [ '0', '1', '2', @@ -10,12 +10,9 @@ const values = [ '7', '8', '9', - 'skip', - 'reverse', - 'draw2', ]; -const specialCards = ['wild', 'draw4']; -const deck = []; +const specialValues: Array = ['skip', 'reverse', 'draw2']; +const wildCardValues: Array = ['colchange', 'draw4']; const sameCardCount = []; // to keep track of same cards in assigning unique id to each card /** @@ -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 { - 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 = []; + + 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;