For the flutter hackathon 2022, I build a slide puzzle and created some first levels using pen and paper. Right now, the submissions review is in progress so I won’t touch the app for another month, but I already look for the next steps. And that is: I need more levels!

The problem

Challenging, fun, engaging, and numerous levels are the heart of every puzzle game. I won’t be able to create them all by hand, for obvious reasons (that is time and skill). But this is all digital, I just have to come up with an algorithm to do the work for me.

My puzzle feature a square grid separated into fields. On these fields are tiles that can be dragged, until the ruby hits the goal. Although this is my very specific implementation, I will try to keep this post as generic as possible.

Back to the problem: I want to generate levels of adjustable size, difficulty, and the user must be able to solve them.

Generate Levels

I split my problem into two parts. The first one is to create an iteration of levels, using my games rules. They do not need to be valid puzzles at this point.

For convenience, I just added another button to my home menu to open a debug only page:

kDebugMode ? Container(
  padding: const EdgeInsets.all(10.0),
  child: Button(
    onPressed: () => generateLevels(),
    child: const Text('Generate Puzzles')
) : const SizedBox()

By now I enter my parameters by hand but I clearly see the possibilities to configure the generated levels with inputs.

Generator rules

To generate levels, even before trying to solve them, I can come up with some rules that I must follow, to create a valid level. In my case, that is a ruby and a goal. And the ruby should not start on the goal field. And there has to be at least one empty space to move the tiles.

My generator uses the random function to decide for each field, what type of tile should be placed here (that is empty, blocker, diamond or some special tiles). With this and a newly created function to create thumbnails, I am able to generate levels of a given dimension. Yay.

As a next step, I insert more complicated tiles. They take up more space and have a smaller likelihood to appear.

My generators result with parameters to create 10 levels with dimension 4.

Next step

Now that I can generate random levels, I need to solve them programmatically to secure that my result is a playable level. And that’s for a follow-up post.

My Flutter Flight – Generate some Puzzle Levels – Part 1

Post navigation

Leave a Reply

Your email address will not be published.