I have already implemented a functionallity, when keeper automatically moves using breadth first search algorithm. Now I want it to move boxes automatically as well (if keeper can move box from source to destination without moving another boxes). How to I do it? I’ve tried modifying BFS, not haven’t yet succeed.
UPDATE: I don’t need to solve the puzzle. Instead I want to develop handy user-interface, when user can move boxes with their mouse. For this I need some algo, which would allow to compute move sequence. But it’s only about moving single box and if only no other boxes should be moved in order to do so.
Use breadth-first search as before (or A* if you prefer), but search the appropriate set of states.
When you are searching for a path for the keeper, the states correspond to the squares in the grid. But when you are searching for a way for the keeper to move a block, the states correspond to pairs of squares in the grid (one for the keeper, one for the block).
Here’s the smallest non-trivial example. Suppose we have a Sokoban level with squares labelled as follows:
The grid contains the keeper and one block. The state space consists of pairs of squares occupied by the keeper and the block. There are 56 such states, drawn as small circles in the diagram below.
The lines show possible transitions within this state space. The thin lines correspond to moves by the keeper (and are bidirectional). The heavy lines correspond to pushing the block (hence go in one direction only). It is this state space that you need to search.
For example, if the block starts at 7 and the keeper at 8, then the keeper can push the block to 8 by following the red path in the state space:
Note that along this path, the block goes through the positions 7–6–5–6–7–8. You couldn’t have found this path by just considering positions for the block, as the block passes through positions 6 and 7 twice.