rewrite stupid IA
This commit is contained in:
parent
aaddbf7d25
commit
efb974deee
|
@ -97,7 +97,7 @@ switch($_POST['act']){
|
||||||
die;
|
die;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
a:
|
||||||
$map=array();
|
$map=array();
|
||||||
//construire une grille
|
//construire une grille
|
||||||
for($i=0; $i < $width; $i++){
|
for($i=0; $i < $width; $i++){
|
||||||
|
@ -114,93 +114,95 @@ switch($_POST['act']){
|
||||||
$dynVar='ship'.$shipWidth;
|
$dynVar='ship'.$shipWidth;
|
||||||
$shipCount=$$dynVar; // #trollface
|
$shipCount=$$dynVar; // #trollface
|
||||||
for( $sh = 0; $sh < $shipCount; $sh++){ //loop for all boats witch size is $shipWidth
|
for( $sh = 0; $sh < $shipCount; $sh++){ //loop for all boats witch size is $shipWidth
|
||||||
$directions=array();
|
//find free cases
|
||||||
while( count($directions) == 0){
|
$freeCases=array();
|
||||||
do{
|
for($y=0; $y < $height; $y++){
|
||||||
$xtest=rand(0,$width -1);
|
for($x=0; $x < $width; $x++){
|
||||||
$ytest=rand(0,$height -1);
|
if($map[$y][$x] == 0){
|
||||||
}while($map[$ytest][$xtest] == 1);
|
$directions=array();
|
||||||
|
//test top
|
||||||
//Y a t'il la place pour le bateau vers le haut?
|
$top=true;
|
||||||
|
for($i = $y; $i > $y - $shipWidth; $i--){
|
||||||
//haut
|
if((!isset($map[$i][$x])) OR ($map[$i][$x]==1)){
|
||||||
$top=true;
|
$top=false;
|
||||||
for($i = $ytest; $i >= $ytest - $shipWidth + 1; $i--){
|
$break;
|
||||||
if ((!isset($map[$i][$xtest]))
|
}
|
||||||
OR ($map[$i][$xtest] == 1)){
|
}
|
||||||
$top=false;
|
if($top){
|
||||||
break;
|
$directions[]='top';
|
||||||
}
|
}
|
||||||
}
|
//test Bottom
|
||||||
|
$bottom=true;
|
||||||
//vers le bas
|
for($i = $y; $i < $y + $shipWidth; $i++){
|
||||||
$bottom=true;
|
if(((!isset($map[$i][$x])) OR $map[$i][$x]==1)){
|
||||||
for($i = $ytest; $i < $ytest + $shipWidth -1; $i++){
|
$bottom=false;
|
||||||
if ((!isset($map[$i][$xtest]))
|
$break;
|
||||||
OR ($map[$i][$xtest] == 1)){
|
}
|
||||||
$bottom=false;
|
}
|
||||||
break;
|
if($bottom){
|
||||||
}
|
$directions[]='bottom';
|
||||||
}
|
}
|
||||||
|
//test left
|
||||||
//droite
|
$left=true;
|
||||||
$right=true;
|
for($i = $x; $i > $x - $shipWidth; $i--){
|
||||||
for($i = $xtest; $i < $xtest + $shipWidth -1; $i++){
|
if((!isset($map[$y][$i])) OR ($map[$y][$i]==1)){
|
||||||
if((!isset($map[$ytest][$i]))
|
$left=false;
|
||||||
OR($map[$ytest][$i] == 1)){
|
$break;
|
||||||
$right= false;
|
}
|
||||||
break;
|
}
|
||||||
}
|
if($left){
|
||||||
}
|
$directions[]='left';
|
||||||
|
}
|
||||||
//gauche
|
//test right
|
||||||
$left=true;
|
$right=true;
|
||||||
for($i = $xtest; $i >= $xtest - $shipWidth + 1; $i--){
|
for($i = $x; $i < $x + $shipWidth; $i++){
|
||||||
if((!isset($map[$ytest][$i]))
|
if((!isset($map[$y][$i])) OR ($map[$y][$i]==1)){
|
||||||
OR($map[$ytest][$i] == 1)){
|
$right=false;
|
||||||
$left= false;
|
$break;
|
||||||
break;
|
}
|
||||||
}
|
}
|
||||||
}
|
if($right){
|
||||||
|
$directions[]='right';
|
||||||
$directions=array();
|
}
|
||||||
if($top){
|
|
||||||
$directions[]='top';
|
if(count($directions)>0){
|
||||||
}
|
$freeCases[]=array($x,$y,$directions);
|
||||||
if($bottom){
|
}
|
||||||
$directions[]='bottom';
|
|
||||||
}
|
}
|
||||||
if($left){
|
}
|
||||||
$directions[]='left';
|
}
|
||||||
}
|
|
||||||
if($right){
|
if(count($freeCases) == 0){
|
||||||
$directions[]='right';
|
//can't place the ship
|
||||||
}
|
goto a; //#facepalm
|
||||||
}
|
}
|
||||||
|
shuffle($freeCases); //choose start case for this ship
|
||||||
shuffle($directions);
|
shuffle($freeCases[0][2]); //choose random direction
|
||||||
switch($directions[0]){
|
$x=$freeCases[0][0];
|
||||||
|
$y=$freeCases[0][1];
|
||||||
|
switch($freeCases[0][2][0]){
|
||||||
case 'top':
|
case 'top':
|
||||||
$shipsCoords[]=$xtest.",".$ytest."-".$xtest.",".($ytest - $shipWidth + 1);
|
$shipsCoords[]=$x.",".$y."-".$x.",".($y - $shipWidth + 1);
|
||||||
$map= place_ship_on_map($xtest,$ytest,$xtest,$ytest - $shipWidth + 1,$map);
|
$map= place_ship_on_map($x,$y,$x,$y - $shipWidth + 1,$map);
|
||||||
break;
|
break;
|
||||||
case 'bottom':
|
case 'bottom':
|
||||||
$shipsCoords[]=$xtest.",".$ytest."-".$xtest.",".($ytest + $shipWidth - 1);
|
$shipsCoords[]=$x.",".$y."-".$x.",".($y + $shipWidth - 1);
|
||||||
$map= place_ship_on_map($xtest,$ytest,$xtest,$ytest + $shipWidth -1 ,$map);
|
$map= place_ship_on_map($x,$y,$x,$y + $shipWidth -1 ,$map);
|
||||||
break;
|
break;
|
||||||
case 'left':
|
case 'left':
|
||||||
$shipsCoords[]=$xtest.",".$ytest."-".($xtest - $shipWidth + 1).",".$ytest;
|
$shipsCoords[]=$x.",".$y."-".($x - $shipWidth + 1).",".$y;
|
||||||
$map= place_ship_on_map($xtest,$ytest,$xtest - $shipWidth + 1 ,$ytest,$map);
|
$map= place_ship_on_map($x,$y,$x - $shipWidth + 1 ,$y,$map);
|
||||||
break;
|
break;
|
||||||
case 'right':
|
case 'right':
|
||||||
$shipsCoords[]=$xtest.",".$ytest."-".($xtest + $shipWidth - 1 ).",".$ytest;
|
$shipsCoords[]=$x.",".$y."-".($x + $shipWidth - 1 ).",".$y;
|
||||||
$map= place_ship_on_map($xtest,$ytest,$xtest + $shipWidth -1 ,$ytest,$map);
|
$map= place_ship_on_map($x,$y,$x + $shipWidth -1 ,$y,$map);
|
||||||
break;
|
break;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
//print_r($map);
|
//print_r($map);
|
||||||
echo json_encode($shipsCoords);
|
echo json_encode($shipsCoords);
|
||||||
break;
|
break;
|
||||||
|
|
Loading…
Reference in New Issue
Block a user