-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmemory.js
More file actions
128 lines (100 loc) · 3.33 KB
/
memory.js
File metadata and controls
128 lines (100 loc) · 3.33 KB
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
const delay = (time) => {
return new Promise(resolve => setTimeout(resolve, time));
};
// Genera i simboli per ogni bottone
const generateButtons = (() => {
// funzione interna, sample di un elemento da un array
const sample = (a) => {
return a[Math.floor(Math.random() * a.length)];
};
// funzione interna, rimuove un elemento da un array
const remove = (a, value) => {
let i = a.indexOf(value);
a.splice(i, 1);
return a;
};
// possibili simboli (hardcoded)
let symbols = [..."👾🧠🦓🐠🦢🌈🍕🍹"];
let buttons = new Array();
for (let i = 0; i < 16; i++) {
buttons.push({
id: i,
btn: document.getElementById(`btn-${i}`),
available: true
});
}
// pick di una coppia e assegnazione del primo simbolo
let pairs = [...Array(buttons.length).keys()];
while (pairs.length >= 2) {
let m1 = sample(pairs);
pairs = remove(pairs, m1);
let m2 = sample(pairs);
pairs = remove(pairs, m2);
buttons[m1].value = symbols[0];
buttons[m2].value = symbols[0];
symbols = symbols.splice(1);
}
return buttons;
});
const flipCard = (card, value, gameInstance) => {
card.btn.textContent = value;
}
const handleMove = async (currentBtn, gameInstance) => {
let currentCard = gameInstance.cards.find(card => card.btn === currentBtn);
if (!currentCard.available)
return;
// stato 0: ho scelto una carta
if (gameInstance.state === 0) {
gameInstance.lastCard = currentCard;
gameInstance.lastCard.available = false;
flipCard(gameInstance.lastCard, gameInstance.lastCard.value, gameInstance);
gameInstance.state = 1;
// stato 1: una seconda carta è stata flippata, check se accoppiata con la prima
} else {
flipCard(currentCard, currentCard.value, gameInstance);
await delay(1000);
if (currentCard.btn.textContent === gameInstance.lastCard.btn.textContent) {
currentCard.available = false;
} else {
flipCard(currentCard, '\u00A0', gameInstance);
flipCard(gameInstance.lastCard, '\u00A0', gameInstance);
currentCard.available = true;
gameInstance.lastCard.available = true;
}
gameInstance.state = 0;
}
gameInstance.nMoves++;
gameInstance.updateMoves();
};
class Game {
// 0 -> nessuna scelta fatta
// 1 -> una carta scoperta
state = 0;
// numero di mosse
nMoves= 0;
// tutte le carte attualmente disponibili
cards = generateButtons();
// ultima carta scelta
lastCard = undefined;
// aggiorna il componente delle mosse
updateMoves = () => {
document.getElementById("nMoves").textContent = this.nMoves;
};
// resetta il gioco
reset = () => {
this.state = 0;
this.nMoves = 0;
this.cards = generateButtons(),
this.lastCard = undefined
for (let card of this.cards)
flipCard(card, '\u00A0', this);
this.updateMoves();
};
};
const game = new Game();
for (let card of game.cards) {
card.btn.addEventListener("click", (e) => {
handleMove(e.target, game);
});
}
document.getElementById("reset").addEventListener("click", game.reset);