I am creating a Stratego Game and am having trouble creating an algorithm that can detect when there are no more possible moves because all your pieces are gone or the remaining pieces are unmovable or trapped by forbidden squares, unmovable pieces, or other trapped pieces.
For simplicity you can think of the board as an array of Squares which can contain pieces.
Square[][] gameBoard = new Square[10][10]
Squares have easily checkable state such as hasPiece(), hasEnemyPiece(), hasUnMovablePiece(), hasMoveablePiece(), isBlocked().
It would also probably be nice if this algorithm wasn’t run every time a player moved but maybe checked in the beginning and then only certain conditions were checked when the player moved.
[ ][ ][ ][ ][ ][ ][ ][ ][ ][ ]
[ ][ ][ ][ ][ ][ ][ ][ ][ ][ ]
[B][ ][ ][ ][ ][ ][B][B][ ][ ]
[F][B][ ][ ][ ][B][3][4][B][ ]
This is an example situation towards the end of a game, you need to be able to check each of the non-movable pieces (3 & 4) to see whether they are movable[not trapped by water(blocked), other unmovable pieces(bomb or flag), or other trapped pieces].
I think something like this would work:
This simply iterates over each square and checks if it has your piece, and that it is a moveable type piece. Then it checks the surrounding spaces to see if there are enemy pieces, or a non-blocked space. If it finds one, we know that there are still moves left, so we exit.
At worst, you check every space and every neighbor (~500 checks) which is really not much at all. You could make it faster by counting as you get to each of your pieces, and if you reach the number of pieces you have left, you can exit as well.