diff --git a/html/StupidIABattleship.php b/html/StupidIABattleship.php index 66b75e1..fdbba96 100644 --- a/html/StupidIABattleship.php +++ b/html/StupidIABattleship.php @@ -1,5 +1,28 @@ $gridHeight * $gridWidth / 2){ + return false; + } + //longest ship + for($i=6; $i > 0; $i--){ + $var='nbShipsSize'.$i; + if($$var > 0){ + $longestShip=$$var; + break; + } + } + if( (!isset($longestShip)) + OR(($longestShip > $gridWidth) && ($longestShip > $gridHeight)) + ){ + return false; + } + return true; +} + + function place_ship_on_map($x1,$y1,$x2,$y2,$map){ if ((($x1 <> $x2) && ($y1 <> $y2)) OR (!isset($map[$y1][$x1])) @@ -68,13 +91,20 @@ switch($_POST['act']){ if(!preg_match('/^[0-9]+-(1|2)$/',$match_id)){ echo "parametre incorrect"; die; } + + if(!is_it_possible_to_place_ships_on_grid($width,$height,$ship1,$ship2,$ship3,$ship4,$ship5,$ship6)){ + echo "I don't want play this game"; + die; + } + + a: $map=array(); //construire une grille for($i=0; $i < $width; $i++){ for($j=0; $j < $height; $j++){ $map[$j][$i]=0; } - } + } $shipsCoords=array(); @@ -84,96 +114,96 @@ switch($_POST['act']){ $dynVar='ship'.$shipWidth; $shipCount=$$dynVar; // #trollface for( $sh = 0; $sh < $shipCount; $sh++){ //loop for all boats witch size is $shipWidth - $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? - - //haut - $top=true; - for($i = $ytest; $i >= $ytest - $shipWidth + 1; $i--){ - if ((!isset($map[$i][$xtest])) - OR ($map[$i][$xtest] == 1)){ - $top=false; - break; - } - } - - //vers le bas - $bottom=true; - for($i = $ytest; $i < $ytest + $shipWidth -1; $i++){ - if ((!isset($map[$i][$xtest])) - OR ($map[$i][$xtest] == 1)){ - $bottom=false; - break; - } - } - - //droite - $right=true; - for($i = $xtest; $i < $xtest + $shipWidth -1; $i++){ - if((!isset($map[$ytest][$i])) - OR($map[$ytest][$i] == 1)){ - $right= false; - break; - } - } - - //gauche - $left=true; - for($i = $xtest; $i >= $xtest - $shipWidth + 1; $i--){ - if((!isset($map[$ytest][$i])) - OR($map[$ytest][$i] == 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]){ + //find free cases + $freeCases=array(); + for($y=0; $y < $height; $y++){ + for($x=0; $x < $width; $x++){ + if($map[$y][$x] == 0){ + $directions=array(); + //test top + $top=true; + for($i = $y; $i > $y - $shipWidth; $i--){ + if((!isset($map[$i][$x])) OR ($map[$i][$x]==1)){ + $top=false; + $break; + } + } + if($top){ + $directions[]='top'; + } + //test Bottom + $bottom=true; + for($i = $y; $i < $y + $shipWidth; $i++){ + if(((!isset($map[$i][$x])) OR $map[$i][$x]==1)){ + $bottom=false; + $break; + } + } + if($bottom){ + $directions[]='bottom'; + } + //test left + $left=true; + for($i = $x; $i > $x - $shipWidth; $i--){ + if((!isset($map[$y][$i])) OR ($map[$y][$i]==1)){ + $left=false; + $break; + } + } + if($left){ + $directions[]='left'; + } + //test right + $right=true; + for($i = $x; $i < $x + $shipWidth; $i++){ + if((!isset($map[$y][$i])) OR ($map[$y][$i]==1)){ + $right=false; + $break; + } + } + if($right){ + $directions[]='right'; + } + + if(count($directions)>0){ + $freeCases[]=array($x,$y,$directions); + } + + } + } + } + + if(count($freeCases) == 0){ + //can't place the ship + goto a; //#facepalm + } + shuffle($freeCases); //choose start case for this ship + shuffle($freeCases[0][2]); //choose random direction + $x=$freeCases[0][0]; + $y=$freeCases[0][1]; + switch($freeCases[0][2][0]){ case 'top': - $shipsCoords[]=$xtest.",".$ytest."-".$xtest.",".($ytest - $shipWidth + 1); - $map= place_ship_on_map($xtest,$ytest,$xtest,$ytest - $shipWidth + 1,$map); + $shipsCoords[]=$x.",".$y."-".$x.",".($y - $shipWidth + 1); + $map= place_ship_on_map($x,$y,$x,$y - $shipWidth + 1,$map); break; case 'bottom': - $shipsCoords[]=$xtest.",".$ytest."-".$xtest.",".($ytest + $shipWidth - 1); - $map= place_ship_on_map($xtest,$ytest,$xtest,$ytest + $shipWidth -1 ,$map); + $shipsCoords[]=$x.",".$y."-".$x.",".($y + $shipWidth - 1); + $map= place_ship_on_map($x,$y,$x,$y + $shipWidth -1 ,$map); break; case 'left': - $shipsCoords[]=$xtest.",".$ytest."-".($xtest - $shipWidth + 1).",".$ytest; - $map= place_ship_on_map($xtest,$ytest,$xtest - $shipWidth + 1 ,$ytest,$map); + $shipsCoords[]=$x.",".$y."-".($x - $shipWidth + 1).",".$y; + $map= place_ship_on_map($x,$y,$x - $shipWidth + 1 ,$y,$map); break; case 'right': - $shipsCoords[]=$xtest.",".$ytest."-".($xtest + $shipWidth - 1 ).",".$ytest; - $map= place_ship_on_map($xtest,$ytest,$xtest + $shipWidth -1 ,$ytest,$map); + $shipsCoords[]=$x.",".$y."-".($x + $shipWidth - 1 ).",".$y; + $map= place_ship_on_map($x,$y,$x + $shipWidth -1 ,$y,$map); break; } - } } - print_r($map); + + //print_r($map); echo json_encode($shipsCoords); break; default: diff --git a/src/arenas/Battleship/act.php b/src/arenas/Battleship/act.php index 6a62660..49a266b 100644 --- a/src/arenas/Battleship/act.php +++ b/src/arenas/Battleship/act.php @@ -57,8 +57,8 @@ switch ($_POST['act']){ error (500,"missing parameter 2"); } - if(!is_it_possible_to_place_ships_on_grid($postValues['gridWidth'],$postValues['gridHeight'],$postValues['nbShip1'],$posValues['nbship2'],$postValues['nbship3'],$postValues['nbship4'],$postValues['nbship5'],$postvalues['nbship6'])){ - error (404,"grid is too little for these sips"); + if(!is_it_possible_to_place_ships_on_grid($postValues['gridWidth'],$postValues['gridHeight'],$postValues['nbShip1'],$postValues['nbShip2'],$postValues['nbShip3'],$postValues['nbShip4'],$postValues['nbShip5'],$postValues['nbShip6'])){ + error (404,"grid is too little for these ships"); } //vars checked, lets init the initGame @@ -119,14 +119,14 @@ switch ($_POST['act']){ list($xStart,$yStart)=explode(",",$startCoord); list($xEnd,$yEnd)=explode(",",$endCoord); if($xStart == $xEnd){ - $long=abs($yStart - $yEnd +1); + $long=abs($yStart - $yEnd) +1; }else{ - $long=abs($xStart - $xEnd +1); + $long=abs($xStart - $xEnd) +1; } $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
".$anwserPlayer." ".$xStart.$yStart.$xEnd.$yEnd."
"; + 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{ @@ -137,7 +137,7 @@ switch ($_POST['act']){ } foreach($nbBoatsIwant as $nb){ if($nb <> 0){ - echo $currentBot['name']." n'a pas placé correctement ses bateaux. Il perd. sa réponse:
".$anwserPlayer."
"; + echo $currentBot['name']." n'a pas placé le bon nombre de bateaux. Il perd."; if($player==1){ save_battle('Battleship',$bot1['name'],$bot2['name'],2); }else{ @@ -148,7 +148,7 @@ switch ($_POST['act']){ } } - $_SESSION['grids']=$grid; + //$_SESSION['grids']=$grid; echo json_encode($grid); die; die; diff --git a/src/arenas/Battleship/functions.php b/src/arenas/Battleship/functions.php index 9079a2e..ae7677c 100644 --- a/src/arenas/Battleship/functions.php +++ b/src/arenas/Battleship/functions.php @@ -2,8 +2,8 @@ function is_it_possible_to_place_ships_on_grid($gridWidth,$gridHeight,$nbShipsSize1,$nbShipsSize2,$nbShipsSize3,$nbShipsSize4,$nbShipsSize5,$nbShipsSize6){ //return false or true //not a perfect solution - $shipsArea=$nbShipsSize1 + 2 * $nbShipsSize2 + 3 * $nbShipsSize3 + 4 * $nbShipsSize4 + 5 * $nbShipsSize5 + 6 * $nbShipsSize6; - if( $shipsArea < $gridHeight * $gridwidth / 2){ + $shipsArea=$nbShipsSize1 + (2 * $nbShipsSize2) + (3 * $nbShipsSize3) + (4 * $nbShipsSize4) + (5 * $nbShipsSize5) + (6 * $nbShipsSize6); + if( $shipsArea > intval($gridHeight * $gridWidth / 2)){ return false; } //longest ship diff --git a/src/arenas/Battleship/js.js b/src/arenas/Battleship/js.js index dfaa90a..fd7d108 100644 --- a/src/arenas/Battleship/js.js +++ b/src/arenas/Battleship/js.js @@ -82,31 +82,31 @@ function battleship(bot1,bot2,gridWidth,gridHeight,nbShip1,nbShip2,nbShip3,nbShi var xhr = Ajx(); xhr.onreadystatechange = function(){if(xhr.readyState == 4){ - if(xhr.status == 200) { - //debug - //alert(xhr.responseText); - try{ - var grids = JSON.parse(xhr.responseText); - for( var player=1; player <= 2 ; player ++){ - var p=createElem("p"); - p.innerHTML='Reponse joueurs:' + xhr.responseText; - document.getElementById('logs').appendChild(p); - - for (var y=0; y < grids[player].length ; y++){ - for (var x=0; x < grids[player][y].length ; x++){ - if (grids[player][y][x] == 1){ - document.getElementById( 'bot' + player + '-' + y + '-' + x).className="shipOn"; - } - } - } - } - - } - catch(e){ - document.getElementById('logs').innerHTML = xhr.responseText; - - } - } + if(xhr.status == 200) { + //debug + //alert(xhr.responseText); + try{ + var grids = JSON.parse(xhr.responseText); + }catch(e){ + document.getElementById('logs').innerHTML = xhr.responseText; + return; + } + + for( var player=1; player <= 2 ; player ++){ + for (var y=0; y < grids[player].length ; y++){ + for (var x=0; x < grids[player][y].length ; x++){ + if (grids[player][y][x] == 1){ + document.getElementById( 'bot' + player + '-' + y + '-' + x).className="shipOn"; + } + } + } + } + var p=createElem("p"); + p.innerHTML='players placed theirs ships'; + document.getElementById('logs').appendChild(p); + + } + }}; xhr.open("POST", '/Battleship', true); xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");