Skip to content

Commit

Permalink
deck: Implemented Fisher-Yates shuffling algorithm to shuffle the deck
Browse files Browse the repository at this point in the history
Fixes: #2
  • Loading branch information
sethdivyansh committed May 31, 2024
1 parent 61e81e7 commit 2e3e179
Showing 1 changed file with 10 additions and 3 deletions.
13 changes: 10 additions & 3 deletions backend/uno-game-engine/deck.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,11 @@ export function getShuffledCardDeck(): Array<UNOCard> {
* 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 };
}
Expand All @@ -65,6 +69,9 @@ export function makeCard(type: CardType, color: CardColor, value: CardValue): UN
* Time complexity: O(n)
*/
export function shuffle(deck: Array<any>) {

Check failure on line 71 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]];
// 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]];
}
}

0 comments on commit 2e3e179

Please sign in to comment.