-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
130 lines (111 loc) · 4 KB
/
Copy pathscript.js
File metadata and controls
130 lines (111 loc) · 4 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
129
// Shuffle function from http://stackoverflow.com/a/2450976
function shuffle(array) {
var currentIndex = array.length,
temporaryValue, randomIndex;
while (currentIndex !== 0) {
randomIndex = Math.floor(Math.random() * currentIndex);
currentIndex -= 1;
temporaryValue = array[currentIndex];
array[currentIndex] = array[randomIndex];
array[randomIndex] = temporaryValue;
}
return array;
}
// timer code
var timer = new easytimer.Timer();
var timerElement = document.getElementById("timer");
timer.addEventListener('secondsUpdated', function (e) {
$('#timer').html(timer.getTimeValues().toString());
});
let Carte8= ["fa fa-diamond", "fa fa-anchor",
"fa fa-bolt", "fa fa-cube", "fa fa-leaf", "fa fa-bicycle",
"fa fa-bomb", "fa fa-paper-plane-o"];
let Carte16 = Carte8.concat(Carte8);
let restartButton = document.querySelector('.restart');
let contatore = [];
let cont0Str = '';
let cont1Str = '';
let gameStarted = false;
let match = 0;
let moves = 1;
let stars = 3;
let starsContainer = document.querySelector('.stars');
let movescounter = document.getElementById("moves");
function createDeck() {
const deck = document.querySelector('.deck');
var shuffledCards = shuffle(Carte16);
for (let carte of shuffledCards) {
const listElement = document.createElement("li");
const cardElement = document.createElement("i");
deck.appendChild(listElement);
listElement.appendChild(cardElement);
listElement.className = "card";
cardElement.className = carte;
}
}
createDeck();
restartButton.addEventListener("click", function() {
location.reload();
})
let allCards = document.querySelectorAll(".card");
function increaseMoves() {
movescounter.textContent = moves;
moves++;
}
function rating() {
if (moves === 10) {
stars--;
starsContainer.removeChild(starsContainer.lastElementChild);
}
if (moves === 20) {
stars--;
starsContainer.removeChild(starsContainer.lastElementChild);
}
}
allCards.forEach(function (carta) {
carta.addEventListener('click', function () {
carta.classList.add('open', 'show', 'disabled');
if (!gameStarted) {
timer.start();
gameStarted = true;
}
contatore.push(carta);
if (contatore.length === 2) {
console.log(contatore[0].firstElementChild.className, contatore[1].firstElementChild.className)
if (contatore[0].firstElementChild.className === contatore[1].firstElementChild.className) {
console.log('Matched!');
contatore[0].classList.add('match');
contatore[1].classList.add('match');
contatore.splice(0, 2);
match++;
console.log(match);
increaseMoves();
console.log(moves);
rating();
if (match === 8) {
timer.stop();
swal({
text: `You won with ${moves} moves and ${stars} stars and you took ${timerElement.innerHTML}`,
icon: "success",
button: {
text: "Play Again?"
}
}).then(
function() {
location.reload();
}
);
}
}
else {
setTimeout(function () {
contatore[0].classList.remove('open', 'show', 'disabled');
contatore[1].classList.remove('open', 'show', 'disabled');
contatore = []
}, 300)
}
increaseMoves();
rating();
}
});
});