From 5e990df0d66eef515ccf7b9937e0dfd0618378bc Mon Sep 17 00:00:00 2001 From: vilmibm Date: Tue, 24 Nov 2020 20:23:06 -0800 Subject: [PATCH] move card pile to own file --- cardpile.js | 39 +++++++++++++++++++++++++++++++++++++++ tarot.js | 40 +--------------------------------------- 2 files changed, 40 insertions(+), 39 deletions(-) create mode 100644 cardpile.js diff --git a/cardpile.js b/cardpile.js new file mode 100644 index 0000000..c7b72f3 --- /dev/null +++ b/cardpile.js @@ -0,0 +1,39 @@ +class CardPile { + constructor() { + this.cards = new Set(); + } + + add(cardName) { + this.cards.add(cardName); + } + + has(cardName) { + return this.cards.has(cardName); + } + + get size() { + return this.cards.size; + } + + drawRandom() { + const size = this.cards.size + if (size === 0) { + return null; + } + const index = Math.floor(Math.random() * Math.floor(size)); + let i = 0; + let cardName = ""; + for (let card of this.cards) { + if (i === index) { + cardName = card; + break + } + i++; + } + this.cards.delete(cardName); + + return cardName; + } +} + +export default CardPile; diff --git a/tarot.js b/tarot.js index e23b1b6..8fcf943 100644 --- a/tarot.js +++ b/tarot.js @@ -1,6 +1,5 @@ import cardDB from './cards.js'; - -// TODO reset state button +import CardPile from './cardpile.js'; class Tarot { constructor() { @@ -130,42 +129,5 @@ class Renderer { } } -class CardPile { - constructor() { - this.cards = new Set(); - } - - add(cardName) { - this.cards.add(cardName); - } - - has(cardName) { - return this.cards.has(cardName); - } - - get size() { - return this.cards.size; - } - - drawRandom() { - const size = this.cards.size - if (size === 0) { - return null; - } - const index = Math.floor(Math.random() * Math.floor(size)); - let i = 0; - let cardName = ""; - for (let card of this.cards) { - if (i === index) { - cardName = card; - break - } - i++; - } - this.cards.delete(cardName); - - return cardName; - } -} export default Tarot;