Skip to content

Commit 88d1f75

Browse files
Merge pull request #45 from ARYAN-CODES-STAR/aryajshack
Added Memory Game folder
2 parents ce77252 + b1ee4d2 commit 88d1f75

14 files changed

Lines changed: 507 additions & 0 deletions

File tree

MEMORY/images/blank.png

987 Bytes
Loading

MEMORY/images/cheeseburger.png

7.27 KB
Loading

MEMORY/images/fries.png

3.52 KB
Loading

MEMORY/images/hotdog.png

8.8 KB
Loading

MEMORY/images/ice-cream.png

1.3 KB
Loading

MEMORY/images/milkshake.png

4.87 KB
Loading

MEMORY/images/pizza.png

7.63 KB
Loading

MEMORY/images/white.png

420 Bytes
Loading

MEMORY/index.html

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<!DOCTYPE html>
2+
<html lang="en" dir="ltr">
3+
<head>
4+
<meta charset="UTF-8">
5+
<title>Memory Game</title>
6+
<link rel="stylesheet" href="style.css"></link>
7+
<!-- <script src="index.js" charset="utf-8"></script> -->
8+
9+
</head>
10+
<body>
11+
<h1 class="heading">MEMORY GAME</h1>
12+
<h3>Score:<span id="result"></span></h3>
13+
14+
<div class="grid">
15+
</div>
16+
<marquee>Touch and match to get points</marquee>
17+
<script src="index.js" charset="utf-8"></script>
18+
</body>
19+
</html>

MEMORY/index.js

Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
document.addEventListener('DOMContentLoaded', () => {
2+
3+
4+
const cardArray = [
5+
{
6+
name: 'fries',
7+
img: 'images/fries.png'
8+
},
9+
{
10+
name: 'cheeseburger',
11+
img: 'images/cheeseburger.png'
12+
},
13+
{
14+
name: 'ice-cream',
15+
img: 'images/ice-cream.png'
16+
},
17+
{
18+
name: 'pizza',
19+
img: 'images/pizza.png'
20+
},
21+
{
22+
name: 'milkshake',
23+
img: 'images/milkshake.png'
24+
},
25+
{
26+
name: 'hotdog',
27+
img: 'images/hotdog.png'
28+
},
29+
{
30+
name: 'fries',
31+
img: 'images/fries.png'
32+
},
33+
{
34+
name: 'cheeseburger',
35+
img: 'images/cheeseburger.png'
36+
},
37+
{
38+
name: 'ice-cream',
39+
img: 'images/ice-cream.png'
40+
},
41+
{
42+
name: 'pizza',
43+
img: 'images/pizza.png'
44+
},
45+
{
46+
name: 'milkshake',
47+
img: 'images/milkshake.png'
48+
},
49+
{
50+
name: 'hotdog',
51+
img: 'images/hotdog.png'
52+
}
53+
]
54+
55+
cardArray.sort(() => 0.5 - Math.random())
56+
57+
const grid = document.querySelector('.grid')
58+
const resultDisplay = document.querySelector('#result')
59+
let cardsChosen = []
60+
let cardsChosenId = []
61+
let cardsWon = []
62+
63+
//create your board
64+
function createBoard() {
65+
for (let i = 0; i < cardArray.length; i++) {
66+
const card = document.createElement('img')
67+
card.setAttribute('src', 'images/blank.png')
68+
card.setAttribute('data-id', i)
69+
card.addEventListener('click', flipCard)
70+
grid.appendChild(card)
71+
}
72+
}
73+
74+
//check for matches
75+
function checkForMatch() {
76+
const cards = document.querySelectorAll('img')
77+
const optionOneId = cardsChosenId[0]
78+
const optionTwoId = cardsChosenId[1]
79+
80+
if(optionOneId == optionTwoId) {
81+
cards[optionOneId].setAttribute('src', 'images/blank.png')
82+
cards[optionTwoId].setAttribute('src', 'images/blank.png')
83+
alert('You have clicked the same image!')
84+
}
85+
else if (cardsChosen[0] === cardsChosen[1]) {
86+
alert('You found a match')
87+
cards[optionOneId].setAttribute('src', 'images/white.png')
88+
cards[optionTwoId].setAttribute('src', 'images/white.png')
89+
cards[optionOneId].removeEventListener('click', flipCard)
90+
cards[optionTwoId].removeEventListener('click', flipCard)
91+
cardsWon.push(cardsChosen)
92+
} else {
93+
cards[optionOneId].setAttribute('src', 'images/blank.png')
94+
cards[optionTwoId].setAttribute('src', 'images/blank.png')
95+
alert('Sorry, try again')
96+
}
97+
cardsChosen = []
98+
cardsChosenId = []
99+
resultDisplay.textContent = cardsWon.length
100+
if (cardsWon.length === cardArray.length/2) {
101+
resultDisplay.textContent = 'Congratulations! You found them all!'
102+
}
103+
}
104+
105+
//flip your card
106+
function flipCard() {
107+
let cardId = this.getAttribute('data-id')
108+
cardsChosen.push(cardArray[cardId].name)
109+
cardsChosenId.push(cardId)
110+
this.setAttribute('src', cardArray[cardId].img)
111+
if (cardsChosen.length ===2) {
112+
setTimeout(checkForMatch, 500)
113+
}
114+
}
115+
116+
createBoard()
117+
})

0 commit comments

Comments
 (0)