From 5fcecd8f2759fc5f00be0e21e0910dc757a3b759 Mon Sep 17 00:00:00 2001 From: gnieark Date: Mon, 14 Dec 2015 08:04:32 +0100 Subject: [PATCH] . --- html/StupidIABattleship.php | 100 ++++++++++++++++++++++++++++++++++-- 1 file changed, 96 insertions(+), 4 deletions(-) diff --git a/html/StupidIABattleship.php b/html/StupidIABattleship.php index d3ebed1..be45296 100644 --- a/html/StupidIABattleship.php +++ b/html/StupidIABattleship.php @@ -1,4 +1,7 @@ = 0; $shipWidth--){ //nombre de bateau à placer de cette taille $shipCount=$('ship'.$shipWidth); // #trollface for( $sh = 0; $sh < $shipCount; $sh++){ - $xtest=rand($width); - - - + $directions=array(); + while( count($directions) == 0){ + + do{ + $xtest=rand(0,$width -1); + $ytest=rand(0,$height -1); + }while($map[$ytest][$xtest] == 1); + + //Y a t'il la place pour le bateau vers le haut? + if($ytest < $shipWidth){ + $top=false; + }else{ + $top=true; + for($i = $ytest; $i > $ytest - $shipWidth; $i--){ + if($map[$i][$xtest] == 1){ + $top=false; + break; + } + } + } + + //vers le bas + if($ytest + $shipWidth > $height){ + $bottom=false; + }else{ + $bottom=true; + for($y=$ytest; $i < $ytest + $shipWidth; $i++){ + if($map[$i][$xtest] == 1){ + $bottom=false; + break; + } + } + } + + //droite + if($xtest + $shipWidth > $width){ + $rigth=false; + }else{ + $right=true; + for($i=$xtest; $i < $xtest + $shipWidth, $i++){ + if($map[$ytest][$y] == 1){ + $right= false; + break; + } + } + } + + //gauche + if($xtest < $shipWidth){ + $left=false; + }else{ + $left=true; + for($i = $xtest; $i > $xtest - $shipWidth; $i--){ + if($map[$ytest][$y] == 1){ + $left= false; + break; + } + } + } + + + $directions=array(); + if($top){ + $directions[]='top'; + } + if($bottom){ + $directions[]='bottom'; + } + if($left){ + $directions[]='left'; + } + if($right){ + $directions[]='right'; + } + } + + shuffle($directions); + switch($directions[0]){ + case 'top': + $shipsCoords[]=$xtest.",".$ytest."-".$ytest.",".$ytest - $shipWidth; + + break; + case 'bottom': + break; + case 'left': + break; + case 'right': + break; + + } + } }