From 5845678eb882b2663102d6d52d744b8cde35df7c 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 --- backend/uno-game-engine/deck.ts | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/backend/uno-game-engine/deck.ts b/backend/uno-game-engine/deck.ts index 6d10732..84ffd80 100644 --- a/backend/uno-game-engine/deck.ts +++ b/backend/uno-game-engine/deck.ts @@ -66,5 +66,9 @@ function makeCard(type: CardType, color: CardColor, value: CardValue): UNOCard { */ function shuffle(deck: Array) { //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]]; + } }