Flood-It Game Recursive Function Algorithm in PHP

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);
        }
    }
}
+3
source share
1 answer

Reading the code, it seems that if you always start with (0,0), then as soon as the squares immediately adjacent to this cell are under the control of the player, further checks will not be passed to them.

You will go:

  • start with (0,0)
  • check (0,1): already set to 0, do nothing.
  • check (1,0): already set to 0, do nothing.

I think maybe you need to remove the concept of "control" squares. All you want to do is repeatedly execute a fill filter with different colors.

, - , $oldcolor - , , $newcolor - , :

function checkRecursive($rkey, $ckey)
{
    global $board, $size, $oldcolor, $newcolor;
    if ($board[$rkey][$ckey] == $oldcolor)
    {
        $board[$rkey][$ckey] = $newcolor
        if ($rkey < $size-1 && $board[$rkey + 1][$ckey] == $oldcolor)
        {            
            checkRecursive($rkey + 1, $ckey);
        }
        if ($ckey < $size-1 && $board[$rkey][$ckey + 1] == $oldcolor)
        {
            checkRecursive($rkey, $ckey + 1);
        }
        if ($rkey > 0 && $board[$rkey - 1][$ckey] == $oldcolor)
        {
            checkRecursive($rkey - 1, $ckey);
        }
        if ($ckey > 0 && $board[$rkey][$ckey - 1] == $oldcolor)
        {
            checkRecursive($rkey, $ckey - 1);
        }
    }
}
+1

Source: https://habr.com/ru/post/1787853/


All Articles