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
fixed matching and winning
  • Loading branch information
Vlad Kovalov committed Aug 5, 2022
commit ab931ea03b356c53d39b5175e1ab8c1b0fb8293a
37 changes: 16 additions & 21 deletions submissions/kovalov/memory-pair-game/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ function getShuffledImages(imageNames, filePath) {
function createCard(cardDetails) {
const cardElement = document.createElement('div');
cardElement.innerHTML = `
<div class="game__card"
<div class="game__card"
data-is-opened="false"
data-name="${cardDetails.imageName}"
>
Expand Down Expand Up @@ -87,26 +87,25 @@ function checkWin() {
gameContainer.dataset.isActive = false;

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

gameMenu.innerHTML = HTML;
gameMenu.innerHTML = successMessage;
}

function handleClick(event) {
timesClicked++;

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

if (!currentCard) {
timesClicked++;
currentCard = event.target.closest('.game__card');
currentCard.dataset.isOpened = true;
return;
Expand All @@ -117,29 +116,25 @@ function handleClick(event) {
previousCard.dataset.isOpened = true;
isActive = false;

const { name: currentCardName } = currentCard.dataset;
const { name: previousCardName } = previousCard.dataset;
if (currentCard.dataset.name === previousCard.dataset.name) {
timesMatched++;
checkWin();
currentCard = '';
previousCard = '';
isActive = true;
return;
}

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

clearVariables();
currentCard = '';
previousCard = '';
isActive = true;

return;
}, 1000);
}

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

checkWin();
clearVariables();
isActive = true;
return;
}
}
}
}
Expand Down
4 changes: 4 additions & 0 deletions submissions/kovalov/memory-pair-game/styles.css
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,10 @@ body {
border-radius: 5px;
}

.game__card[data-is-opened="true"] {
pointer-events: none;
}

.game__card-flipper {
height: 100%;
width: 100%;
Expand Down