Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Memory pair game #42

Merged
merged 8 commits into from
Aug 15, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
added amended files
  • Loading branch information
Vlad Kovalov committed Aug 2, 2022
commit f0dbcf058c8ff5cb1b876555a08c1bff9ae4ea1b
25 changes: 21 additions & 4 deletions submissions/kovalov/memory-pair-game/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,30 @@
name="viewport"
content="width=device-width, initial-scale=1.0"
/>
<link rel="stylesheet" href="styles.css" />
<title>Memory Game App</title>
<link rel="stylesheet" href="./styles.css" />
<title>Document</title>
</head>
<body>
<div class="page-wrapper">
<main class="game" data-game-content></main>
<main class="game" data-game data-is-active="false">
<div class="game__flipper">
<div class="game__front">
<div class="game__menu" data-menu>
<h1 class="game__menu-title">Memory game app</h1>
<p class="game__menu-description">
Turn any two cards picture-side-up and find the pair.
</p>
<button class="game__menu-button" data-button="start">
Start
</button>
</div>
</div>
<div class="game__back">
<div class="game__grid" data-grid></div>
</div>
</div>
</main>
</div>
<script type="module" src="./scripts/main.js"></script>
<script src="./main.js"></script>
</body>
</html>
147 changes: 147 additions & 0 deletions submissions/kovalov/memory-pair-game/main.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,147 @@
const imageNames = [
'img-1.png',
'img-2.png',
'img-3.png',
'img-4.png',
'img-5.png',
'img-6.png',
'img-7.png',
'img-8.png',
];

let currentCard = '';
let previousCard = '';
let isActive = true;
let timesClicked = 0;
let timesMatched = 0;

const gameContainer = document.querySelector('[data-game]');
const gameMenu = document.querySelector('[data-menu]');
const cardGrid = document.querySelector('[data-grid]');

function getShuffledImages(imageNames, filePath) {
return [...imageNames, ...imageNames]
.sort(() => 0.5 - Math.random())
.map((imageName) => ({
imageName,
src: `${filePath}/${imageName}`,
}));
}

function createCard(cardDetails) {
const cardElement = document.createElement('div');
cardElement.innerHTML = `
<div class="game__card"
data-is-opened="false"
data-name="${cardDetails.imageName}"
>
<div class="game__card-flipper">
<div class="game__card-front">
<span class="game__card-icon">?</span>
</div>
<div class="game__card-back">
<img src="${cardDetails.src}" alt="" class="game__card-image"
/>
</div>
</div>
</div>
`;
return cardElement;
}

function initGame(imageNames, filePath, container) {
const shuffledImages = getShuffledImages(imageNames, filePath);
shuffledImages
.map(createCard)
.forEach((card) => addCard(card, container));
}

function addCard(cardElement, container) {
container.appendChild(cardElement);
}

gameMenu.addEventListener('click', (event) => {
if (event.target.dataset.button !== 'start') return;

cardGrid.innerHTML = '';

currentCard = '';
previousCard = '';

timesClicked = 0;
timesMatched = 0;

gameContainer.dataset.isActive = true;
initGame(imageNames, './assets/images', cardGrid);
});

function clearVariables() {
currentCard = '';
previousCard = '';
}

function checkWin() {
if (timesMatched !== 8) return;

isActive = false;
gameContainer.dataset.isActive = false;

gameMenu.innerHTML = '';
const HTML = `
<h1 class="game__menu-title">You win!</h1>
<p class="game__menu-description">
It has taken ${timesClicked} clicks.
</p>
<button class="game__menu-button" data-button="start">
Play Again
</button>
`;

gameMenu.innerHTML = HTML;
}

function handleClick(event) {
timesClicked++;

if (isActive) {
if (!event.target.closest('.game__card')) return;

if (!currentCard) {
currentCard = event.target.closest('.game__card');
currentCard.dataset.isOpened = true;
return;
}

if (currentCard) {
previousCard = event.target.closest('.game__card');
previousCard.dataset.isOpened = true;
isActive = false;

const { name: currentCardName } = currentCard.dataset;
const { name: previousCardName } = previousCard.dataset;

if (currentCardName !== previousCardName) {
setTimeout(() => {
currentCard.dataset.isOpened = false;
previousCard.dataset.isOpened = false;

clearVariables();
isActive = true;

return;
}, 1000);
}

if (currentCardName === previousCardName) {
timesMatched++;

checkWin();
clearVariables();
isActive = true;
return;
}
}
}
}

cardGrid.addEventListener('click', handleClick);
3 changes: 0 additions & 3 deletions submissions/kovalov/memory-pair-game/scripts/addGameCard.js

This file was deleted.

10 changes: 0 additions & 10 deletions submissions/kovalov/memory-pair-game/scripts/cardImages.js

This file was deleted.

20 changes: 0 additions & 20 deletions submissions/kovalov/memory-pair-game/scripts/createGameCard.js

This file was deleted.

11 changes: 0 additions & 11 deletions submissions/kovalov/memory-pair-game/scripts/getShuffledImages.js

This file was deleted.

60 changes: 0 additions & 60 deletions submissions/kovalov/memory-pair-game/scripts/main.js

This file was deleted.

Loading