Match 3 Engine

Hi Everyone,
Just curious if anyone has any code snips that might help. I am looking at making a match 3 type engine for a game. My wife loves those types of games and I thought it would be awesome to create one for her for her upcoming birthday.

Anyone got any ideas where to start? I already thought of using the physics engine to fill the screen with blocks. I guess the biggest hurdle is going to be getting the blocks to drop random but not so much as to not being able to match 3 up.

Also movement and collison detection of 3 same blocks will be a hurdle. From there it should be pretty easy. I just need a little guidance or sample code to look at.

Anyone got any suggestions? I appreciate any help you guys can provide. Thanks.

Joe… [import]uid: 12765 topic_id: 4619 reply_id: 304619[/import]

Personally i would not use the physics engine. You do not need it.

You really only need a two-dimensional table to hold the tile info, and you can use two for loops to read the data.

Your table would look like this:

myTiles = {{0,0,0,0,0,0,0},  
 {0,0,0,0,0,0,0},  
 {0,0,0,0,0,0,0},  
 {0,0,0,0,0,0,0},  
 {0,0,0,0,0,0,0},  
 {0,0,0,0,0,0,0},  
 {0,0,0,0,0,0,0}}  

That represents your grid of tiles. Generally a match-3 game relies on colors, so you could use an integer to hold color values. i.e: red = 1, blue = 2, yellow = 3, and so forth.

The functions to build the grid and to check the grid for matches would utilize a nested for loop, like this:

local function buildGrid()  
  
 for i = 1, #myTiles, 1 do  
 for j = 1, #myTiles[i], 1 do  
 local randomColor = math.random(1,7)  
 local myTile = display.newImage("images/block"..randomColor..".png")  
 myTile.myColor = randomColor  
 myTile.myName = "t\_"..i.."\_"..j  
 myTile.x = (j - 1) \* tileWidth -- set tileWidth at the top of the file  
 myTile.y = (i - 1) \* tileHeight -- set tileHeight at the top of the file  
 myTile:addEventListener("touch", touchTile)  
 myTiles[i][j] = randomColor  
 end  
 end  
  
end  

That is very basic code to lay out a grid of randomly colored tiles. The nested for loop will “walk” over each block in the grid. In this setup, the “i” loop represents the rows, and the “j” loop represents the columns of the grid.

The above code will lay out the grid. You will need to devise similar functions to check for matches, and drop new tiles.

A match-3 game needs these functions:

  1. Build the map
  2. Player swaps tile
  3. Check for matches
  4. Delete matches (or better yet, move them to the top)
  5. Drop the tiles
  6. Repeat step 3, if no matches found go to step 7.
  7. Return control to the player

Hope this gets you started!
Joe
[import]uid: 8444 topic_id: 4619 reply_id: 14592[/import]

Thanks for the insight. I’ll definately give it a go tonight when I get home from work. I’ll let you know what I come up with. Thanks :slight_smile:

Joe… [import]uid: 12765 topic_id: 4619 reply_id: 14593[/import]

do you have flash? I followed this thread… http://www.kongregate.com/forums/4-programming/topics/81112-match-3-type-game-with-box2d-trouble-with-the-match-3

…and built a prototype in flash originally. then when I got corona it wasn’t difficult at all really to port the code across, since you’re basically matching it function for function. (collision begin, collision end etc)

j

[import]uid: 6645 topic_id: 4619 reply_id: 14610[/import]