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;