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 28, 2024
1 parent 40be68f commit 73a5f1c
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 2 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
node_modules
frontend/node_modules
.env
.env
7 changes: 6 additions & 1 deletion backend/uno-game-engine/deck.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,5 +70,10 @@ function makeCard(type, color, value) {
*/
function shuffle(deck) {
//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 73a5f1c

Please sign in to comment.