From 98d809cab73e2b35683392e90dcc5d213e7bdbd5 Mon Sep 17 00:00:00 2001 From: Divyansh Seth Date: Tue, 28 May 2024 16:29:32 +0530 Subject: [PATCH 1/2] deck: used fisher-yates algorithm to shuffle deck array --- backend/uno-game-engine/deck.ts | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/backend/uno-game-engine/deck.ts b/backend/uno-game-engine/deck.ts index 9e113f5..068916b 100644 --- a/backend/uno-game-engine/deck.ts +++ b/backend/uno-game-engine/deck.ts @@ -55,7 +55,11 @@ export function getShuffledCardDeck(): Array { * Helper function to make a card object. * @returns {UNOCard} The composed UNO card. */ -export function makeCard(type: CardType, color: CardColor, value: CardValue): UNOCard { +export function makeCard( + type: CardType, + color: CardColor, + value: CardValue +): UNOCard { //todo: Implement unique identification of cards by assigning an id to each card return { type, color, value, id: undefined }; } @@ -64,7 +68,10 @@ export function makeCard(type: CardType, color: CardColor, value: CardValue): UN * 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) { - //todo: Implement a generic shuffling algorithm - [deck[0], deck[1]] = [deck[1], deck[0]]; +function shuffle(deck: Array) { + // Fisher-Yates shuffle algorithm to shuffle card deck + for (let i = deck.length - 1; i > 0; i--) { + const j = Math.floor(Math.random() * i); + [deck[i], deck[j]] = [deck[j], deck[i]]; + } } From 0f5db6e4a27c1d94f394d5ffc9e36fc4e13be2c1 Mon Sep 17 00:00:00 2001 From: Divyansh Seth Date: Fri, 31 May 2024 23:22:30 +0530 Subject: [PATCH 2/2] deck: completed function to create and shuffle a UNO card deck. - added black as a color in `CardColor` in `types.d.ts` - separated the values of special cards and wild cards into two arrays named specialValues and wildCardValues, respectively. - Iterates over each color in the colors array. For each color: * Iterates over each number value (0 to 9) in the numValues array and adds two cards of that color for each number, except for '0' where only one card is added. * Iterates over each special value in the specialValues array and adds two cards of that color for each special value. * If the color is 'black', iterates over each wild card value in the wildCardValue array and adds four cards of that color for each wild card value. --- backend/uno-game-engine/deck.ts | 36 +++++++++++++++++++++--------- backend/uno-game-engine/types.d.ts | 2 +- 2 files changed, 26 insertions(+), 12 deletions(-) diff --git a/backend/uno-game-engine/deck.ts b/backend/uno-game-engine/deck.ts index 068916b..0567ba5 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 numValues: Array = [ '0', '1', '2', @@ -10,12 +10,10 @@ const values = [ '7', '8', '9', - 'skip', - 'reverse', - 'draw2', ]; -const specialCards = ['wild', 'draw4']; -const deck = []; +const specialValues: Array = ['skip', 'reverse', 'draw2']; +const wildCardValue: Array = ['colchange', 'draw4']; +const deck: Array = []; /** * In a standard UNO deck, there are 108 cards. Here's the breakdown: @@ -41,11 +39,27 @@ const deck = []; @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) => { + 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; diff --git a/backend/uno-game-engine/types.d.ts b/backend/uno-game-engine/types.d.ts index 7c281f2..30fc8c0 100644 --- a/backend/uno-game-engine/types.d.ts +++ b/backend/uno-game-engine/types.d.ts @@ -1,6 +1,6 @@ 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';