-
Notifications
You must be signed in to change notification settings - Fork 0
/
mem.js
131 lines (117 loc) · 3.32 KB
/
mem.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
const cardsArray = [
{
name:'horse',
icon:'<i class="fa-solid fa-horse-head"></i>'
},
{
name:'dove',
icon:'<i class="fa-solid fa-dove"></i>'
},
{
name:'feather',
icon:'<i class="fa-solid fa-feather"></i>'
},
{
name:'dragon',
icon:'<i class="fa-solid fa-dragon"></i>'
},
{
name:'spider',
icon:'<i class="fa-solid fa-spider"></i>'
},
{
name:'jet',
icon:'<i class="fa-solid fa-jet-fighter-up"></i>'
},
{
name:'horse',
icon:'<i class="fa-solid fa-horse-head"></i>'
},
{
name:'dove',
icon:'<i class="fa-solid fa-dove"></i>'
},
{
name:'feather',
icon:'<i class="fa-solid fa-feather"></i>'
},
{
name:'dragon',
icon:'<i class="fa-solid fa-dragon"></i>'
},
{
name:'spider',
icon:'<i class="fa-solid fa-spider"></i>'
},
{
name:'jet',
icon:'<i class="fa-solid fa-jet-fighter-up"></i>'
}
];
//shuffle method last card each one with other postion by using random int
// curr i with random indx - 2 cards swap
let flippedCards = []
let matchedPairs = 0;
shuffleCard();
const gameBoard = document.getElementById('gameBoard')
displayCards();
function shuffleCard(){
for(let i=cardsArray.length-1;i>0;i--){
const randIndex = Math.floor(Math.random()*(i+1));
[cardsArray[i],cardsArray[randIndex]] = [cardsArray[randIndex],cardsArray[i]]
}
}
function displayCards(){
cardsArray.forEach((curr,index,arr)=>{
const card = document.createElement('div');
card.setAttribute('id',index)
card.classList.add('cardback');
card.classList.add("active");
gameBoard.append(card);
card.addEventListener('click',flipCard);
})
}
function flipCard(){
if(flippedCards.length<2 && this.classList.contains('active')){
let cardId = this.getAttribute('id');
flippedCards.push(this);
this.classList.remove('cardback');
this.innerHTML = cardsArray[cardId].icon;
if(flippedCards.length==2){
setTimeout(checkMatch,1000);
}
}
}
function checkMatch(){
const card1Id = flippedCards[0].getAttribute('id');
const card2Id = flippedCards[1].getAttribute('id');
if(cardsArray[card1Id].name === cardsArray[card2Id].name){
flippedCards[0].style.border = 'none';
flippedCards[0].style.background = '#070b1e';
flippedCards[0].innerHTML = '';
flippedCards[0].classList.remove('active');
flippedCards[1].classList.remove('active');
flippedCards[1].style.border = 'none';
flippedCards[1].style.backgroundColor = '#070b1e';
flippedCards[1].innerHTML = "";
matchedPairs++;
checkGameOver();
}
else{
flippedCards[0].innerHTML = '';
flippedCards[0].classList.add('cardback');
flippedCards[1].innerHTML = '';
flippedCards[1].classList.add('cardback');
}
flippedCards = [];
}
function checkGameOver(){
if(matchedPairs == cardsArray.length/2){
while(gameBoard.firstChild){
gameBoard.removeChild(gameBoard.firstChild)
}
gameBoard.innerHTML = 'You Won';
gameBoard.classList.remove('game');
gameBoard.classList.add('won');
}
}