Skip to content

[BER-WDPT-2024] Yujiro Akihiro #1961

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
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
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
34 changes: 15 additions & 19 deletions index.html
Original file line number Diff line number Diff line change
@@ -1,23 +1,19 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<meta http-equiv="X-UA-Compatible" content="ie=edge" />
<title>Superhero Memory Game</title>
<!-- LINK THE STYLES HERE -->
</head>
<body>
<div>
<h1>Superhero Memory Game</h1>
<html lang="ja">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Memory Game</title>
<link rel="stylesheet" href="styles/style.css">
</head>
<body>
<div id="game-board">
<!-- カードが表示される場所 -->
</div>
<div id="score">
<h2>Score</h2>
<p>Pairs clicked: <span id="pairs-clicked">0</span></p>
<p>Pairs guessed: <span id="pairs-guessed">0</span></p>
<div id="score-board">
<!-- スコアが表示される場所 -->
</div>
<div id="memory-board"></div>

<!-- LINK THE JAVASCRIPT FILES HERE (keep in mind that the order in which you link them MATTERS) -->
</body>
<script src="src/memory.js"></script>
<script src="src/index.js"></script>
</body>
</html>
100 changes: 59 additions & 41 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,51 +1,69 @@
const cards = [
{ name: 'aquaman', img: 'aquaman.jpg' },
{ name: 'batman', img: 'batman.jpg' },
{ name: 'captain america', img: 'captain-america.jpg' },
{ name: 'fantastic four', img: 'fantastic-four.jpg' },
{ name: 'flash', img: 'flash.jpg' },
{ name: 'green arrow', img: 'green-arrow.jpg' },
{ name: 'green lantern', img: 'green-lantern.jpg' },
{ name: 'ironman', img: 'ironman.jpg' },
{ name: 'spiderman', img: 'spiderman.jpg' },
{ name: 'superman', img: 'superman.jpg' },
{ name: 'the avengers', img: 'the-avengers.jpg' },
{ name: 'thor', img: 'thor.jpg' },
{ name: 'aquaman', img: 'aquaman.jpg' },
{ name: 'batman', img: 'batman.jpg' },
{ name: 'captain america', img: 'captain-america.jpg' },
{ name: 'fantastic four', img: 'fantastic-four.jpg' },
{ name: 'flash', img: 'flash.jpg' },
{ name: 'green arrow', img: 'green-arrow.jpg' },
{ name: 'green lantern', img: 'green-lantern.jpg' },
{ name: 'ironman', img: 'ironman.jpg' },
{ name: 'spiderman', img: 'spiderman.jpg' },
{ name: 'superman', img: 'superman.jpg' },
{ name: 'the avengers', img: 'the-avengers.jpg' },
{ name: 'thor', img: 'thor.jpg' }
];

const memoryGame = new MemoryGame(cards);
// src/index.js

window.addEventListener('load', (event) => {
let html = '';
const cardsArray = [
{ name: 'aquaman', img: 'img/aquaman.jpg' },
{ name: 'batman', img: 'img/batman.jpg' },
{ name: 'captain america', img: 'img/captain-america.jpg' },
{ name: 'fantastic four', img: 'img/fantastic-four.jpg' },
{ name: 'flash', img: 'img/flash.jpg' },
{ name: 'green arrow', img: 'img/green-arrow.jpg' },
{ name: 'green lantern', img: 'img/green-lantern.jpg' },
{ name: 'ironman', img: 'img/ironman.jpg' },
{ name: 'aquaman', img: 'img/aquaman.jpg' },
{ name: 'batman', img: 'img/batman.jpg' },
{ name: 'captain america', img: 'img/captain-america.jpg' },
{ name: 'fantastic four', img: 'img/fantastic-four.jpg' },
{ name: 'flash', img: 'img/flash.jpg' },
{ name: 'green arrow', img: 'img/green-arrow.jpg' },
{ name: 'green lantern', img: 'img/green-lantern.jpg' },
{ name: 'ironman', img: 'img/ironman.jpg' }
];

const memoryGame = new MemoryGame(cardsArray);
memoryGame.shuffleCards();

const memoryBoard = document.querySelector('#memory-board');
memoryGame.cards.forEach((pic) => {
html += `
<div class="card" data-card-name="${pic.name}">
<div class="back" name="${pic.img}"></div>
<div class="front" style="background: url(img/${pic.img}) no-repeat"></div>
</div>
`;
const cardHTML = `<div class="card" data-card-name="${pic.name}">
<div class="back" name="${pic.img}"></div>
<div class="front" style="background: url(img/${pic.img}) no-repeat"></div>
</div>`;
memoryBoard.innerHTML += cardHTML;
});

// Add all the divs to the HTML
document.querySelector('#memory-board').innerHTML = html;

// Bind the click event of each element to a function
document.querySelectorAll('.card').forEach((card) => {
card.addEventListener('click', () => {
// TODO: write some code here
console.log(`Card clicked: ${card}`);
if (memoryGame.pickedCards.length < 2 && !card.classList.contains('turned')) {
card.classList.add('turned');
memoryGame.pickedCards.push(card);

if (memoryGame.pickedCards.length === 2) {
const card1 = memoryGame.pickedCards[0];
const card2 = memoryGame.pickedCards[1];

const isPair = memoryGame.checkIfPair(
card1.getAttribute('data-card-name'),
card2.getAttribute('data-card-name')
);

if (isPair) {
card1.classList.add('blocked');
card2.classList.add('blocked');
memoryGame.pickedCards = [];
} else {
setTimeout(() => {
card1.classList.remove('turned');
card2.classList.remove('turned');
memoryGame.pickedCards = [];
}, 1000);
}

if (memoryGame.checkIfFinished()) {
alert('You won!');
}
}
}
});
});
});
27 changes: 22 additions & 5 deletions src/memory.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,35 @@
// src/memory.js

class MemoryGame {
constructor(cards) {
this.cards = cards;
// add the rest of the class properties here
this.cards = cards || [];
this.pickedCards = [];
this.pairsClicked = 0;
this.pairsGuessed = 0;
}

shuffleCards() {
// ... write your code here
if (!this.cards) return undefined;
for (let i = this.cards.length - 1; i > 0; i--) {
const j = Math.floor(Math.random() * (i + 1));
[this.cards[i], this.cards[j]] = [this.cards[j], this.cards[i]];
}
}

checkIfPair(card1, card2) {
// ... write your code here
this.pairsClicked++;
if (card1 === card2) {
this.pairsGuessed++;
return true;
}
return false;
}

checkIfFinished() {
// ... write your code here
return this.pairsGuessed === this.cards.length / 2;
}
}

if (typeof module !== 'undefined') {
module.exports = MemoryGame;
}
4 changes: 2 additions & 2 deletions styles/style.css
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
body {
margin: 0;

background: url('../img/background.jpg') no-repeat;
background: url("../img/background.jpg") no-repeat;
background-size: cover;
font-family: 'Helvetica Neue', Arial, sans-serif;
font-family: "Helvetica Neue", Arial, sans-serif;
}

#memory-board {
Expand Down