Merge pull request #23 from gnieark/dev

Dev
This commit is contained in:
Gnieark 2015-12-17 16:32:12 +01:00
commit 06ef9b5a2b
7 changed files with 218 additions and 127 deletions

View File

@ -1,9 +1,13 @@
<?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)){ if ((($x1 <> $x2) && ($y1 <> $y2))
OR (!isset($map[$y1][$x1]))
OR (!isset($map[$y2][$x2]))){
return false; return false;
} }
if($x1 == $x2){ if($x1 == $x2){
//horizontal ship //horizontal ship
if($y1 <= $y2 ){ if($y1 <= $y2 ){
@ -14,7 +18,11 @@ function place_ship_on_map($x1,$y1,$x2,$y2,$map){
$end=$y1; $end=$y1;
} }
for($i = $start; $i <= $end; $i++){ for($i = $start; $i <= $end; $i++){
if($map[$i][$x1]==0){
$map[$i][$x1]=1; $map[$i][$x1]=1;
}else{
return false;
}
} }
return $map; return $map;
} }
@ -28,11 +36,14 @@ function place_ship_on_map($x1,$y1,$x2,$y2,$map){
$end=$x1; $end=$x1;
} }
for( $i = $start; $i <= $end; $i++){ for( $i = $start; $i <= $end; $i++){
if( $map[$y1][$i] == 0){
$map[$y1][$i]=1; $map[$y1][$i]=1;
}else{
return false;
}
} }
return $map; return $map;
} }
} }
switch($_POST['act']){ switch($_POST['act']){
case "init": case "init":
@ -57,11 +68,11 @@ switch($_POST['act']){
if(!preg_match('/^[0-9]+-(1|2)$/',$match_id)){ if(!preg_match('/^[0-9]+-(1|2)$/',$match_id)){
echo "parametre incorrect"; die; echo "parametre incorrect"; die;
} }
$map=array();
//construire une grille //construire une grille
for($i=0; $i < $width; $i++){ for($i=0; $i < $width; $i++){
for($j=0; $j < $height; $j++){ for($j=0; $j < $height; $j++){
$map[$i][$j]=0; $map[$j][$i]=0;
} }
} }
@ -72,66 +83,56 @@ switch($_POST['act']){
//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){
$top=false; //haut
}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 ((!isset($map[$i][$xtest]))
OR ($map[$i][$xtest] == 1)){
$top=false; $top=false;
break; break;
} }
} }
}
//vers le bas //vers le bas
if($ytest + $shipWidth > $height){
$bottom=false;
}else{
$bottom=true; $bottom=true;
for($y=$ytest; $i < $ytest + $shipWidth; $i++){ for($i = $ytest; $i < $ytest + $shipWidth -1; $i++){
if($map[$i][$xtest] == 1){ if ((!isset($map[$i][$xtest]))
OR ($map[$i][$xtest] == 1)){
$bottom=false; $bottom=false;
break; break;
} }
} }
}
//droite //droite
if($xtest + $shipWidth > $width){
$right=false;
}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((!isset($map[$ytest][$i]))
OR($map[$ytest][$i] == 1)){
$right= false; $right= false;
break; break;
} }
} }
}
//gauche //gauche
if($xtest < $shipWidth){
$left=false;
}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((!isset($map[$ytest][$i]))
OR($map[$ytest][$i] == 1)){
$left= false; $left= false;
break; break;
} }
} }
}
$directions=array(); $directions=array();
@ -152,27 +153,27 @@ 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;
} }
} }
} }
print_r($map);
echo json_encode($shipsCoords); echo json_encode($shipsCoords);
break; break;
default: default:

View File

@ -90,6 +90,9 @@ if(isset($_GET['arena'])){
$mainSectionScript="../src/arenas/".$currentArenaArr['id']."/doc-".$lang['lang'].".html"; $mainSectionScript="../src/arenas/".$currentArenaArr['id']."/doc-".$lang['lang'].".html";
$asideSectionContent=''; //to do $asideSectionContent=''; //to do
$cssAdditionalScript=""; $cssAdditionalScript="";
if(isset($currentArenaArr['cssFile'])){
$cssAdditionalScript.='<style type="text/css"><!--'."\n".file_get_contents("../src/arenas/".$currentArena."/".$currentArenaArr['cssFile'])."\n--></style>";
}
$jsAdditionalScript=""; $jsAdditionalScript="";
}elseif(isset($_GET['page'])){ }elseif(isset($_GET['page'])){

View File

@ -57,6 +57,10 @@ switch ($_POST['act']){
error (500,"missing parameter 2"); 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");
}
//vars checked, lets init the initGame //vars checked, lets init the initGame
$_SESSION['matchId']=get_unique_id(); $_SESSION['matchId']=get_unique_id();
@ -90,7 +94,7 @@ switch ($_POST['act']){
$anwserPlayer=get_IA_Response($currentBot['url'],$botParamsToSend); $anwserPlayer=get_IA_Response($currentBot['url'],$botParamsToSend);
$boatsPlayer = json_decode( html_entity_decode($anwserPlayer)); $boatsPlayer = json_decode( html_entity_decode($anwserPlayer));
if(!$boatsPlayer){ if(!$boatsPlayer){
echo $currentBot['name']." a fait une réponse non conforme, il perd."; echo $currentBot['name']." a fait une réponse non conforme, il perd.".$anwserPlayer;
if($player==1){ if($player==1){
save_battle('Battleship',$bot1['name'],$bot2['name'],2); save_battle('Battleship',$bot1['name'],$bot2['name'],2);
}else{ }else{
@ -109,19 +113,20 @@ switch ($_POST['act']){
//vérifier si'il y a le bon nombre de bateaux et les placer //vérifier si'il y a le bon nombre de bateaux et les placer
$nbBoatsIwant=array(0,$postValues['nbShip1'],$postValues['nbShip2'],$postValues['nbShip3'], $nbBoatsIwant=array(0,$postValues['nbShip1'],$postValues['nbShip2'],$postValues['nbShip3'],
$postValues['nbShip4'],$postValues['nbShip5'],$postValues['nbShip6']); $postValues['nbShip4'],$postValues['nbShip5'],$postValues['nbShip6']);
foreach($boatsPlayer as $boat){ foreach($boatsPlayer as $boat){
list($startCoord,$endCoord) = explode("-",$boat); list($startCoord,$endCoord) = explode("-",$boat);
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]);
if(!$grid[$player]){ if(!$grid[$player]){
echo $currentBot['name']." n'a pas placé correctement ses bateaux. Certains se chevauchent. Il perd"; echo $currentBot['name']." n'a pas placé correctement ses bateaux. Certains se chevauchent. Il perd<pre>".$anwserPlayer." ".$xStart.$yStart.$xEnd.$yEnd."</pre>";
if($player==1){ if($player==1){
save_battle('Battleship',$bot1['name'],$bot2['name'],2); save_battle('Battleship',$bot1['name'],$bot2['name'],2);
}else{ }else{
@ -132,7 +137,7 @@ switch ($_POST['act']){
} }
foreach($nbBoatsIwant as $nb){ foreach($nbBoatsIwant as $nb){
if($nb <> 0){ if($nb <> 0){
echo $currentBot['name']." n'a pas placé correctement ses bateaux. Il perd"; echo $currentBot['name']." n'a pas placé correctement ses bateaux. Il perd. sa réponse: <pre>".$anwserPlayer."</pre>";
if($player==1){ if($player==1){
save_battle('Battleship',$bot1['name'],$bot2['name'],2); save_battle('Battleship',$bot1['name'],$bot2['name'],2);
}else{ }else{

View File

@ -39,5 +39,36 @@
<h4>"ship6"</h4> <h4>"ship6"</h4>
<p>integer, compris entre 0 et 10, bornes incluses, Vous indique le nombre de bateaux de longeur 6 cases à placer.</p> <p>integer, compris entre 0 et 10, bornes incluses, Vous indique le nombre de bateaux de longeur 6 cases à placer.</p>
<h3>Ce que doit retourner votre bot</h3> <h3>Ce que doit retourner votre bot</h3>
Il doit retourner la position des navires. <p>Il doit retourner la position des navires.</p>
<p>Vous retournez un tableau JSON ayant autant d'enregistrement que de navires.
Chaque navire est défini par les coordonnées de ses extrémités.</p>
<ul>
<li>La grille commence au point 0,0.</li>
<li>Les entiers; ordonnée et abscisses définissant un point sont séparés d'une virgule &quot;,&quot;.</li>
<li>Les deux points définissant les extrémités d'un navire sont séparés par un tiret &quot;-&quot;.</li>
</ul>
<h4>Exemple:</h4>
<table class="battleshipGrid nofloat" id="tbl1">
<tr><td></td><td>0</td><td>1</td><td>2</td><td>3</td><td>4</td><td>5</td><td>6</td><td>7</td><td>8</td><td>9</td></tr>
<tr><td>0</td><td class="empty" id="bot1-0-0"></td><td class="shipOn" id="bot1-0-1"></td><td class="empty" id="bot1-0-2"></td><td class="empty" id="bot1-0-3"></td><td class="empty" id="bot1-0-4"></td><td class="empty" id="bot1-0-5"></td><td class="empty" id="bot1-0-6"></td><td class="empty" id="bot1-0-7"></td><td class="empty" id="bot1-0-8"></td><td class="empty" id="bot1-0-9"></td></tr>
<tr><td>1</td><td class="empty" id="bot1-1-0"></td><td class="shipOn" id="bot1-1-1"></td><td class="empty" id="bot1-1-2"></td><td class="empty" id="bot1-1-3"></td><td class="empty" id="bot1-1-4"></td><td class="empty" id="bot1-1-5"></td><td class="empty" id="bot1-1-6"></td><td class="empty" id="bot1-1-7"></td><td class="empty" id="bot1-1-8"></td><td class="empty" id="bot1-1-9"></td></tr>
<tr><td>2</td><td class="empty" id="bot1-2-0"></td><td class="shipOn" id="bot1-2-1"></td><td class="empty" id="bot1-2-2"></td><td class="empty" id="bot1-2-3"></td><td class="empty" id="bot1-2-4"></td><td class="empty" id="bot1-2-5"></td><td class="empty" id="bot1-2-6"></td><td class="empty" id="bot1-2-7"></td><td class="empty" id="bot1-2-8"></td><td class="empty" id="bot1-2-9"></td></tr>
<tr><td>3</td><td class="empty" id="bot1-3-0"></td><td class="shipOn" id="bot1-3-1"></td><td class="empty" id="bot1-3-2"></td><td class="empty" id="bot1-3-3"></td><td class="empty" id="bot1-3-4"></td><td class="empty" id="bot1-3-5"></td><td class="empty" id="bot1-3-6"></td><td class="empty" id="bot1-3-7"></td><td class="empty" id="bot1-3-8"></td><td class="empty" id="bot1-3-9"></td></tr>
<tr><td>4</td><td class="empty" id="bot1-4-0"></td><td class="empty" id="bot1-4-1"></td><td class="shipOn" id="bot1-4-2"></td><td class="shipOn" id="bot1-4-3"></td><td class="empty" id="bot1-4-4"></td><td class="empty" id="bot1-4-5"></td><td class="empty" id="bot1-4-6"></td><td class="empty" id="bot1-4-7"></td><td class="empty" id="bot1-4-8"></td><td class="empty" id="bot1-4-9"></td></tr>
<tr><td>5</td><td class="empty" id="bot1-5-0"></td><td class="empty" id="bot1-5-1"></td><td class="empty" id="bot1-5-2"></td><td class="empty" id="bot1-5-3"></td><td class="empty" id="bot1-5-4"></td><td class="empty" id="bot1-5-5"></td><td class="empty" id="bot1-5-6"></td><td class="empty" id="bot1-5-7"></td><td class="empty" id="bot1-5-8"></td><td class="empty" id="bot1-5-9"></td></tr>
<tr><td>6</td><td class="empty" id="bot1-6-0"></td><td class="empty" id="bot1-6-1"></td><td class="empty" id="bot1-6-2"></td><td class="shipOn" id="bot1-6-3"></td><td class="shipOn" id="bot1-6-4"></td><td class="shipOn" id="bot1-6-5"></td><td class="empty" id="bot1-6-6"></td><td class="empty" id="bot1-6-7"></td><td class="empty" id="bot1-6-8"></td><td class="empty" id="bot1-6-9"></td></tr>
<tr><td>7</td><td class="empty" id="bot1-7-0"></td><td class="empty" id="bot1-7-1"></td><td class="empty" id="bot1-7-2"></td><td class="empty" id="bot1-7-3"></td><td class="empty" id="bot1-7-4"></td><td class="empty" id="bot1-7-5"></td><td class="empty" id="bot1-7-6"></td><td class="empty" id="bot1-7-7"></td><td class="empty" id="bot1-7-8"></td><td class="empty" id="bot1-7-9"></td></tr>
<tr><td>8</td><td class="empty" id="bot1-8-0"></td><td class="empty" id="bot1-8-1"></td><td class="empty" id="bot1-8-2"></td><td class="empty" id="bot1-8-3"></td><td class="empty" id="bot1-8-4"></td><td class="empty" id="bot1-8-5"></td><td class="empty" id="bot1-8-6"></td><td class="empty" id="bot1-8-7"></td><td class="empty" id="bot1-8-8"></td><td class="empty" id="bot1-8-9"></td></tr>
<tr><td>9</td><td class="empty" id="bot1-9-0"></td><td class="empty" id="bot1-9-1"></td><td class="empty" id="bot1-9-2"></td><td class="empty" id="bot1-9-3"></td><td class="empty" id="bot1-9-4"></td><td class="empty" id="bot1-9-5"></td><td class="empty" id="bot1-9-6"></td><td class="empty" id="bot1-9-7"></td><td class="empty" id="bot1-9-8"></td><td class="empty" id="bot1-9-9"></td></tr>
</table>
<p>largeur; 10, hauteur 10; 3 bateaux:</p>
<ul>
<li>Bateau de 4 cases ayant pour extrémités 1,0 et 1,3. sera noté &quot;1,0-1,3&quot; ou &quot;1,3-1,0&quot;</li>
<li>Bateau de 2 cases ayant pour extrémités 2,4 et 3,4. sera noté &quot;2,4-3,4&quot; ou &quot;3,4-2,4&quot;</li>
<li>Bateau de 3 cases ayant pour extrémités 3,6 et 5,6. sera noté &quot;3,6-5,6&quot; ou &quot;5,6-3,6&quot;</li>
</ul>
<p>Pour placer ces 3 bateaux, vous devez retourner au format JSON le tableau suivant:</p>
<pre>
&#91;&quot;1,0-1,3&quot;,&quot;2,4-3,4&quot;,&quot;3,6-5,6&quot;&#93;
</pre>

View File

@ -1,5 +1,27 @@
<?php <?php
function get_Post_Params($botsCount){ 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){
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 get_Post_Params($ccbotsCount){
$keysBots=array('bot1','bot2'); $keysBots=array('bot1','bot2');
foreach($keysBots as $botKey){ foreach($keysBots as $botKey){
if(!isset($_POST[$botKey])){ if(!isset($_POST[$botKey])){
@ -53,6 +75,8 @@ function get_IA_Response($iaUrl,$postParams){
curl_close($ch); curl_close($ch);
return htmlentities($output); return htmlentities($output);
} }
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)) if ((($x1 <> $x2) && ($y1 <> $y2))
OR (!isset($map[$y1][$x1])) OR (!isset($map[$y1][$x1]))

View File

@ -21,6 +21,28 @@ function createElem(type,attributes)
} }
function battleship(bot1,bot2,gridWidth,gridHeight,nbShip1,nbShip2,nbShip3,nbShip4,nbShip5,nbShip6,xd_check){ function battleship(bot1,bot2,gridWidth,gridHeight,nbShip1,nbShip2,nbShip3,nbShip4,nbShip5,nbShip6,xd_check){
var shipsArea= parseInt(nbShip1) + 2 * parseInt(nbShip2) + 3 * parseInt(nbShip3) + 4 * parseInt(nbShip4) + 5 * parseInt(nbShip5) + 6 * parseInt(nbShip6);
if(shipsArea > parseInt(gridWidth * gridHeight / 2) ){
alert("Map is too small. Sum of ships areas must be under 50% of the map." + shipsArea + " ");
return;
}
var ships= [0,parseInt(nbShip1),parseInt(nbShip2),parseInt(nbShip3),parseInt(nbShip4),parseInt(nbShip5),parseInt(nbShip6)];
var longestShip=0;
for(var i = 6; i > 0; i--){
if(ships[i] > 0){
longestShip=ships[i];
break;
}
}
if((longestShip==0)
||((longestShip > gridWidth) && (longestShip > gridHeight))
){
alert ("Map is too small. Grow it or reduce ships");
return;
}
var bot1IdName = bot1.split("-"); var bot1IdName = bot1.split("-");
var bot2IdName = bot2.split("-"); var bot2IdName = bot2.split("-");
document.getElementById('fightResult').innerHTML = ''; document.getElementById('fightResult').innerHTML = '';
@ -66,6 +88,10 @@ function battleship(bot1,bot2,gridWidth,gridHeight,nbShip1,nbShip2,nbShip3,nbShi
try{ try{
var grids = JSON.parse(xhr.responseText); var grids = JSON.parse(xhr.responseText);
for( var player=1; player <= 2 ; player ++){ 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 y=0; y < grids[player].length ; y++){
for (var x=0; x < grids[player][y].length ; x++){ for (var x=0; x < grids[player][y].length ; x++){
if (grids[player][y][x] == 1){ if (grids[player][y][x] == 1){

View File

@ -3,6 +3,7 @@ article p label {float:left; text-align:right; width:60%}
article p select {} article p select {}
td{min-width: 15px; height: 15px;} td{min-width: 15px; height: 15px;}
.battleshipGrid{float: left; border-collapse:collapse; margin: 20px 20px 20px 20px;} .battleshipGrid{float: left; border-collapse:collapse; margin: 20px 20px 20px 20px;}
.nofloat{float: none;}
.battleshipGrid tr{} .battleshipGrid tr{}
.battleshipGrid tr td{border: 1px dashed green;} .battleshipGrid tr td{border: 1px dashed green;}
.battleshipGrid tr th{text-align: center;} .battleshipGrid tr th{text-align: center;}