-
Notifications
You must be signed in to change notification settings - Fork 49
/
Copy pathapp.js
84 lines (66 loc) · 1.88 KB
/
app.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
function* generatePoker() {
const points = ['A', 2, 3, 4, 5, 6, 7, 8, 9, 10, 'J', 'Q', 'K'];
yield* points.map(p => ['♠️', p]);
yield* points.map(p => ['♣️', p]);
yield* points.map(p => ['♥️', p]);
yield* points.map(p => ['♦️', p]);
}
const cards = generatePoker();
class PickedCards {
constructor(key, storage = localStorage) {
this.key = key;
this.storage = storage;
this.cards = JSON.parse(storage.getItem(key)) || [];
this.cardSet = new Set(this.cards.map(card => card.join('')));
}
add(card) {
this.cards.push(card);
this.cardSet.add(card.join(''));
this.storage.setItem(this.key, JSON.stringify(this.cards));
}
has(card) {
return this.cardSet.has(card.join(''));
}
clear() {
this.storage.clear();
}
}
const pickedCards = new PickedCards('pickedCards');
function* shuffle(cards, pickedCards) {
cards = [...cards];
cards = cards.filter(card => !pickedCards.has(card));
let len = cards.length;
while(len) {
const i = Math.floor(Math.random() * len);
pickedCards.add(cards[i]);
yield cards[i];
[cards[i], cards[len - 1]] = [cards[len - 1], cards[i]];
len--;
}
}
const shuffled = shuffle(cards, pickedCards);
const cardList = document.getElementById('cardList');
const pickBtn = document.getElementById('pickBtn');
const clearBtn = document.getElementById('clearBtn');
function showCard(card) {
const [suit, point] = card;
const cardEl = document.createElement('span');
cardEl.innerHTML = point;
setTimeout(() => {
cardEl.className = suit;
}, 100);
cardList.appendChild(cardEl);
}
[...pickedCards.cards].forEach((card) => {
showCard(card);
});
pickBtn.addEventListener('click', (evt) => {
const card = shuffled.next();
if(!card.done) {
showCard(card.value);
}
});
clearBtn.addEventListener('click', (evt) => {
pickedCards.clear();
window.top.location.reload();
});