tarot/cardpile.js

40 lines
635 B
JavaScript
Raw Normal View History

2020-11-25 04:23:06 +00:00
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;
}
2020-11-25 06:50:24 +00:00
random() {
2020-11-25 04:23:06 +00:00
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;