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 38a68d9
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
node_modules
frontend/node_modules
.env
.vscode
6 changes: 6 additions & 0 deletions backend/uno-game-engine/deck.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,5 +70,11 @@ function makeCard(type, color, value) {
*/
function shuffle(deck) {
//todo: Implement a generic shuffling algorithm

// 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]];
}
[deck[0], deck[1]] = [deck[1], deck[0]];
}

0 comments on commit 38a68d9

Please sign in to comment.