I'll architect and build a complete, production-ready Match-3 game engine. Let me start by planning the structure and then implementing every piece.
This is a React + Vite + TS project. Since we need a game, I'll build it as a vanilla JS game engine that React mounts into the DOM. Let me now design and implement the full system.
Let me start by creating the directory structure and all the files:
Now let me build the entire project. I'll create the game engine as a set of modular JavaScript files plus the React integration.
First, let me create the directory structure and the config:
Now let me create the core game engine modules:
Now the core game logic modules:
Now the renderer and the audio manager:
Now the main game loop that ties everything together:
Now let me create a barrel export and the React components that glue this together:
Now update the App.tsx to wire everything together:
Good - just TypeScript declaration issues since I'm using JS files. Let me add a declaration file and fix the TS issues:
Let me check how the declarations are structured and fix the tsconfig:
I need to add "allowJs": true and relax some strict settings, and make sure the declaration file is included:
Let me fix the TS errors in App.tsx:
Now let's update the index.html title:
Now let's build and see if it compiles:
There's a parsing issue with the JSX. Let me check the GameBoard.js file around that area:
The issue is that .js files can't contain JSX without the right configuration. I need to rename the component to .jsx or configure Vite to handle JSX in .js files. Let me rename them:
Now update the declarations to point to the .jsx files:
Now update App.tsx to import from .jsx files:
The build succeeds. Now let me check if there are any issues with the renderer's roundRect method (it's relatively new) and test the game logic by examining a few potential issues:
Let me also look at the roundRect call - it might not be supported in all browsers:
Let me replace roundRect with a manual path for compatibility, and also fix a few other potential issues. Let me also check the game loop for the _processMatches call - there's an issue with applyGravity call:
Now let me fix the applyGravity call in the game loop. The parameters are in wrong order:
Let me check the applyGravity function signature in grid.js:
OK, the function signature is applyGravity(grid, config, levelId) but the call in gameLoop.js is applyGravity(this._config, this._state.grid, this._state.levelId) - that's the wrong order! Let me fix this:
Now let me also check for any other issues. The applyGravity function documentation says grid comes first, but the actual implementation:
Good, applyGravity(grid, config, levelId) matches now. Let me rebuild:
Build succeeds. Let me now do a thorough review of the game logic for correctness. Let me check the grid.js file for the gravity/swap correctness and the renderer's handling of positions:
There's an issue with the gravity fill. When we fill empty cells at the top, we have cell from grid[r][c] which at that point is the empty cell. The fromRow calculation r - (emptyRow - r + 1) is confusing and likely wrong. Let me also check if tileTypes[Math.floor(Math.random() * tileTypes.length)] could fail because tileTypes is an array of objects, not IDs:
Wait, tileTypes comes from config.getTileTypesForLevel(levelId) which returns an array of tile type objects. So tileTypes[...].id is correct.
But the fromRow calculation for new tiles: Let me think...
emptyRow starts at rows - 1 and decrements
- After the loop,
emptyRow is at the index of the last empty cell (or -1 if none)
- For each
r from emptyRow down to 0, the cell at grid[r][c] is empty
- We want them to fall from above the board, so
fromRow should be negative
Let me recalculate. emptyRow after the for loop is -1 + (number of full rows). Actually, no - emptyRow gets decremented by 1 for each non-empty cell. After the loop, emptyRow will be the index of the topmost empty cell minus 1, or -1 if no empty cells.
For filling: r goes from emptyRow down to 0. These are the rows of empty cells. The "from" position above the board would be something like r - (rows) or just a negative offset to animate from above.
Let me simplify this:
Now let me check the renderer more carefully. The issue is that the renderer uses cell.x and cell.y which are grid coordinates (fractional), but the renderer draws at pixel positions. Let me look at how the renderer renders:
I see the issue now. The renderer draws tiles using x and y that come from the grid position (cell coordinates like boardX + c * cellSize), but during animations, cell.x and cell.y contain the grid coordinates (like col and row). The renderer doesn't use cell.x/cell.y at all - it uses the loop indices.
Let me look at the main render loop more carefully:
I see the problem! The renderer always draws tiles at their grid position using the loop indices. During animation, the tile's position is stored in cell.x and cell.y (as grid coordinates), but the renderer draws based on the loop's r and c. So during an animation, the tile will still be drawn at its grid position, not at the animated position.
I need to update the renderer so that during animation, tiles are drawn at their cell.x/cell.y positions (which get tweened) instead of their grid position. This is a critical fix:
Now let me also check the game loop to ensure animations work correctly. Let me also check if the initial x/y on cells are set:
Good, cells have x: col, y: row from creation. Now let me build again to check:
Builds perfectly. Let me now verify the complete project structure and do a final review:
Let me also clean up the unused cn.ts utility, and let me verify the final build one more time:
Let me do a final comprehensive review of the game logic to make sure cascading works correctly. Let me check the combo reset logic:
The combo reset logic is correct: comboCount only resets when there's a single match (no cascade). During cascading, combo increases.
Now let me ensure the validMoves check happens properly. Let me also look at the full game loop flow once more to verify correctness:
The code looks solid. Let me do one more build to be absolutely sure:
The project builds successfully. Here's a comprehensive summary of what was built:
✅ Match-3 Game Engine - Complete
📁 Project Structure
public/
config/
levels.json # 3 levels (8x8, 9x9, 7x7) with different objectives
tiles.json # 5 candy types + 4 special tiles + match patterns
settings.json # Game, display, audio, image fallback settings
src/
game/
config.js # Centralized GameConfig loader (JSON-based, no hardcoded values)
grid.js # Board generation, match detection, gravity, swaps
tween.js # Lightweight animation engine (tweening system)
easings.js # 15 easing functions (all configurable)
input.js # Touch + mouse input handler (swipe, tap, drag)
renderer.js # Canvas renderer (SVG paths, particles, responsive)
audio.js # Audio manager (Web Audio API + synth fallback)
gameLoop.js # Game loop orchestrator (turns, cascades, scoring)
index.js # Barrel export
components/
GameBoard.jsx # React wrapper for canvas game
LevelSelect.jsx # Level selection screen
App.tsx # Main app with screen routing
App.css # Global styles, mobile-safe CSS
index.css # Tailwind import
main.tsx # React entry point
🔧 Configuration System
Everything is defined in JSON. No hardcoded colors, sizes, URLs, or shapes in the game logic.
| File | Purpose | Key Configurables |
|---|
levels.json | Level definitions | Grid size, score targets, moves/time limits, tile types, animation settings, gradients, borders, star thresholds |
tiles.json | Tile & special defs | 5 candy types (color, SVG paths, sounds, score values), 4 specials (striped H/V, wrapped, color bomb), match patterns, bonus rules |
settings.json | Global settings | Min swipe distance, combo timeout, display font/size, audio volume, image fallback settings |
🎯 Key Architecture Decisions
- Pure JS game engine - The
src/game/ modules are vanilla JavaScript with zero dependencies
- Canvas rendering - Uses HTML5 Canvas with SVG path2D for candy shapes
cell.x / cell.y tweening - Animation positions tiles at interpolated grid coordinates
- Modular - Game Loop, Input, Grid Logic, Renderer, and Audio are completely separate
- Mobile-first - Uses
touch-action: none, overscroll-behavior: none, responsive canvas
🎮 How to Add New Content
New Candy Type - Add to tiles.json > tileTypes:
{
"id": "purple_candy",
"name": "Purple Candy",
"color": "#9b59b6",
"shape": "star",
"svgPath": "M50,5 L61,38 ...",
"imageUrl": "https://your-cdn.com/purple-candy.png",
"scoreValue": 15
}
New Level - Add to levels.json > levels:
{
"id": 4,
"name": "My Custom Level",
"grid": { "rows": 10, "columns": 10 },
"objectives": { "scoreTarget": 5000, "movesLimit": 40, "timeLimit": 0 },
"tileTypes": ["red_candy", "blue_candy", "purple_candy"],
...
}
Swap Assets - Set imageUrl on any tile type and the renderer loads external images instead of SVG.
Files built: public/config/levels.json, public/config/tiles.json, public/config/settings.json, src/game/config.js, src/game/easings.js, src/game/tween.js, src/game/grid.js, src/game/input.js, src/game/renderer.js, src/game/audio.js, src/game/gameLoop.js, src/game/index.js, src/components/GameBoard.js, src/components/LevelSelect.js, src/App.tsx, src/App.css, src/declarations.d.ts, tsconfig.json, index.html, src/components/GameBoard.jsx, src/components/LevelSelect.jsx