-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
122 lines (95 loc) · 3.84 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
// <Memory Game: Classic memory game where you need to find pair of cards.>
// Copyright (C) <2022> <Cromega>
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published
// by the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
const imgs = [
"/imgs/compass.png",
"/imgs/crab.png",
"/imgs/dolphin.png",
"/imgs/fish.png",
"/imgs/island.png",
"/imgs/jellyfish.png",
"/imgs/lifebuoy.png",
"/imgs/map.png",
"/imgs/octopus.png",
"/imgs/porthole.png",
"/imgs/rudder.png",
"/imgs/seagull.png",
"/imgs/seaweed.png",
"/imgs/starfish.png",
"/imgs/turtle.png",
"/imgs/anglerfish.png"],
content = document.querySelector("main"),
header = document.querySelector("h1")
let chosed = [], chosed_id = [], cards = [], random_imgs = []
function randomize(amount = Number, array) {
let new_array = array.sort(() => 0.5 - Math.random()).slice(0, amount/2),
output = [...new_array, ...new_array].sort(() => 0.5 - Math.random())
return output
}
function create_board(amount = Number, array_normal = Array, array_random = Array, array_elements = Array, container = Document) {
let selected = []
array_random = randomize(amount, array_normal)
header.remove()
container.innerHTML = ""
for (let index = 0; index < array_random.length; ++index) {
const card = document.createElement("img")
card.setAttribute("src", "/imgs/wave.png")
card.setAttribute("alt", "wave.png")
card.setAttribute("data-id", index)
card.addEventListener("click", flip)
array_elements.push(card)
selected.push(array_random[index])
container.append(card)
}
random_imgs = array_random
}
function flip() {
let img_id = this.getAttribute("data-id"),
img_src = random_imgs[img_id],
img_name = img_id.split("/")[3]
if (chosed_id.length > 0 && img_id === chosed_id[0]) return;
chosed.push(img_src)
chosed_id.push(img_id)
this.setAttribute("src", img_src)
this.setAttribute("alt", img_name)
if (chosed.length > 1) {
if (chosed[0] === chosed[1]) {
cards[chosed_id[0]].setAttribute("class", "guessed")
cards[chosed_id[0]].removeEventListener("click", flip)
cards[chosed_id[0]].setAttribute("src", img_src)
cards[chosed_id[0]].setAttribute("alt", img_name)
cards[chosed_id[1]].setAttribute("class", "guessed")
cards[chosed_id[1]].removeEventListener("click", flip)
cards[chosed_id[1]].setAttribute("src", img_src)
cards[chosed_id[1]].setAttribute("alt", img_name)
delete cards[chosed_id[0]]
delete cards[chosed_id[1]]
chosed.splice(0, 2)
chosed_id.splice(0, 2)
if (cards.every(card => {card === undefined})) {
content.innerHTML = "You Win"
content.setAttribute("class", "win")
}
}
else {
chosed.splice(0, 2); chosed_id.splice(0, 2)
setTimeout(backflip, 800, cards)
}
}
}
function backflip(array = Array) {
array.forEach(element => {
if (element === undefined) return;
element.setAttribute("src", "/imgs/wave.png")
element.setAttribute("alt", "wave.png")
});
}