stupid IA place boats

This commit is contained in:
Gnieark 2015-12-14 16:46:27 +01:00
parent 5fcecd8f27
commit f2236e0a28

View File

@ -1,5 +1,37 @@
<?php <?php
function place_ship_on_map($x1,$y1,$x2,$y2,$map){ 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++){
$map[$i][$x1]=1;
}
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++){
$map[$y1][$i]=1;
}
return $map;
}
} }
switch($_POST['act']){ switch($_POST['act']){
@ -80,7 +112,7 @@ switch($_POST['act']){
}else{ }else{
$right=true; $right=true;
for($i=$xtest; $i < $xtest + $shipWidth, $i++){ for($i=$xtest; $i < $xtest + $shipWidth, $i++){
if($map[$ytest][$y] == 1){ if($map[$ytest][$i] == 1){
$right= false; $right= false;
break; break;
} }
@ -93,7 +125,7 @@ switch($_POST['act']){
}else{ }else{
$left=true; $left=true;
for($i = $xtest; $i > $xtest - $shipWidth; $i--){ for($i = $xtest; $i > $xtest - $shipWidth; $i--){
if($map[$ytest][$y] == 1){ if($map[$ytest][$i] == 1){
$left= false; $left= false;
break; break;
} }
@ -119,14 +151,20 @@ switch($_POST['act']){
shuffle($directions); shuffle($directions);
switch($directions[0]){ switch($directions[0]){
case 'top': case 'top':
$shipsCoords[]=$xtest.",".$ytest."-".$ytest.",".$ytest - $shipWidth; $shipsCoords[]=$xtest.",".$ytest."-".$xtest.",".($ytest - $shipWidth);
$map= place_ship_on_map($xtest,$ytest,$xtest,$ytest - $shipWidth,$map);
break; break;
case 'bottom': case 'bottom':
$shipsCoords[]=$xtest.",".$ytest."-".$xtest.",".($ytest + $shipWidth);
$map= place_ship_on_map($xtest,$ytest,$xtest,$ytest + $shipWidth,$map);
break; break;
case 'left': case 'left':
$shipsCoords[]=$xtest.",".$ytest."-".($xtest - $shipWidth).",".$ytest;
$map= place_ship_on_map($xtest,$ytest,$xtest - $shipWidth ,$ytest,$map);
break; break;
case 'right': case 'right':
$shipsCoords[]=$xtest.",".$ytest."-".($xtest + $shipWidth).",".$ytest;
$map= place_ship_on_map($xtest,$ytest,$xtest + $shipWidth ,$ytest,$map);
break; break;
} }
@ -134,7 +172,7 @@ switch($_POST['act']){
} }
} }
print_r($shipsCoords);
break; break;
default: default:
break; break;