I am trying to create a "match 3" game. I'm stuck figuring out a good algorithm to fill up the game board.

My game's board is 8x8.


A few things:

The board should always be solvable. for example, the user has a score to reach by making matches. All boards must be able to produce enough available matches for the user to win each level or solve it.

  • \$\begingroup\$ I removed the part of the question about handling the shuffling of the pieces when a player removes a match, because it's a separate question. However, I think you need to provide a lot more detail to make it clear what you're asking. In addition to what Byte56 asked, it would be important to know if you want the board to always be solvable (that is, always reducible to empty if the player makes the "correct" moves) or not. \$\endgroup\$ – user1430 Dec 9 '13 at 16:55
  • \$\begingroup\$ Hi Guys, I've updated the question to include more details about the question. \$\endgroup\$ – coder_For_Life22 Dec 9 '13 at 21:16

One of the easiest ways is to generate the board solved. Generate a board that is all matched pairs, then run it through an algorithm that shuffles it about until some criteria is met (Like no matching triplets left on the board.)

There will always be a solution for a board made this way that can be solved to completion if you control the number/intensity of the shuffling.

Edit: I wrote this thinking it was a match two for some reason. With a match three it is a bit more difficult, and it depends on the way the pieces move after being removed, and if any new pieces are added. With a match 3 it may be more realistic to start with three blocks matched, then add three more matched that possibly break up the first three, and loop and repeat until you have a full board.

Basically play the game in reverse as a function and it should generate solvable boards.

  • \$\begingroup\$ Wow...This way seems complex. Could you give me some pseudocode if possible? \$\endgroup\$ – coder_For_Life22 Dec 10 '13 at 1:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.