I am trying to create a Flood-It style game and am having problems with the main algorithm.
The algorithm checks every square that you already control, and finds adjacent squares with a selected color that you don't control.
Variables
1 - $ board: 2-dimensional array that contains the state of the board
- The squares you control in the array are = 0
- The squares you do not control vary from 1, 6 and represent different colors.
2 - $ color: user selected color
3 - $ size: the square size of the playing field
4 - $ rkey / $ ckey: row and column in two-dimensional array
The main problem is that it works for the first few squares in the starting corner, maybe 2 or 3, and then stops managing new squares.
Here is an example of the game I'm trying to make: http://floodit.appspot.com/
function checkRecursive($rkey, $ckey)
{
global $board, $size, $color;
if ($board[$rkey][$ckey] == 0)
{
if ($rkey < $size-1 && $board[$rkey + 1][$ckey] == $color)
{
$board[$rkey + 1][$ckey] = 0;
checkRecursive($rkey + 1, $ckey);
}
if ($ckey < $size-1 && $board[$rkey][$ckey + 1] == $color)
{
$board[$rkey][$ckey + 1] = 0;
checkRecursive($rkey, $ckey + 1);
}
if ($rkey > 0 && $board[$rkey - 1][$ckey] == $color)
{
$board[$rkey - 1][$ckey] = 0;
checkRecursive($rkey - 1, $ckey);
}
if ($ckey > 0 && $board[$rkey][$ckey - 1] == $color)
{
$board[$rkey][$ckey - 1] = 0;
checkRecursive($rkey, $ckey - 1);
}
}
}
source
share