switch ($freeCases[0][2][0]) { case 'top': $shipsCoords[] = $x . "," . $y . "-" . $x . "," . ($y - $shipWidth + 1); $map = place_ship_on_map($x, $y, $x, $y - $shipWidth + 1, $map); break; case 'bottom': $shipsCoords[] = $x . "," . $y . "-" . $x . "," . ($y + $shipWidth - 1); $map = place_ship_on_map($x, $y, $x, $y + $shipWidth - 1, $map); break; case 'left': $shipsCoords[] = $x . "," . $y . "-" . ($x - $shipWidth + 1) . "," . $y; $map = place_ship_on_map($x, $y, $x - $shipWidth + 1, $y, $map); break; case 'right': $shipsCoords[] = $x . "," . $y . "-" . ($x + $shipWidth - 1) . "," . $y; $map = place_ship_on_map($x, $y, $x + $shipWidth - 1, $y, $map); break; } } } echo json_encode($shipsCoords); file_put_contents(__DIR__ . "/log.txt", json_encode($shipsCoords), FILE_APPEND); break; case "fight": //for debog arena file_put_contents(__DIR__ . "/log.txt", print_r($_POST, true), FILE_APPEND); echo rand(0, $_POST['width'] - 1) . "," . rand(0, $_POST['height'] - 1); die; break; default: break;
$grid[$player][$y][$x] = 0; } } //vérifier si'il y a le bon nombre de bateaux et les placer $nbBoatsIwant = array(0, $postValues['nbShip1'], $postValues['nbShip2'], $postValues['nbShip3'], $postValues['nbShip4'], $postValues['nbShip5'], $postValues['nbShip6']); foreach ($boatsPlayer as $boat) { list($startCoord, $endCoord) = explode("-", $boat); list($xStart, $yStart) = explode(",", $startCoord); list($xEnd, $yEnd) = explode(",", $endCoord); if ($xStart == $xEnd) { $long = abs($yStart - $yEnd) + 1; } else { $long = abs($xStart - $xEnd) + 1; } $nbBoatsIwant[$long] -= 1; $grid[$player] = place_ship_on_map($xStart, $yStart, $xEnd, $yEnd, $grid[$player]); if (!$grid[$player]) { echo $currentBot['name'] . " n'a pas placé correctement ses bateaux. Certains se chevauchent. Il perd."; if ($player == 1) { save_battle('Battleship', $bot1['name'], $bot2['name'], 2); } else { save_battle('Battleship', $bot1['name'], $bot2['name'], 1); } die; } //remember each cases of each boats $boatListOfCases = array(); if ($xStart == $xEnd) { if ($yStart <= $yEnd) { $start = $yStart; $end = $yEnd;