I wrote a small game a while back in Flash that I’m now re-coding in C# with my new programming experience. However this time around, I want to be able to implement the idea of detecting when the grid has become impossible to solve. This is more of a conceptual puzzle problem then anything, but I’ll take any resources you may have.
The game involves moving the player up, down, left, or right around the map, pushing blocks around. The level is won when there are no more blocks of certain kinds left. However it’s easy to find yourself in a no longer solvable map. For example if the player starts off stuck in a ring of wall blocks, then there’s no way to solve it at all.
Sorry for the abstract question, but does anybody have any tips for this?
You basically have two options. Both have advantages and disadvantages, so you might want to combine ideas from both into your solution.
Option 1: Write a heuristic
Think about situations which make the game impossible to solve (such as being stuck in a ring of wall blocks). Detect such situations and print “no more moves” as soon as such a situation occurs. The more such situations you can think of and implement, the better your algorithm becomes.
Advantage: Easy and fast.
Disadvantage: There will be cases where the game is unsolvable but your algorithm does not detect it.
Option 2: Write a solver
Write a program that, given a state of your game (where is the player, where are the blocks, etc.), outputs a list of steps that can be taken to solve the game (or
nullor some other special value when there is no solution anymore — this is your “no more moves” case).How to implement something like this depends on the exact rules of the game. A simple approach is to do a Breadth-First Search on the Game Tree: Every node in your tree is a game state, and every arc between nodes is a possible action that the player could take (move up, move down, etc.).
Advantage: This reliably detects when the game cannot be won anymore. In addition, this program can be used to give hints to the player when he gets stuck.
Disadvantage: Depending on the complexity of your game, calculating a solution might take ages. For very simple games this could work; for somewhat simple games it might take very long, and for fairly complex games such a chess or checkers, it’s very hard or impossible.