Skip to content

Commit

Permalink
used fisher-yates algo to shuffle deck array
Browse files Browse the repository at this point in the history
  • Loading branch information
sethdivyansh committed May 30, 2024
1 parent a73de43 commit 5845678
Showing 1 changed file with 5 additions and 1 deletion.
6 changes: 5 additions & 1 deletion backend/uno-game-engine/deck.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,5 +66,9 @@ function makeCard(type: CardType, color: CardColor, value: CardValue): UNOCard {
*/
function shuffle(deck: Array<any>) {
//todo: Implement a generic shuffling algorithm
[deck[0], deck[1]] = [deck[1], deck[0]];
// Fisher-Yates shuffle algorithm
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 5845678

Please sign in to comment.