Reset and Fisher-Yates shuffle at the end of the deck.

This commit is contained in:
nicholas 2019-07-03 22:32:41 -04:00
parent 4ef4194d3e
commit 283f2380f2

View File

@ -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();
}
}