-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
113 lines (87 loc) · 2.63 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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
const CARD_NAMES = ['bear', 'bill', 'blaster', 'diary', 'dragon', 'fighter'];
const WIN_CON_CARDS_NUM = 12;
const NUM_OF_CARDS_TO_CHECK = 2;
const state = {
flippedCards: {
flippedCardsCount: null,
flippedCollection: null,
},
hiddenCardsCount: null,
gameState: null,
};
const cardsContainer = document.querySelector('.cards-container');
const cardTemplate = (cardName) => `
<div class="card" data-card-id="${cardName}" data-flip="false">
<div class="card-back">
<img src="./resources/gf-pics/${cardName}.png" alt="flag" class="card-img" />
</div>
<div class="card-front">
</div>
</div>
`;
const shuffleCards = (cardNames) => {
return [...cardNames, ...cardNames].sort(() => 0.5 - Math.random());
};
const makeCardpool = () => {
const shuffledCards = shuffleCards(CARD_NAMES);
cardsContainer.innerHTML = shuffledCards.map(cardTemplate).join('');
state.gameState = 'in progress';
};
const sleep = (ms) => new Promise((resolve) => setTimeout(resolve, ms));
const isCardBlocked = (card) => card.dataset.flip !== 'blocked';
const isFlippedCardsCount = () =>
state.flippedCards.flippedCardsCount === NUM_OF_CARDS_TO_CHECK;
const hasIdentity = () => {
const [first, second] = state.flippedCards.flippedCollection.map(
(el) => el.dataset.cardId
);
return first === second;
};
const hasWinCondition = () =>
document.querySelectorAll('.hidden').length === WIN_CON_CARDS_NUM;
const flipCard = (card) => {
card.classList.add('flip');
card.dataset.flip = 'true';
};
const hideCards = () => {
state.flippedCards.flippedCollection.forEach((card) => {
card.dataset.flip = 'blocked';
card.querySelector('.card-back').classList.add('hidden');
});
};
const unFlip = async () => {
await sleep(500);
state.flippedCards.flippedCollection.forEach((el) => {
el.classList.remove('flip');
el.dataset.flip = 'false';
});
state.flippedCards.flippedCardsCount = 0;
};
const endGame = async () => {
await sleep(500);
alert('You win');
state.gameState = null;
render(state);
};
const render = (state) => {
if (!state.gameState) makeCardpool();
};
const handler = ({ target }) => {
const card = target.closest('.card');
if(!card) return;
if (isCardBlocked(card)) {
flipCard(card);
const flippedCards = document.querySelectorAll('[data-flip="true"]');
state.flippedCards.flippedCardsCount = flippedCards.length;
state.flippedCards.flippedCollection = [...flippedCards];
}
if (isFlippedCardsCount()) {
if (hasIdentity()) hideCards();
else unFlip();
}
if (hasWinCondition()) endGame();
};
cardsContainer.addEventListener('click', handler);
export default () => {
render(state);
};