Google Sheets Memory Game
Ready to play the classic game of memory in Google Sheets?
Here it is:
Google Sheets Memory Game
Play Memory with 8, 18, 32, or 50 pairs (of emojis) in a spreadsheet using mouse clicks to reveal potential pairs.
Get GameHow To Play Memory In Google Sheets
Here's what the game looks like:
You can:
- Start a new game
- Change the board to contain 8, 18, 32, or 50 pairs
- Play the game by clicking on the checkboxes
- Reveal further game instructions
- See how many turns you've taken
The board builds itself when you change the number of pairs (or start a new game):
Then you click on the checkboxes to reveal potential pairs:
When you find a match the checkboxes disappear:
When you've matched every pair the game is won:
While this is going on the turn counter is keeping score:
If a player hasn't read this post, there are instructions hidden in a row group ready to be revealed when needed:
FREE RESOURCE
Google Sheets Cheat Sheet
12 exclusive tips to make user-friendly sheets from today:
You'll get updates from me with an easy-to-find "unsubscribe" link.
Making A Game Of Memory In Google Sheets
The game is made to look good using:
- Checkboxes for interactivity (and to avoid authorization requests)
- A row group to hide instructions
- A nice font (Pacifico), and
- Purple (#8a6be2)
The actual mechanics of the game all happen within the attached Google Apps Script.
You can make a copy of the sheet to read through the code.
Here, I'm going to provide an overview of how it works.
Triggers
Everything is triggered using a big onEdit(e) function that runs every time a user changes a value in the sheet.
Because it runs so often, we want it to be over really quickly to avoid performance issues.
For this reason, the edit event only continues to more intensive code when the edited cell was:
- within the limits of the game area,
- the 'New Game' checkbox, or
- the game level dropdown
When a new game is started an emoji set of the appropriate size is randomly pulled from the 50 available. This means every game of the same size isn't guaranteed to use the same emojis.
These pairs are then shuffled and assigned to a two-dimensional, board-sized array:
//2D Board Array
const board = [
["🧬", "🍌", "🐒", "❤️"],
["🍕", "👊", "🧬", "🤡"],
["👊", "🍕", "🤡", "❤️"],
["🍌", "🍎", "🐒", "🍎"]
];
Script Memory
To get the game to have memory of where everything is, this initial board needs to persist throughout the game.
You could use the CacheService or PropertiesService to achieve this natively, but this would require authorization. For games, I like to avoid this.
As such, I instead took advantage of the Utilities.base64Encode() method to obfuscate the board array before storing it in the sheet:
//Stringify the board placement array
const jsonBoard = JSON.stringify(boardPlacement);
//Encode and place the string in the sheet
const b64eBoard = Utilities.base64Encode(jsonBoard, Utilities.Charset.UTF_8);
sheet.getRange(boardSecretA1)
.setValue(b64eBoard)
.setFontColor('#ffffff')
.setBackground('#ffffff');
The same thing happens on the first click of a turn: the revealed emoji and its position on the board is encoded and stored in the sheet.
The cells where this data is located have a white background and white text which means that, without knowing they exist, a user is unlikely to find them.
If they do, what they find is nonsense:
//2D Board Array
const decodedBoard = [
["🧬", "🍌", "🐒", "❤️"],
["🍕", "👊", "🧬", "🤡"],
["👊", "🍕", "🤡", "❤️"],
["🍌", "🍎", "🐒", "🍎"]
];
//Encoded version
const encodedBoard = "W1si8J+nrCIsIvCfjYwiLCLwn5CSIiwi4p2k77iPIl0sWyLwn42VIiwi8J+RiiIsIvCfp6wiLCLwn6ShIl0sWyLwn5GKIiwi8J+NlSIsIvCfpKEiLCLinaTvuI8iXSxbIvCfjYwiLCLwn42OIiwi8J+QkiIsIvCfjY4iXV0=";
Here's the flow of the script.
When a user clicks a checkbox:
- If the checkbox is within the board the code continues:
- The obfuscated board is decoded
- The coordinates of the click are used to reveal the relevant emoji to the user
- If there isn't a previous emoji:
- This is the first half of a turn and the emoji and its location are encoded and inserted into the sheet for the second half of the turn
- If there is a previous emoji:
- This is the second half of a turn, the previous emoji is decoded, shown to the user, a turn is added to the count, compared to the current emoji, and:
- If there is a match the checkboxes are cleared and the code checks if the game is one
- If this isn't a match the checkboxes are reinstated
- This is the second half of a turn, the previous emoji is decoded, shown to the user, a turn is added to the count, compared to the current emoji, and:
Have fun with it!
FREE RESOURCE
Google Sheets Cheat Sheet
12 exclusive tips to make user-friendly sheets from today:
You'll get updates from me with an easy-to-find "unsubscribe" link.