-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmemorygame.js
160 lines (119 loc) · 3.46 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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
//we have to create a board
//creating array with objects inside!
const cardArray=[
{
name:"fries",
img:'images/fries.png'
},
{
name:"pizza",
img:'images/pizza.png'
},
{
name:"cheeseburger",
img:'images/cheeseburger.png'
},
{
name:"hotdog",
img:'images/hotdog.png'
},
{
name:"ice-cream",
img:'images/ice-cream.png'
},
{
name:"milkshake",
img:'images/milkshake.png'
},
//copypasting as we have 12 cards
{
name:"fries",
img:'images/fries.png'
},
{
name:"pizza",
img:'images/pizza.png'
},
{
name:"cheeseburger",
img:'images/cheeseburger.png'
},
{
name:"hotdog",
img:'images/hotdog.png'
},
{
name:"ice-cream",
img:'images/ice-cream.png'
},
{
name:"milkshake",
img:'images/milkshake.png'
}
]
// to randomly sort out the array
cardArray.sort(()=>0.5-Math.random());
// console.log(cardArray);
let grid=document.querySelector('#grid');
let resultDisplay=document.querySelector('#Score');
let clicks=document.querySelector('#click')
let cardsChosenName=[];
let cardsChosenId=[];
let cardsWon=[];
// console.log(gridDisplay);
// creating the board now
function createBoard(){
for(let i=0;i<cardArray.length;i++){
const card=document.createElement('img');
card.setAttribute('src','images/blank.png'); //setting default as blank card
card.setAttribute('data-id',i);
card.addEventListener('click',flipCard)
grid.appendChild(card);
}
}
function checkMatch(){
// cardsChosenName=[];
// cardsChosenId=[];
const cards=document.querySelectorAll('img')
const optionOneId=cardsChosenId[0];
const optionTwoId=cardsChosenId[1];
if(optionOneId==optionTwoId)
{ cards[optionOneId].setAttribute('src','images/blank.png');
cards[optionTwoId].setAttribute('src','images/blank.png');
alert("You pressed the same card");
}
else if(cardsChosenName[0]===cardsChosenName[1])
{
alert("Congrats! Match Found");
//replacing chosen cards with white image
cards[optionOneId].setAttribute('src','images/white.png');
cards[optionTwoId].setAttribute('src','images/white.png');
cards[optionOneId].removeEventListener('click',flipCard);
cards[optionTwoId].removeEventListener('click',flipCard);
cardsWon.push(cardsChosenName);
}
else
{ cards[optionOneId].setAttribute('src','images/blank.png');
cards[optionTwoId].setAttribute('src','images/blank.png');
alert("Sorry, Retry!!");
}
resultDisplay.textContent=cardsWon.length;
cardsChosenName=[];
cardsChosenId=[];
if(cardsWon.length===cardArray.length/2)
{resultDisplay.innerHTML='Congrats you found them all!!'}
}
//function to flip a card
function flipCard(){
let cardId=this.getAttribute('data-id');
//saving Ids of chosen card
cardsChosenName.push(cardArray[cardId].name) //temproraily save chosen name in this array
cardsChosenId.push(cardId);
this.setAttribute('src',cardArray[cardId].img)
//now if 2 clicks are made then check for match must be made
if(cardsChosenName.length==2){
//for a delay use
setTimeout(checkMatch,100);
}
}
createBoard();