You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
43 lines
901 B
JavaScript
43 lines
901 B
JavaScript
3 years ago
|
function pickRandom(array) {
|
||
|
return array[Math.floor(Math.random() * array.length)];
|
||
|
}
|
||
|
|
||
|
const colors = [
|
||
|
{ color: "black", emoji: "♣" },
|
||
|
{ color: "black", emoji: "♠" },
|
||
|
{ color: "red", emoji: "♥" },
|
||
|
{ color: "red", emoji: "♦" },
|
||
|
];
|
||
|
const numbers = "23456789JDAK";
|
||
|
|
||
|
const used_cards = [];
|
||
|
|
||
|
function* getCardGen() {
|
||
|
while (true) {
|
||
|
const color = pickRandom(colors);
|
||
|
const number = pickRandom(numbers);
|
||
|
const card = { ...color, number };
|
||
|
if (used_cards.includes(getEmoji(card))) {
|
||
|
continue;
|
||
|
}
|
||
|
used_cards.push(getEmoji(card));
|
||
|
yield card;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
const cardGen = getCardGen();
|
||
|
|
||
|
function getCard() {
|
||
|
return cardGen.next().value;
|
||
|
}
|
||
|
|
||
|
function getEmoji(card) {
|
||
|
return /* HTML */ `
|
||
|
<span class="card" style="background-color: white; color: ${card.color}"
|
||
|
>${card.number}${card.emoji}</span
|
||
|
>
|
||
|
`;
|
||
|
}
|
||
|
|
||
|
module.exports = { getCard, getEmoji };
|