0

I'm looking for some suggestions as to what's a good algorithm I could use for solving the puzzle on the attached image with Javascript, preferably if the algo is documented and could contain a JS example.

enter image description here

The white pieces can be moved one at a time, 4 directions, into the grey spots (if they fit) The game ends when the larger white piece reaches the red exit spot.

This is just for fun.

I'm making this in Javascript and I'll be using p5.js for displaying it. I'm not looking for actual code solution, this is a personal exercise for fun.

I tried it in the past, but it didn't work, I remember it spend a lot of time calculating the exit path but it wasn't able to find it after several minutes.

The puzzle shown is a very basic version to grasp the idea, but the board will be more and more complex each time, e.g. like having lots of white pieces of different sizes in larger boards.

I understand that I could try the breadth-first-search algo, nonetheless I don't know if it will be still suited if the board gets larger, e.g. 5x7 instead of 3x3, I don't know how to estimate how many permutations it could take the computer find the shortest exit path.

With BFS I understand that, from a given state of the board, I would need to explore all the possible ramifications, each one being a different branch, which could lead to millions of combinations, where each combination would explore what happens if a given is moved here instead of there.

  • Would I pick randomly the next piece to be moved? Maybe not, this might be harder to debug due to it constantly changing. Maybe I would pick the first piece that can be moved, starting from TOP LEFT.

  • How do I prevent regressions? E.g. pieces resulting in a board that was already explored in a different branch?

Thanks for your time and patience :)

Agustin Garzon
  • 317
  • 2
  • 15

1 Answers1

1

According to http://groups.csail.mit.edu/mac/users/bob/sliding-blocks.pdf sliding puzzles can often space in the world of NP-complete problems, which are usually gnarly and computationally expensive (if solvable). This explains why it was hard for you to even try to calculate the path.

Usually there is no generic approach to such problems as each one of them needs a particular tailored solution.

In your specific case though I would suggest trying a naive path finding approach where first you try to get the piece as close as possible and then, one moves are out, you try pushing pieces "away" from the potential path.

I hope this can point you in the right direction :-)

Lucat
  • 2,242
  • 1
  • 30
  • 41
  • 1
    Hey Luke, my question got closed :( I'm looking forward to post a solution / reply myself when I get to make some progress with it ^__^ – Agustin Garzon Oct 09 '20 at 14:04