From 38a68d93f3c9dadfd6909e5d6d9fdb09e8582313 Mon Sep 17 00:00:00 2001 From: Divyansh Seth Date: Tue, 28 May 2024 16:29:32 +0530 Subject: [PATCH] used fisher-yates algo to shuffle deck array --- .gitignore | 1 + backend/uno-game-engine/deck.js | 6 ++++++ 2 files changed, 7 insertions(+) diff --git a/.gitignore b/.gitignore index 8e27764..9a30af8 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ node_modules frontend/node_modules .env +.vscode \ No newline at end of file diff --git a/backend/uno-game-engine/deck.js b/backend/uno-game-engine/deck.js index e094a7d..923e5bb 100644 --- a/backend/uno-game-engine/deck.js +++ b/backend/uno-game-engine/deck.js @@ -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]]; } \ No newline at end of file