$squares = array(); $squares[$fx . 'x' . $fy] = (string) $minefield[$fx][$fy]; if ($minefield[$fx][$fy] === 0) { $check = array(); $check[] = $fx . 'x' . $fy; while (true) { foreach ($check as $k => $v) { list($_x, $_y) = explode('x', $v); unset($check[$k]); if ($minefield[$_x - 1][$_y - 1] === 0 && !isset($squares[($_x - 1) . 'x' . ($_y - 1)])) { $check[] = ($_x - 1) . 'x' . ($_y - 1); $squares[($_x - 1) . 'x' . ($_y - 1)] = 0; } if ($minefield[$_x][$_y - 1] === 0 && !isset($squares[($_x) . 'x' . ($_y - 1)])) { $check[] = ($_x) . 'x' . ($_y - 1); $squares[($_x) . 'x' . ($_y - 1)] = 0; } if ($minefield[$_x + 1][$_y - 1] === 0 && !isset($squares[($_x + 1) . 'x' . ($_y - 1)])) { $check[] = ($_x + 1) . 'x' . ($_y - 1); $squares[($_x + 1) . 'x' . ($_y - 1)] = 0; } if ($minefield[$_x - 1][$_y] === 0 && !isset($squares[($_x - 1) . 'x' . ($_y)])) { $check[] = ($_x - 1) . 'x' . ($_y); $squares[($_x - 1) . 'x' . ($_y)] = 0; } if ($minefield[$_x + 1][$_y] === 0 && !isset($squares[($_x + 1) . 'x' . ($_y)])) { $check[] = ($_x + 1) . 'x' . ($_y); $squares[($_x + 1) . 'x' . ($_y)] = 0; } if ($minefield[$_x - 1][$_y + 1] === 0 && !isset($squares[($_x - 1) . 'x' . ($_y + 1)])) { $check[] = ($_x - 1) . 'x' . ($_y + 1); $squares[($_x - 1) . 'x' . ($_y + 1)] = 0; } if ($minefield[$_x][$_y + 1] === 0 && !isset($squares[($_x) . 'x' . ($_y + 1)])) { $check[] = ($_x) . 'x' . ($_y + 1); $squares[($_x) . 'x' . ($_y + 1)] = 0; } if ($minefield[$_x + 1][$_y + 1] === 0 && !isset($squares[($_x + 1) . 'x' . ($_y + 1)])) { $check[] = ($_x + 1) . 'x' . ($_y + 1); $squares[($_x + 1) . 'x' . ($_y + 1)] = 0; } } if (count($check) == 0) break; } foreach ($squares as $key => $value) { list($_x, $_y) = explode('x', $key); $squares[($_x - 1) . 'x' . ($_y - 1)] = $minefield[$_x - 1][$_y - 1]; $squares[($_x) . 'x' . ($_y - 1)] = $minefield[$_x][$_y - 1]; $squares[($_x + 1) . 'x' . ($_y - 1)] = $minefield[$_x + 1][$_y - 1]; $squares[($_x - 1) . 'x' . ($_y)] = $minefield[$_x - 1][$_y]; $squares[($_x + 1) . 'x' . ($_y)] = $minefield[$_x + 1][$_y]; $squares[($_x - 1) . 'x' . ($_y + 1)] = $minefield[$_x - 1][$_y + 1]; $squares[($_x) . 'x' . ($_y + 1)] = $minefield[$_x][$_y + 1]; $squares[($_x + 1) . 'x' . ($_y + 1)] = $minefield[$_x + 1][$_y + 1]; } }