-
-
Notifications
You must be signed in to change notification settings - Fork 79
Memory – Pair Game #445
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
Open
okkkko
wants to merge
2
commits into
kottans:main
Choose a base branch
from
okkkko:memory_pairs_game
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Memory – Pair Game #445
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,19 @@ | ||
| <!DOCTYPE html> | ||
| <html lang="en"> | ||
| <head> | ||
| <meta charset="UTF-8"> | ||
| <meta name="viewport" content="width=device-width, initial-scale=1.0"> | ||
| <title>Memory Pairs Game</title> | ||
| <link rel="stylesheet" href="style.css"> | ||
| <link rel="preconnect" href="https://fonts.gstatic.com"> | ||
| <link | ||
| href="https://fonts.googleapis.com/css2?family=Inconsolata&display=swap" | ||
| rel="stylesheet" | ||
| > | ||
| </head> | ||
| <body> | ||
| <h1 class="game_title">Memory Pairs Game</h1> | ||
| <div class="game_field"></div> | ||
| <script src="script.js"></script> | ||
| </body> | ||
| </html> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,84 @@ | ||
| const cardsIds = ["01", "02", "03", "04", "05", "06"]; | ||
| const cardsArr = [...cardsIds, ...cardsIds]; | ||
| const maxNumOfHiddenCards = 12; | ||
| const maxNumOfFlippedCards = 2; | ||
| const delayForHidden = 100; | ||
| const delayForClicked = 800; | ||
| const delayForAlert = 700; | ||
| let gameField = document.querySelector(".game_field"); | ||
| let numOfHiddenCards = 0; | ||
| let arrOfFlippedCardsId = []; | ||
| let firstFlippedContainer; | ||
| let secondFlippedContainer; | ||
|
|
||
| function createCard(id) { | ||
| let flipContainer = document.createElement("div"); | ||
| flipContainer.classList.add("flip-container"); | ||
| flipContainer.setAttribute("id", id); | ||
| flipContainer.innerHTML = ` | ||
| <div class="flipper"> | ||
| <div class="front"> | ||
| <img src="img/paw.jpg"> | ||
| </div> | ||
| <div class="back"> | ||
| <img src="img/funny-cat_${id}.jpg"> | ||
| </div> | ||
| </div>`; | ||
| gameField.append(flipContainer); | ||
| } | ||
| function createField() { | ||
| cardsArr.sort(function () { | ||
| return 0.5 - Math.random(); | ||
| }); | ||
| cardsArr.forEach((id) => createCard(id)); | ||
| } | ||
| function flipCard(event) { | ||
| if (!arrOfFlippedCardsId.length) { | ||
| firstFlippedContainer = event.target.closest(".flip-container"); | ||
| firstFlippedContainer.classList.add("clicked"); | ||
| arrOfFlippedCardsId.push(firstFlippedContainer.id); | ||
| } else if ( | ||
| arrOfFlippedCardsId.length === 1 && | ||
| event.target.closest(".flip-container") !== firstFlippedContainer | ||
| ) { | ||
| secondFlippedContainer = event.target.closest(".flip-container"); | ||
| secondFlippedContainer.classList.add("clicked"); | ||
| arrOfFlippedCardsId.push(secondFlippedContainer.id); | ||
| checkPairs(); | ||
|
Comment on lines
+36
to
+47
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. DRY |
||
| } | ||
| } | ||
| function checkPairs() { | ||
| const [firstId, secondId] = arrOfFlippedCardsId; | ||
| if ( | ||
| firstId === secondId && | ||
| firstFlippedContainer !== secondFlippedContainer | ||
| ) { | ||
| setTimeout(() => { | ||
| firstFlippedContainer.classList.add("hidden"); | ||
| secondFlippedContainer.classList.add("hidden"); | ||
| arrOfFlippedCardsId = []; | ||
| numOfHiddenCards += 2; | ||
| setTimeout(() => { | ||
| checkNumOfHiddenCard(); | ||
| }, delayForAlert); | ||
| }, delayForHidden); | ||
| } else { | ||
| setTimeout(() => { | ||
| firstFlippedContainer.classList.remove("clicked"); | ||
| secondFlippedContainer.classList.remove("clicked"); | ||
| arrOfFlippedCardsId = []; | ||
| }, delayForClicked); | ||
| } | ||
| } | ||
| function checkNumOfHiddenCard() { | ||
| if (numOfHiddenCards === maxNumOfHiddenCards) { | ||
| alert("You win!"); | ||
|
|
||
| gameField.innerHTML = ""; | ||
| numOfHiddenCards = 0; | ||
| createField(); | ||
| } | ||
| } | ||
|
|
||
| createField(); | ||
| gameField.addEventListener("click", flipCard); | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,83 @@ | ||
| * { | ||
| padding: 0; | ||
| margin: 0; | ||
| box-sizing: border-box; | ||
| } | ||
| body { | ||
| background-color: rgb(223, 223, 223); | ||
| } | ||
| .game_title { | ||
| text-align: center; | ||
| margin-top: 5vh; | ||
| font-size: 32px; | ||
| font-family: "Inconsolata", monospace; | ||
| font-weight: 500; | ||
| } | ||
| .game_field { | ||
| display: grid; | ||
| grid-template-columns: repeat(4, 1fr); | ||
| gap: 10px 10px; | ||
| margin-left: auto; | ||
| margin-right: auto; | ||
| margin-top: 30px; | ||
| width: 630px; | ||
| } | ||
| .flip-container { | ||
| perspective: 1000px; | ||
| } | ||
|
|
||
| .flip-container.clicked .flipper { | ||
| transform: rotateY(180deg); | ||
| } | ||
| .flip-container.clicked.hidden { | ||
| visibility: hidden; | ||
| } | ||
| .front img, | ||
| .back img { | ||
| cursor: pointer; | ||
| width: 150px; | ||
| height: 150px; | ||
| border: 2px solid rgb(71, 68, 68); | ||
| border-radius:5px; | ||
| } | ||
| .flip-container { | ||
| width: 150px; | ||
| height: 150px; | ||
| } | ||
|
|
||
| .flipper { | ||
| transition: 0.6s; | ||
| transform-style: preserve-3d; | ||
| position: relative; | ||
| } | ||
|
|
||
| .front, | ||
| .back { | ||
| backface-visibility: hidden; | ||
| position: absolute; | ||
| top: 0; | ||
| left: 0; | ||
| } | ||
| .front { | ||
| z-index: 2; | ||
| transform: rotateY(0deg); | ||
| } | ||
| .back { | ||
| transform: rotateY(180deg); | ||
| } | ||
| @media (max-width: 660px) { | ||
| .game_field { | ||
| grid-template-columns: repeat(3, 1fr); | ||
| width: 320px; | ||
| gap: 5px 5px; | ||
| } | ||
| .front img, | ||
| .back img { | ||
| width: 103px; | ||
| height: 103px; | ||
| } | ||
| .flip-container { | ||
| width: 103px; | ||
| height: 103px; | ||
| } | ||
| } |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Adding elements to DOM from a loop is a bad practice. A browser will run reflow and repaint for every element in the loop. Instead, you can: 1. Use append method, which can add several elements in one operation 2. Create some wrapper, add your items to the wrapper and then add it to DOM. It will be one operation. 3. Clone current container. Add items to a container and then replace your old container with a new one. But be aware of event listeners. 4. Use innerHTML instead
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Got it, thanks☺️