-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmemorygame.js
129 lines (118 loc) · 3.31 KB
/
memorygame.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
'use strict';
// Create memorycards and put them in an array
let cardImages = ['chase', 'everest', 'marshall', 'rocky', 'rubble', 'ryder', 'skye', 'zuma'];
let cards = [];
cardImages.forEach (function(image) {
// push each object twice to get pairs
cards.push({image: image, turned: false});
cards.push({image: image, turned: false});
});
// Define if player is allowed to click on cards
let allowedToClick = true;
// Create array to temporary store cards in while comparing them
let clickedCards = [];
// Let the game begin!
startGame(cards);
// Shuffle cards and put them on memory board
function startGame (cards) {
shuffle(cards);
for (let i = 0; i < cards.length; i++) {
let cardElement = document.createElement('div');
cardElement.innerHTML = "<img src='card_images/" + cards[i].image + ".png'>";
cardElement.className = 'backofcard';
document.querySelector('.memoryboard').appendChild(cardElement);
// Push div to card object in cards array
cards[i].element = cardElement;
cardElement.addEventListener('click', function (event) {
onClick(cards[i]);
});
}
}
// Shuffle memory cards using Fisher–Yates shuffle
function shuffle (array) {
let m = array.length;
let t;
let i;
// While there remain elements to shuffle…
while (m) {
// Pick a remaining element…
i = Math.floor(Math.random() * m--);
// And swap it with the current element.
t = array[m];
array[m] = array[i];
array[i] = t;
}
return array;
}
// Flip clicked card to front and compare when two cards have been turned
function onClick (card) {
if (card.turned || !allowedToClick) {
return;
}
flipCardToFront(card);
if (clickedCards.length != 2) {
return;
}
// Compare clicked cards
if (clickedCards[0].image == clickedCards[1].image) {
clickedCards.forEach (function(card) {
// Set green border when player gets matching pair
card.element.setAttribute('id','greenBorder');
});
if (isGameFinished()) {
givePlayerStar();
}
} else {
// Player unable to click until cards have been turned back
allowedToClick = false;
clickedCards.forEach (function(card) {
// Set temporary red border if cards don't match
card.element.setAttribute('id','redBorder');
flipBackToBack(clickedCards);
});
}
clickedCards = [];
};
// Flip card to front when clicked
function flipCardToFront (card) {
card.element.className = 'front';
card.turned = true;
clickedCards.push(card);
}
// Flip cards back if they don't match
function flipBackToBack (clickedCards) {
setTimeout(function() {
clickedCards.forEach (function(card) {
card.element.removeAttribute("id");
card.element.className = 'backofcard';
card.turned = false;
allowedToClick = true;
});
}, 2000); // 2 sec delay
}
// Check if game is finished
function isGameFinished () {
for (let i = 0; i < cards.length; i++) {
if (!cards[i].turned) {
return false;
}
}
return true;
}
// Give player a star when game is finished
function givePlayerStar () {
let star = document.createElement('div');
star.className = 'star';
document.querySelector('.memoryboard').appendChild(star);
}
// Click button to play again
function onPlayAgainClicked () {
document.querySelector('.star').remove();
clickedCards = [];
cards.forEach (function(card) {
card.element.remove();
card.element = null;
card.turned = false;
});
startGame(cards);
}