Welcome to 😺 Emoji Swap!
Demo Play the game!
GitHub A link to the project source
Discussion and Algorithm
Emoji Rendering
I thought of using emojis due to their popularity, fun, and wide-spread compatibility. The first trick was to get the emoji to render on a web page based on its code.
Starting with the Full Emoji List, the first emoji listed is 1F600
which is the 😀 or “grinning face”. After some work, I discovered that this is how to render an emoji using Angular:
<div [innerHTML]="😀"></div>
Game Board
The next bit of fun was to draw the game board or grid of emojis. I initially thought to use CSS Grid, but decided that most breakpoints where going to be similar so good ole Flexbox did the trick!
Basic algorithm:
- Start with a 2-D array in TypeScript ` grid: GameTile[][];`
- Loop through each dimension’s limit (row and column)
- Add a
GameTile
from a random list of emojis - See: src\app\game\services\game.service.ts:
CreateGame(
Finding Matches and Potential Matches
For the game to function, I needed a mechanism that would quickly tell me if there existed a match on the board. This mechanism would then be ready for any scenario:
- after the game board was established
- new game instance
- level change
- reshuffle
- after player interaction
- after a previously found match was removed and new tiles added This would equip the game with the ability to react to any of the above events.
Matching Algorithm:
For each tile…
- for each cardinal direction…
- is this tile within the grid?
- does this tile have the same emoji code?
- add tile to candidate list
- recurse until the next tile doesn’t match
- is candidate list length within threshold? (note: this allows for any length match!)
If the candidate list happens to be 1 less then the threshold…
- perform potential match
-
from the previous tile, look in diagonal directions for potential match
- extended potential match
- from the previous tile, in the same direction
- skip next tile
- check next tile
There is another category of potential matches - staggered matches. These are searched during the initial find algorithm as it must be acted upon each tile. For each tile…
- search in all cardinal directions
- if tile is within grid, add to list
- after all directions have been searched
- start with the first tile in list
- filter remaining tiles that match list
- if length of filtered list meets the threshold, potential match found!