-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
149 lines (122 loc) · 3.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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
/*
Extension:
- Add better styling
- Slap react on it
- Consider animations? https://reactjs.org/docs/faq-styling.html
- Slap ElectronJs
- Get random images from unsplash
- Consider CDN e.g. cloudinary, cloudflare
- Consider contentful CDN
- https://app.contentful.com/spaces/t55nbyyaqwhu/home
- https://app.contentful.com/spaces/t55nbyyaqwhu/onboarding/copy
- Consider image resizer api e.g. https://img-resize.com/examples
*/
const boardImages = {
"unflipped" : "images/unflipped.jpg",
"burger" : "images/burger.jpg",
"fries" : "images/fries.jpg",
"hotdog" : "images/hotdog.jpg",
"icecream" : "images/icecream.png",
"milkshake" : "images/milkshake.jpg",
"pizza" : "images/pizza.jpg"
}
const matchList = [ "burger", "fries", "hotdog", "icecream", "milkshake", "pizza" ];
let cardList = [];
let cardsChosen_ID = [];
let cardsChosen_Name = [];
let matchesFound = 0;
const grid = document.querySelector('#grid');
function createCard(name, id){
var card = document.createElement('img');
card.setAttribute('src', boardImages[name]);
card.setAttribute('data-id', id);
return card;
}
function createPairs(cardList){
return cardList.reduce((acc, item)=>{
acc.push(item)
acc.push(item)
return acc
}, [])
}
function randomizeList(thisList){
return thisList.sort(() => 0.5 - Math.random());
}
function initializeBoard(){
cardList = randomizeList(createPairs(matchList));
for (let id=0; id < cardList.length; id++){
var card = createCard( "unflipped", id);
card.addEventListener('click', flipCard);
grid.appendChild(card);
}
}
function getImageWithID(id){
let result;
document.querySelectorAll('img').forEach((node)=>{
if (node.getAttribute('data-id') == id){
result = node;
}
});
return result;
}
function showCards(kind){
let image;
cardsChosen_ID.map((id, index)=>{
if(kind == "miss"){
image = boardImages["unflipped"];
} else {
image = boardImages[cardsChosen_Name[index]];
}
getImageWithID(id).setAttribute('src', image);
})
}
function isMatch(){
return (cardsChosen_Name[0] == cardsChosen_Name[1]);
}
function disableChosen(id){
document.querySelectorAll('img').forEach((node)=>{
if (node.getAttribute('data-id') == id){
node.removeEventListener('click', flipCard);
}
});
}
function resetGame(){
cardsChosen_ID = [];
cardsChosen_Name = [];
matchesFound = 0;
document.querySelectorAll('img').forEach((node)=>{
node.setAttribute("src", boardImages["unflipped"]);
})
}
function updateMatchCounter(){
document.querySelector('#counter').innerText = `Found: ${matchesFound}`;
}
function endGame(){
document.querySelector('#counter').innerText = "You won!!!";
}
function flipCard(){
var cardID = this.getAttribute('data-id');
var cardName = cardList[cardID];
cardsChosen_ID.push(cardID);
cardsChosen_Name.push(cardName)
showCards("default")
if(cardsChosen_ID.length > 1){
setTimeout(() => {
if(isMatch()){
matchesFound += 2;
updateMatchCounter();
cardsChosen_ID.map((id)=>{
disableChosen(id);
})
} else {
showCards("miss");
}
cardsChosen_ID = []
cardsChosen_Name = []
if (matchesFound == cardList.length){
endGame();
}
}, 650);
}
}
initializeBoard();