Здравствуйте! Пишу код игры сапёр, но в функции проверки почему-то получается вечный рекурсивный цикл. Функция Calc_OpenFreeFields возвращает объект $innerArray но с открытыми клетками после нажатия. $type может быть left - открытие ячейки, right - флажок $innerArray и $bombArray - объекты вида Код | [0]=> [0]=> [1]=> [2]=>b [3]=> [4]=> [1]=> [0]=> [1]=> [2]=> [3]=> [4]=>b и т.п.
|
Буква b означает, что в ячейке бомба ($bombArray) Буква f означает, что в ячейке флажок, n? означает, что в ячейке цифра ? ($innerArray) Код | $bomb = 0;
function Calc_OpenFreeFields($innerArray,$bombArray,$x,$y,$type,$callback = false){ global $bomb;
$x = (int)$x; $y = (int)$y;
if($type == 'left'){ //1 if($innerArray->$x->$y == 'd') return $innerArray;
//2 $innerArray->$x->$y = 'd';
//3 $bomb = 0; $chk_x = $x-1; $chk_y = $y; if(($bombArray -> $chk_x -> $chk_y) == 'b'){ $bomb++; } $chk_x = $x-1; $chk_y = $y-1; if(($bombArray -> $chk_x -> $chk_y) == 'b'){ $bomb++; } $chk_x = $x; $chk_y = $y-1; if(($bombArray -> $chk_x -> $chk_y) == 'b'){ $bomb++; } $chk_x = $x+1; $chk_y = $y; if(($bombArray -> $chk_x -> $chk_y) == 'b'){ $bomb++; } $chk_x = $x; $chk_y = $y+1; if(($bombArray -> $chk_x -> $chk_y) == 'b'){ $bomb++; } $chk_x = $x+1; $chk_y = $y+1; if(($bombArray -> $chk_x -> $chk_y) == 'b'){ $bomb++; } $chk_x = $x-1; $chk_y = $y+1; if(($bombArray -> $chk_x -> $chk_y) == 'b'){ $bomb++; } $chk_x = $x+1; $chk_y = $y-1; if(($bombArray -> $chk_x -> $chk_y) == 'b'){ $bomb++; }
if($bomb > 0){ if($callback != true) $innerArray->$x->$y = ('n'.(string)$bomb); }else{ $chk_x = $x-1; $chk_y = $y; if(($innerArray -> $chk_x -> $chk_y) == ''){ $innerArray = Calc_OpenFreeFields($innerArray,$bombArray,$chk_x,$chk_y,$type,true); if($bomb > 0){ $bomb = 0; return $innerArray; } } $chk_x = $x-1; $chk_y = $y-1; if(($innerArray -> $chk_x -> $chk_y) == ''){ $innerArray = Calc_OpenFreeFields($innerArray,$bombArray,$chk_x,$chk_y,$type,true); if($bomb > 0){ $bomb = 0; return $innerArray; } } $chk_x = $x; $chk_y = $y-1; if(($innerArray -> $chk_x -> $chk_y) == ''){ $innerArray = Calc_OpenFreeFields($innerArray,$bombArray,$chk_x,$chk_y,$type,true); if($bomb > 0){ $bomb = 0; return $innerArray; } } $chk_x = $x+1; $chk_y = $y; if(($innerArray -> $chk_x -> $chk_y) == ''){ $innerArray = Calc_OpenFreeFields($innerArray,$bombArray,$chk_x,$chk_y,$type,true); if($bomb > 0){ $bomb = 0; return $innerArray; } } $chk_x = $x; $chk_y = $y+1; if(($innerArray -> $chk_x -> $chk_y) == ''){ $innerArray = Calc_OpenFreeFields($innerArray,$bombArray,$chk_x,$chk_y,$type,true); if($bomb > 0){ $bomb = 0; return $innerArray; } } $chk_x = $x+1; $chk_y = $y+1; if(($innerArray -> $chk_x -> $chk_y) == ''){ $innerArray = Calc_OpenFreeFields($innerArray,$bombArray,$chk_x,$chk_y,$type,true); if($bomb > 0){ $bomb = 0; return $innerArray; } } $chk_x = $x-1; $chk_y = $y+1; if(($innerArray -> $chk_x -> $chk_y) == ''){ $innerArray = Calc_OpenFreeFields($innerArray,$bombArray,$chk_x,$chk_y,$type,true); if($bomb > 0){ $bomb = 0; return $innerArray; } } $chk_x = $x+1; $chk_y = $y-1; if(($innerArray -> $chk_x -> $chk_y) == ''){ $innerArray = Calc_OpenFreeFields($innerArray,$bombArray,$chk_x,$chk_y,$type,true); if($bomb > 0){ $bomb = 0; return $innerArray; } } }
//\3
}else if($type=='right'){ $innerArray->$x->$y = ($innerArray->$x->$y=='f'?'':'f'); }
if($callback != true) $bomb = 0;
return $innerArray;
}
|
Никак не пойму, в чём проблема?
|