From 283f2380f235e45aa54776c4028f0de8cce185cd Mon Sep 17 00:00:00 2001 From: nicholas Date: Wed, 3 Jul 2019 22:32:41 -0400 Subject: [PATCH] Reset and Fisher-Yates shuffle at the end of the deck. --- src/draw.js | 22 +++++++++++++++------- 1 file changed, 15 insertions(+), 7 deletions(-) diff --git a/src/draw.js b/src/draw.js index 0b67173..0684eae 100644 --- a/src/draw.js +++ b/src/draw.js @@ -7,16 +7,19 @@ class Strategies { } reset() { - this.cards = strategies; + this.cards = [...strategies]; } shuffle() { const { cards } = this; - let f = cards.length, y; + const randomCard = top => Math.floor(Math.random() * top); + const shuffling = randomCard(cards.length); - while (f) { - y = Math.floor(Math.random() * f--); - [cards[f], cards[y]] = [cards[y], cards[f]]; + for (let index = cards.length - 1; index > -1; index--) { + const toSwap = randomCard(index); + [cards[index], cards[toSwap]] = [cards[toSwap], cards[index]]; + + if (index === shuffling) console.log(`shuffling the deck`); } return this; @@ -24,8 +27,13 @@ class Strategies { draw() { const drawn = this.cards.pop(); - if (this.cards.length === 0) this.reset(); - return drawn.toLowerCase(); + + if (this.cards.length === 0) { + this.reset(); + this.shuffle(); + } + + return (drawn || 'hello').toLowerCase(); } }