How would you detect soft locks in a tile sliding game?

69 Views Asked by At

I have created a tile sliding game in python (using pygame). It's a 3x3, and an online example can be found here.

When my and my friends started testing this game out, comparing it to the online example, we found a few examples where the game got 'soft locked', a point it cannot be solved. We tried to recreate the situation in the online version and found it impossible.

Quick rundown of how I made the game using python:

  • [1, 2, 3, 4, 5, 6, 7, 8, 0], 0 representing the space.
  • Randomly generates/scrambles a list
  • Click on a number, assuming its adjacent to 0, is switched with it
  • 'Adjacent' meaning: (clicked number) + 1, -1, +3, -3, = 0
  • When the game reaches the list [1, 2, 3, 4, 5, 6, 7, 8, 0], the player wins.

Now my question involves the soft locks. There has to be some math behind it, sequences that can never be solved with this method. Although there are some ways I can bypass the soft locks, I'm more interested in the math side of this.

So far I don't have much. If anyone needs the python file I can post it. If I can clarify anything please tell me. Thank you.

Also, here I'm assuming there are cases where it is unsolvable. If that's not true, then sorry about this.