5-2=3, mais [2à5]= 4 cases

This commit is contained in:
gnieark 2015-12-15 22:40:46 +01:00
parent d34d9f90f2
commit eb2f475b59
2 changed files with 20 additions and 21 deletions

View File

@ -68,25 +68,24 @@ switch($_POST['act']){
$shipsCoords=array(); $shipsCoords=array();
//pour toutes les tailles de bateau //pour toutes les tailles de bateau
for($shipWidth = 6; $shipWidth > 0; $shipWidth--){ for($shipWidth = 6; $shipWidth > 0; $shipWidth--){pl
//nombre de bateau à placer de cette taille //nombre de bateau à placer de cette taille
$dynVar='ship'.$shipWidth; $dynVar='ship'.$shipWidth;
$shipCount=$$dynVar; // #trollface $shipCount=$$dynVar; // #trollface
for( $sh = 0; $sh < $shipCount; $sh++){ for( $sh = 0; $sh < $shipCount; $sh++){ //loop for all boats witch size is $shipWidth
$directions=array(); $directions=array();
while( count($directions) == 0){ while( count($directions) == 0){
do{ do{
$xtest=rand(0,$width -1); $xtest=rand(0,$width -1);
$ytest=rand(0,$height -1); $ytest=rand(0,$height -1);
}while($map[$ytest][$xtest] == 1); }while($map[$ytest][$xtest] == 1);
//Y a t'il la place pour le bateau vers le haut? //Y a t'il la place pour le bateau vers le haut?
if($ytest < $shipWidth){ if($ytest < $shipWidth -1){
$top=false; $top=false;
}else{ }else{
$top=true; $top=true;
for($i = $ytest; $i > $ytest - $shipWidth; $i--){ for($i = $ytest; $i > $ytest - $shipWidth + 1; $i--){
if($map[$i][$xtest] == 1){ if($map[$i][$xtest] == 1){
$top=false; $top=false;
break; break;
@ -95,11 +94,11 @@ switch($_POST['act']){
} }
//vers le bas //vers le bas
if($ytest + $shipWidth > $height){ if($ytest + $shipWidth -1 > $height){
$bottom=false; $bottom=false;
}else{ }else{
$bottom=true; $bottom=true;
for($y=$ytest; $i < $ytest + $shipWidth; $i++){ for($y=$ytest; $i < $ytest + $shipWidth -1; $i++){
if($map[$i][$xtest] == 1){ if($map[$i][$xtest] == 1){
$bottom=false; $bottom=false;
break; break;
@ -108,11 +107,11 @@ switch($_POST['act']){
} }
//droite //droite
if($xtest + $shipWidth > $width){ if($xtest + $shipWidth -1 > $width){
$right=false; $right=false;
}else{ }else{
$right=true; $right=true;
for($i = $xtest; $i < $xtest + $shipWidth; $i++){ for($i = $xtest; $i < $xtest + $shipWidth -1; $i++){
if($map[$ytest][$i] == 1){ if($map[$ytest][$i] == 1){
$right= false; $right= false;
break; break;
@ -121,11 +120,11 @@ switch($_POST['act']){
} }
//gauche //gauche
if($xtest < $shipWidth){ if($xtest < $shipWidth +1){
$left=false; $left=false;
}else{ }else{
$left=true; $left=true;
for($i = $xtest; $i > $xtest - $shipWidth; $i--){ for($i = $xtest; $i > $xtest - $shipWidth + 1; $i--){
if($map[$ytest][$i] == 1){ if($map[$ytest][$i] == 1){
$left= false; $left= false;
break; break;
@ -152,20 +151,20 @@ switch($_POST['act']){
shuffle($directions); shuffle($directions);
switch($directions[0]){ switch($directions[0]){
case 'top': case 'top':
$shipsCoords[]=$xtest.",".$ytest."-".$xtest.",".($ytest - $shipWidth); $shipsCoords[]=$xtest.",".$ytest."-".$xtest.",".($ytest - $shipWidth + 1);
$map= place_ship_on_map($xtest,$ytest,$xtest,$ytest - $shipWidth,$map); $map= place_ship_on_map($xtest,$ytest,$xtest,$ytest - $shipWidth + 1,$map);
break; break;
case 'bottom': case 'bottom':
$shipsCoords[]=$xtest.",".$ytest."-".$xtest.",".($ytest + $shipWidth); $shipsCoords[]=$xtest.",".$ytest."-".$xtest.",".($ytest + $shipWidth - 1);
$map= place_ship_on_map($xtest,$ytest,$xtest,$ytest + $shipWidth,$map); $map= place_ship_on_map($xtest,$ytest,$xtest,$ytest + $shipWidth -1 ,$map);
break; break;
case 'left': case 'left':
$shipsCoords[]=$xtest.",".$ytest."-".($xtest - $shipWidth).",".$ytest; $shipsCoords[]=$xtest.",".$ytest."-".($xtest - $shipWidth + 1).",".$ytest;
$map= place_ship_on_map($xtest,$ytest,$xtest - $shipWidth ,$ytest,$map); $map= place_ship_on_map($xtest,$ytest,$xtest - $shipWidth + 1 ,$ytest,$map);
break; break;
case 'right': case 'right':
$shipsCoords[]=$xtest.",".$ytest."-".($xtest + $shipWidth).",".$ytest; $shipsCoords[]=$xtest.",".$ytest."-".($xtest + $shipWidth - 1 ).",".$ytest;
$map= place_ship_on_map($xtest,$ytest,$xtest + $shipWidth ,$ytest,$map); $map= place_ship_on_map($xtest,$ytest,$xtest + $shipWidth -1 ,$ytest,$map);
break; break;
} }

View File

@ -114,9 +114,9 @@ switch ($_POST['act']){
list($xStart,$yStart)=explode(",",$startCoord); list($xStart,$yStart)=explode(",",$startCoord);
list($xEnd,$yEnd)=explode(",",$endCoord); list($xEnd,$yEnd)=explode(",",$endCoord);
if($xStart == $xEnd){ if($xStart == $xEnd){
$long=abs($yStart - $yEnd); $long=abs($yStart - $yEnd +1);
}else{ }else{
$long=abs($xStart - $xEnd); $long=abs($xStart - $xEnd +1);
} }
$nbBoatsIwant[$long]-=1; $nbBoatsIwant[$long]-=1;
$grid[$player]=place_ship_on_map($xStart,$yStart,$xEnd,$yEnd,$grid[$player]); $grid[$player]=place_ship_on_map($xStart,$yStart,$xEnd,$yEnd,$grid[$player]);