Skip to content

Commit

Permalink
init
Browse files Browse the repository at this point in the history
  • Loading branch information
annkwon1123 authored Jun 6, 2024
0 parents commit 856855c
Show file tree
Hide file tree
Showing 14 changed files with 261 additions and 0 deletions.
Binary file added image/0.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added image/1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added image/10.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added image/2.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added image/3.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added image/4.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added image/5.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added image/6.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added image/7.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added image/8.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added image/9.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
140 changes: 140 additions & 0 deletions memorycard.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,140 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Memory Card Game</title>
<style>
.placeholder{ /* ๋ž˜ํผ */
display:grid;
grid-template-columns: repeat(var(--row),150px);
gap: 20px;
perspective: 800px;
}

.placeholder > .card{ /* ์นด๋“œ ๋’ท๋ฉด */
background-image: url('./image/0.png');
background-position: center;
background-size: contain;
background-repeat: no-repeat;
width: 140px;
height: 200px;
transform-style: preserve-3d;
perspective: 400px;
backface-visibility: hidden;
transform: rotateY(180deg);
text-align: center;
}
.placeholder > .card::before{ /* ์นด๋“œ ์•ž๋ฉด */
background-image: url('./image/`{data-number}`.png');
background-position: center;
background-size: contain;
visibility: hidden;
content: "";
position: absolute;
font-size: 5em;
line-height: 1.125;
font-weight: bold;
top: 50%;
transform: rotateY(180deg) translate(50%,-50%);
backface-visibility: hidden;
background-color: #e8e8e8;
color: #444;
width: 100%;
height: 100%;
}

.placeholder > .card[data-number="1"]::before {
background-image: url('./image/1.png');
background-position: center;
background-size: 100%;
}

.placeholder > .card[data-number="2"]::before {
background-image: url('./image/2.png');
background-position: center;
background-size: 100%;
}

.placeholder > .card[data-number="3"]::before {
background-image: url('./image/3.png');
background-position: center;
background-size: 100%;
}

.placeholder > .card[data-number="4"]::before {
background-image: url('./image/4.png');
background-position: center;
background-size: 100%;
}

.placeholder > .card[data-number="5"]::before {
background-image: url('./image/5.png');
background-position: center;
background-size: 100%;
}

.placeholder > .card[data-number="6"]::before {
background-image: url('./image/6.png');
background-position: center;
background-size: 100%;
}

.placeholder > .card[data-number="7"]::before {
background-image: url('./image/7.png');
background-position: center;
background-size: 100%;
}

.placeholder > .card[data-number="8"]::before {
background-image: url('./image/8.png');
background-position: center;
background-size: 100%;
}

.placeholder > .card[data-number="9"]::before {
background-image: url('./image/9.png');
background-position: center;
background-size: 100%;
}

.placeholder > .card[data-number="10"]::before {
background-image: url('./image/10.png');
background-position: center;
background-size: 100%;
}

.placeholder .card.back{ /* ๋’ท๋ฉด ํ‘œ์‹œ */
transition: transform .5s;
transform: rotateY(0deg);
}
.placeholder .card.front{ /* ํด๋ฆญ ํ›„ ์•ž๋ฉด ํ‘œ์‹œ */
display: block;
transition: transform .5s;
transform: rotateY(180deg);
}
.placeholder .card.front::before{
visibility: visible;
}
.placeholder .card.matched:before{ /* ๋งค์นญ๋œ ์•ž๋ฉด ๋ฐฐ๊ฒฝ์ƒ‰ ๋ณ€๊ฒฝ */
background-color: #673ab7;
color: #fff;
}
.placeholder > .card.back:hover{ /* ์นด๋“œ ๋งˆ์šฐ์Šค ํ˜ธ๋ฒ„ */
transform: scale(1.1);
transition: transform 0.1s linear;
box-shadow: 1px 4px 15px -3px rgba(0,0,0,0.5);
}
input[type=button]{
padding: 5px 20px;
margin: 20px;
}
</style>
</head>
<body>
<div class="placeholder"></div>
<input type="button" name="init" value="๋‹ค์‹œ ์‹œ์ž‘" onclick="doneFinding()">
<script src="./memorycard.js"></script>
</body>
</html>
115 changes: 115 additions & 0 deletions memorycard.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
let cardCount = 20, row = 5, column = Math.floor(cardCount/5), pair = -1, pairindex = -1 // ์นด๋“œ ๊ฐœ์ˆ˜, ํ–‰์ˆ˜, ๋งž๋Š” ์ง ์นด์šดํŠธ์šฉ ๋ณ€์ˆ˜
let arrDeck = [] // ์นด๋“œ ๋ฐฐ์—ด

document.addEventListener('DOMContentLoaded', () => {
cardCount = row * column // ๊ฐ€๋กœ*์„ธ๋กœ ๊ฐœ์ˆ˜๋ฅผ ๋ฌด์กฐ๊ฑด ๋งž์ถค
document.documentElement.style.setProperty('--row', row)

// UI ์ƒ์„ฑ
for (let i = 0; i < cardCount; i++) {
let el = document.createElement('div')
el.id = 'card' + i
el.classList.add('card')
el.classList.add('back')
document.querySelector('.placeholder').appendChild(el)
}

// ํด๋ฆญ ์ด๋ฒคํŠธ ํ•ธ๋“ค๋Ÿฌ
document.querySelector('.placeholder').addEventListener('click', (e) => {
if (e.target.classList.contains('card') && e.target.classList.contains('back')) {
console.log('clicked');
e.target.classList.remove('back')
e.target.classList.add('front')
e.target.style.backgroundImage = `url("./image/${e.target.dataset.number}.png")` // ์•ž๋ฉด ์ด๋ฏธ์ง€ ์„ค์ •
}
})

// ์• ๋‹ˆ๋ฉ”์ด์…˜ ์™„๋ฃŒ ํ•ธ๋“ค๋Ÿฌ - ์• ๋‹ˆ๋ฉ”์ด์…˜ ์ข…๋ฃŒ ํ›„ ๋งค์นญ ํŒ๋‹จ
document.querySelector('.placeholder').addEventListener('transitionend', (e) => {
if (e.target.classList.contains('card')) {
console.log('transitionended');
if (e.target.classList.contains('front')) {
if (pair < 0) {
pair = e.target.dataset.number
pairindex = e.target.dataset.index
} else {
if (pair == e.target.dataset.number && pairindex != e.target.dataset.index) {
// ๋งค์น˜๋จ - ์ปฌ๋Ÿฌ๋ง
document.querySelectorAll('.placeholder .card.front').forEach((card) => { card.classList.add('matched'); })
pair = -1
pairindex = -1
if (document.querySelector('.placeholder .card:not(.matched)') == null) {
// ์™„๋ฃŒ
console.log('card finding end.')
doneFinding()
}
} else if (pairindex != e.target.dataset.index) {
// ๋งค์น˜์•ˆ๋จ - ํŽ˜์–ด ๋ฆฌ์…‹
document.querySelectorAll('.placeholder .card.front:not(.matched)').forEach((card) => {
card.classList.remove('front')
card.classList.add('back')
card.style.backgroundImage = 'url("./image/0.png")' // ๋’ท๋ฉด ์ด๋ฏธ์ง€๋กœ ์žฌ์„ค์ •
})
pair = -1
pairindex = -1
}
}
}
}
})

// ๋ฐฐ์—ด ์…”ํ”Œ
reShuffle(false)
// ์ •๋ณด ์ดˆ๊ธฐํ™”
initCard()
})

function initCard() {
// ์นด๋“œ์— ์…”ํ”Œ ์ˆซ์ž ์ง€์ •
for (let i = 0; i < cardCount; i++) {
document.querySelectorAll('.placeholder .card').forEach((card, idx) => {
card.dataset.number = arrDeck[idx]
card.dataset.index = idx
})
}

// ์ดˆ๊ธฐํ™” ์• ๋‹ˆ๋ฉ”์ด์…˜
let init = window.setInterval(() => {
let card = document.querySelector('.placeholder .card:not(.back)')
if (card) {
card.classList.remove('front')
card.classList.remove('matched')
card.classList.add('back')
card.style.backgroundImage = 'url("./image/0.png")' // ๋’ท๋ฉด ์ด๋ฏธ์ง€๋กœ ์žฌ์„ค์ •
} else {
window.clearInterval(init)
}
}, 50);
}

// ๋ฐฐ์—ด ์…”ํ”Œ ํ˜ธ์ถœ์šฉ
function reShuffle(bReInit) {
if (!bReInit) {
for (let i = 0; i < Math.floor(cardCount / 2); i++) {
arrDeck.push(i + 1, i + 1)
}
}
arrDeck = fyShuffler(arrDeck)
}

// ๋ฐฐ์—ด ์…”ํ”Œ ๋ฉ”์ธ
const fyShuffler = (arr) => {
for (let i = arr.length - 1; i > 0; i--) {
const j = Math.floor((i + 1) * Math.random());
[arr[i], arr[j]] = [arr[j], arr[i]]; // ๋ฐฐ์—ด ๊ฐ’ ๊ตํ™˜
}
return arr
}

// ๋‹ค ์ฐพ์€ ํ›„ ์žฌ ์‹คํ–‰ ์„ ํƒ
function doneFinding() {
if (confirm('์ฐพ๊ธฐ ์™„๋ฃŒ! ๋ฐ์Šคํฌ์—์„œ ์ƒํ’ˆ์„ ์ˆ˜๋ นํ•ด ์ฃผ์„ธ์š”')) {
reShuffle(true)
initCard()
}
}
6 changes: 6 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 856855c

Please sign in to comment.