Explain the core gameplay mechanics behind games like Bejeweled
The Logic of Match-3 Games
How simple grids and algorithms create addictive gameplay loops.
The Data Structure: 2D Arrays
Beneath the colorful gems and candies, a Match-3 game is essentially a spreadsheet—a grid of data. Developers use a 2D Array (a list of lists) to represent the board.
Every item has a specific address defined by its Row (Y-axis) and Column (X-axis). When you see a gem move, the computer is simply updating a value at a specific coordinate board[row][col].
Coordinate System Visualizer
Hover over the grid cells to see how the computer addresses each slot in memory.
Match Detection Algorithms
How does the game know you made a match? It doesn't "see" shapes. It runs a scanning algorithm.
Typically, after every swap, the game iterates through the board:
- Horizontal Scan: Look at a gem, then check the one to its right. Are they the same type? If yes, check the next one.
- Vertical Scan: Look at a gem, then check the one below.
- Collection: If the count of identical neighbors is 3 or more, mark them for deletion.
Algorithm Scanner
Click "Run Scan" to visualize how the computer iterates through the grid looking for matches.
Gravity and Filling
When matches are removed, they leave empty memory slots (null). The "gravity" mechanic isn't physics simulation—it's column management.
The algorithm typically works like this:
- Iterate Bottom-Up: For every column, start at the bottom row.
- Find Holes: If a slot is empty, look up to find the nearest block.
- Shift Down: Move that block to the empty slot.
- Spawn: If you reach the top and there are no blocks left, generate a new random one.
Gravity Simulator
Create holes in the board, then apply gravity to see the logic in action.
The Complete Loop (Playable Demo)
When we combine the Grid, Swapping, Matching, and Gravity, we get the full Game Loop.
The logic flows like a state machine:
- Idle: Waiting for input.
- Swap: Player moves piece.
- Check: Is the move valid? (If no, reverse swap).
- Resolve: Find matches -> Remove -> Drop -> Fill.
- Re-Check: Did the falling pieces create new matches? (Cascades).
Match-3 Playground
A fully functional engine. I've slowed down the animations so you can see the distinct phases of the code execution.
Sign in to ask follow ups