Skip to content
TheChatArchiveThe Chat Archive
gpt-4-0314· openaiHugging Face Suzhen/CodeChat

Create a memory game with JavaScript

  1. User

    Create a memory game with JavaScript

  2. Assistant

    This is a simple implementation of a memory game using HTML, CSS, and JavaScript, where each card hides an image, and you need to find all pairs of matching images.

    1. First, create the HTML structure for the memory game. In this example, we have a simple 4x4 grid of cards.
    <!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 Game</title>
        <link rel="stylesheet" href="styles.css">
    </head>
    <body>
        <div class="memory-game">
            <div class="card" data-image="1"></div>
            <div class="card" data-image="1"></div>
            <div class="card" data-image="2"></div>
            <div class="card" data-image="2"></div>
    
            <div class="card" data-image="3"></div>
            <div class="card" data-image="3"></div>
            <div class="card" data-image="4"></div>
            <div class="card" data-image="4"></div>
    
            <div class="card" data-image="5"></div>
            <div class="card" data-image="5"></div>
            <div class="card" data-image="6"></div>
            <div class="card" data-image="6"></div>
    
            <div class="card" data-image="7"></div>
            <div class="card" data-image="7"></div>
            <div class="card" data-image="8"></div>
            <div class="card" data-image="8"></div>
        </div>
        <script src="memory-game.js"></script>
    </body>
    </html>
    
    1. Next, create the CSS file styles.css to style our memory game.
    body {
        display: flex;
        justify-content: center;
        align-items: center;
        height: 100vh;
        margin: 0;
        background-color: #333;
    }
    
    .memory-game {
        display: grid;
        grid-template-columns: repeat(4, 1fr);
        gap: 1rem;
    }
    
    .card {
        display: flex;
        justify-content: center;
        align-items: center;
        width: 100px;
        height: 100px;
        background-color: #444;
        cursor: pointer;
        user-select: none;
    }
    
    .card::before {
        content: '';
        display: none;
        width: 50px;
        height: 50px;
    }
    
    .card[data-image="1"]::before {
        background-image: url('path/to/image1.jpg');
    }
    
    .card[data-image="2"]::before {
        background-image: url('path/to/image2.jpg');
    }
    
    /*... rest of the images ...*/
    
    1. Finally, create the JavaScript file memory-game.js to add the game logic.
    document.addEventListener("DOMContentLoaded", () => {
        const cards = document.querySelectorAll(".card");
        let hasFlippedCard = false;
        let firstCard, secondCard;
        let lockBoard = false;
    
        function flipCard() {
            if (lockBoard) return;
            if (this === firstCard) return;
    
            this.classList.toggle("flip");
    
            if (!hasFlippedCard) {
                hasFlippedCard = true;
                firstCard = this;
                return;
            }
    
            secondCard = this;
    
            checkForMatch();
        }
    
        function checkForMatch() {
            let isMatch = firstCard.dataset.image === secondCard.dataset.image;
    
            isMatch ? disableCards() : unflipCards();
        }
    
        function disableCards() {
            firstCard.removeEventListener("click", flipCard);
            secondCard.removeEventListener("click", flipCard);
    
            resetBoard();
        }
    
        function unflipCards() {
            lockBoard = true;
    
            setTimeout(() => {
                firstCard.classList.remove("flip");
                secondCard.classList.remove("flip");
    
                resetBoard();
            }, 1500);
        }
    
        function resetBoard() {
            hasFlippedCard = false;
            lockBoard = false;
            firstCard = null;
            secondCard = null;
        }
    
        (function shuffle() {
            cards.forEach(card => {
                let randomPos = Math.floor(Math.random() * cards.length);
                card.style.order = randomPos;
            });
        })();
    
        cards.forEach(card => card.addEventListener("click", flipCard));
    });
    

    Now you should have a fully functional memory game! You can add more cards and images as needed, remembering to update the HTML, CSS, and JS files as necessary. Store your images locally, or provide the URL to the images in the CSS file.

Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.