I was presented with a coding exercise to make a memory game using any language. I decided such a task really doesn’t require any libraries like React or jQuery, so I made it with plain JavaScript.

Since the memory game reminded me of the card game from Super Mario 3, I decided to use that as the basis for the style of my app. You can view the completed demo here.

Prerequisites

We’re also going to use some ES6 and some basic DOM concepts, but you can learn and follow along even if you don’t know them yet.

Goals

The premise of the game is to have a grid of 24 face-down cards. The card faces consist of pairs of matches. Clicking on cards will flip them over, revealing the value. When two are chosen, if it’s a match, both cards will disappear. If it’s not, the cards will flip back over to face-down. The game should be different every time the game is refreshed.

Click the demo below to get an understanding of what we’ll be creating.

Planning

Before writing any code, we need to get an idea of the actual steps to take to go from nothing to finished product. Building this whole game at once might seem a bit overwhelming, but if you break it into small tasks, each task will seem achievable until it’s done.

  • Display 12 cards.
  • Duplicate the cards to have 2 sets of 12.
  • Randomize the display of cards.
  • Add selected style for selected cards.
  • Only allow two cards to be selected at a time.
  • Determine if two selected cards are a match and hide them.
  • Reset guess count after 2.
  • Add delay to selections.
  • Show back of card initially and flip on select
  • Finished game!

Now, there are probably a million and one ways to create this game. This is the way we’re going to do it.

Each instruction will be a version. Version 0.1, 0.2, 0.3, until reaching version 1.0. I will place a link to the JavaScript file at the end of each version so you don’t get lost. All of the versions can be found here.

#game #javascript #programming

Create a Memory Game (ala Super Mario 3) with JavaScript
2.90 GEEK