From 0eab319240c782c39b7949b2bfb8b27e6b8a7c16 Mon Sep 17 00:00:00 2001 From: Gnieark Date: Mon, 14 Dec 2015 23:12:47 +0100 Subject: [PATCH] checking boats placd --- src/arenas/Battleship/act.php | 25 ++++++++++++++--- src/arenas/Battleship/functions.php | 42 +++++++++++++++++++++++++++++ 2 files changed, 63 insertions(+), 4 deletions(-) diff --git a/src/arenas/Battleship/act.php b/src/arenas/Battleship/act.php index 51a5940..78e553d 100644 --- a/src/arenas/Battleship/act.php +++ b/src/arenas/Battleship/act.php @@ -98,8 +98,14 @@ switch ($_POST['act']){ } } - - //vérifier si'il y a le bon nombre de bateaux + //init grid + for($y = 0; $y < $height){ + for($x = 0; $x < $width){ + $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){ @@ -107,11 +113,20 @@ switch ($_POST['act']){ list($xStart,$yStart)=explode(",",$startCoord); list($xEnd,$yEnd)=explode(",",$endCoord); if($xStart == $xEnd){ - $long=abs($yStart - $yEnd); + $long=abs($yStart - $yEnd); }else{ $long=abs($xStart - $xEnd); } $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); + } + } } foreach($nbBoatsIwant as $nb){ if($nb <> 0){ @@ -122,9 +137,11 @@ switch ($_POST['act']){ save_battle('Battleship',$bot1['name'],$bot2['name'],1); } } - } + } } + $_SESSION['grids']=$grid; + echo json_encode($grid); die; die; diff --git a/src/arenas/Battleship/functions.php b/src/arenas/Battleship/functions.php index e570ee1..7f1e08e 100644 --- a/src/arenas/Battleship/functions.php +++ b/src/arenas/Battleship/functions.php @@ -52,4 +52,46 @@ function get_IA_Response($iaUrl,$postParams){ $output = curl_exec($ch); curl_close($ch); return htmlentities($output); +} +function place_ship_on_map($x1,$y1,$x2,$y2,$map){ + if (($x1 <> $x2) && ($y1 <> $y2)){ + return false; + } + + if($x1 == $x2){ + //horizontal ship + if($y1 <= $y2 ){ + $start=$y1; + $end=$y2; + }else{ + $start=$y2; + $end=$y1; + } + for($i = $start; $i <= $end; $i++){ + if($map[$i][$x1]==0){ + $map[$i][$x1]=1; + }else{ + return false; + } + } + return $map; + } + if($y1 == $y2){ + //vertical ship + if( $x1 <= $x2){ + $start=$x1; + $end=$x2; + }else{ + $start=$x2; + $end=$x1; + } + for( $i = $start; $i <= $end; $i++){ + if( $map[$y1][$i] == 0){ + $map[$y1][$i]=1; + }else{ + return false; + } + } + return $map; + } } \ No newline at end of file