diff --git a/html/StupidIABattleship.php b/html/StupidIABattleship.php index 7701e49..66b75e1 100644 --- a/html/StupidIABattleship.php +++ b/html/StupidIABattleship.php @@ -1,8 +1,12 @@ $x2) && ($y1 <> $y2)){ - return false; + if ((($x1 <> $x2) && ($y1 <> $y2)) + OR (!isset($map[$y1][$x1])) + OR (!isset($map[$y2][$x2]))){ + return false; } + if($x1 == $x2){ //horizontal ship @@ -14,7 +18,11 @@ function place_ship_on_map($x1,$y1,$x2,$y2,$map){ $end=$y1; } for($i = $start; $i <= $end; $i++){ - $map[$i][$x1]=1; + if($map[$i][$x1]==0){ + $map[$i][$x1]=1; + }else{ + return false; + } } return $map; } @@ -28,11 +36,14 @@ function place_ship_on_map($x1,$y1,$x2,$y2,$map){ $end=$x1; } for( $i = $start; $i <= $end; $i++){ - $map[$y1][$i]=1; + if( $map[$y1][$i] == 0){ + $map[$y1][$i]=1; + }else{ + return false; + } } return $map; } - } switch($_POST['act']){ case "init": @@ -57,11 +68,11 @@ switch($_POST['act']){ if(!preg_match('/^[0-9]+-(1|2)$/',$match_id)){ echo "parametre incorrect"; die; } - + $map=array(); //construire une grille for($i=0; $i < $width; $i++){ for($j=0; $j < $height; $j++){ - $map[$i][$j]=0; + $map[$j][$i]=0; } } @@ -72,68 +83,58 @@ switch($_POST['act']){ //nombre de bateau à placer de cette taille $dynVar='ship'.$shipWidth; $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(); 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? - if($ytest < $shipWidth){ - $top=false; - }else{ - $top=true; - for($i = $ytest; $i > $ytest - $shipWidth; $i--){ - if($map[$i][$xtest] == 1){ - $top=false; - break; - } + + //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 - if($ytest + $shipWidth > $height){ - $bottom=false; - }else{ - $bottom=true; - for($y=$ytest; $i < $ytest + $shipWidth; $i++){ - if($map[$i][$xtest] == 1){ - $bottom=false; - break; - } + $bottom=true; + for($i = $ytest; $i < $ytest + $shipWidth -1; $i++){ + if ((!isset($map[$i][$xtest])) + OR ($map[$i][$xtest] == 1)){ + $bottom=false; + break; } } //droite - if($xtest + $shipWidth > $width){ - $right=false; - }else{ - $right=true; - for($i = $xtest; $i < $xtest + $shipWidth; $i++){ - if($map[$ytest][$i] == 1){ - $right= false; - break; - } + $right=true; + for($i = $xtest; $i < $xtest + $shipWidth -1; $i++){ + if((!isset($map[$ytest][$i])) + OR($map[$ytest][$i] == 1)){ + $right= false; + break; } } //gauche - if($xtest < $shipWidth){ - $left=false; - }else{ - $left=true; - for($i = $xtest; $i > $xtest - $shipWidth; $i--){ - if($map[$ytest][$i] == 1){ - $left= false; - break; - } - } + $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'; @@ -152,27 +153,27 @@ switch($_POST['act']){ shuffle($directions); switch($directions[0]){ case 'top': - $shipsCoords[]=$xtest.",".$ytest."-".$xtest.",".($ytest - $shipWidth); - $map= place_ship_on_map($xtest,$ytest,$xtest,$ytest - $shipWidth,$map); + $shipsCoords[]=$xtest.",".$ytest."-".$xtest.",".($ytest - $shipWidth + 1); + $map= place_ship_on_map($xtest,$ytest,$xtest,$ytest - $shipWidth + 1,$map); break; case 'bottom': - $shipsCoords[]=$xtest.",".$ytest."-".$xtest.",".($ytest + $shipWidth); - $map= place_ship_on_map($xtest,$ytest,$xtest,$ytest + $shipWidth,$map); + $shipsCoords[]=$xtest.",".$ytest."-".$xtest.",".($ytest + $shipWidth - 1); + $map= place_ship_on_map($xtest,$ytest,$xtest,$ytest + $shipWidth -1 ,$map); break; case 'left': - $shipsCoords[]=$xtest.",".$ytest."-".($xtest - $shipWidth).",".$ytest; - $map= place_ship_on_map($xtest,$ytest,$xtest - $shipWidth ,$ytest,$map); + $shipsCoords[]=$xtest.",".$ytest."-".($xtest - $shipWidth + 1).",".$ytest; + $map= place_ship_on_map($xtest,$ytest,$xtest - $shipWidth + 1 ,$ytest,$map); break; case 'right': - $shipsCoords[]=$xtest.",".$ytest."-".($xtest + $shipWidth).",".$ytest; - $map= place_ship_on_map($xtest,$ytest,$xtest + $shipWidth ,$ytest,$map); + $shipsCoords[]=$xtest.",".$ytest."-".($xtest + $shipWidth - 1 ).",".$ytest; + $map= place_ship_on_map($xtest,$ytest,$xtest + $shipWidth -1 ,$ytest,$map); break; } } } - + print_r($map); echo json_encode($shipsCoords); break; default: diff --git a/html/index.php b/html/index.php index 1e1393e..2ff7447 100644 --- a/html/index.php +++ b/html/index.php @@ -90,6 +90,9 @@ if(isset($_GET['arena'])){ $mainSectionScript="../src/arenas/".$currentArenaArr['id']."/doc-".$lang['lang'].".html"; $asideSectionContent=''; //to do $cssAdditionalScript=""; + if(isset($currentArenaArr['cssFile'])){ + $cssAdditionalScript.='"; + } $jsAdditionalScript=""; }elseif(isset($_GET['page'])){ diff --git a/src/arenas/Battleship/act.php b/src/arenas/Battleship/act.php index 2416726..6a62660 100644 --- a/src/arenas/Battleship/act.php +++ b/src/arenas/Battleship/act.php @@ -57,6 +57,10 @@ 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"); + } + //vars checked, lets init the initGame $_SESSION['matchId']=get_unique_id(); @@ -90,7 +94,7 @@ switch ($_POST['act']){ $anwserPlayer=get_IA_Response($currentBot['url'],$botParamsToSend); $boatsPlayer = json_decode( html_entity_decode($anwserPlayer)); 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){ save_battle('Battleship',$bot1['name'],$bot2['name'],2); }else{ @@ -109,19 +113,20 @@ switch ($_POST['act']){ //vérifier si'il y a le bon nombre de bateaux et les placer $nbBoatsIwant=array(0,$postValues['nbShip1'],$postValues['nbShip2'],$postValues['nbShip3'], $postValues['nbShip4'],$postValues['nbShip5'],$postValues['nbShip6']); + foreach($boatsPlayer as $boat){ list($startCoord,$endCoord) = explode("-",$boat); list($xStart,$yStart)=explode(",",$startCoord); list($xEnd,$yEnd)=explode(",",$endCoord); if($xStart == $xEnd){ - $long=abs($yStart - $yEnd); + $long=abs($yStart - $yEnd +1); }else{ - $long=abs($xStart - $xEnd); + $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"; + echo $currentBot['name']." n'a pas placé correctement ses bateaux. Certains se chevauchent. Il perd
".$anwserPlayer." ".$xStart.$yStart.$xEnd.$yEnd."
"; if($player==1){ save_battle('Battleship',$bot1['name'],$bot2['name'],2); }else{ @@ -132,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"; + echo $currentBot['name']." n'a pas placé correctement ses bateaux. Il perd. sa réponse:
".$anwserPlayer."
"; if($player==1){ save_battle('Battleship',$bot1['name'],$bot2['name'],2); }else{ diff --git a/src/arenas/Battleship/doc-fr.html b/src/arenas/Battleship/doc-fr.html index 5933375..d47c454 100644 --- a/src/arenas/Battleship/doc-fr.html +++ b/src/arenas/Battleship/doc-fr.html @@ -39,5 +39,36 @@

"ship6"

integer, compris entre 0 et 10, bornes incluses, Vous indique le nombre de bateaux de longeur 6 cases à placer.

Ce que doit retourner votre bot

-Il doit retourner la position des navires. +

Il doit retourner la position des navires.

+

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.

+ +

Exemple:

+ + + + + + + + + + + + +
0123456789
0
1
2
3
4
5
6
7
8
9
+

largeur; 10, hauteur 10; 3 bateaux:

+ +

Pour placer ces 3 bateaux, vous devez retourner au format JSON le tableau suivant:

+
+["1,0-1,3","2,4-3,4","3,6-5,6"]
+
diff --git a/src/arenas/Battleship/functions.php b/src/arenas/Battleship/functions.php index deca0ce..9079a2e 100644 --- a/src/arenas/Battleship/functions.php +++ b/src/arenas/Battleship/functions.php @@ -1,5 +1,27 @@ 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'); foreach($keysBots as $botKey){ if(!isset($_POST[$botKey])){ @@ -53,6 +75,8 @@ function get_IA_Response($iaUrl,$postParams){ curl_close($ch); return htmlentities($output); } + + function place_ship_on_map($x1,$y1,$x2,$y2,$map){ if ((($x1 <> $x2) && ($y1 <> $y2)) OR (!isset($map[$y1][$x1])) diff --git a/src/arenas/Battleship/js.js b/src/arenas/Battleship/js.js index 3986ac8..dfaa90a 100644 --- a/src/arenas/Battleship/js.js +++ b/src/arenas/Battleship/js.js @@ -21,81 +21,107 @@ function createElem(type,attributes) } function battleship(bot1,bot2,gridWidth,gridHeight,nbShip1,nbShip2,nbShip3,nbShip4,nbShip5,nbShip6,xd_check){ - var bot1IdName = bot1.split("-"); - var bot2IdName = bot2.split("-"); + + 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 bot2IdName = bot2.split("-"); document.getElementById('fightResult').innerHTML = ''; //dessiner les deux grilles var tableAdv=createElem("table",{"id":"tbl1","class":"battleshipGrid"}); var tableMe=createElem("table",{"id":"tbl2","class":"battleshipGrid"}); - //ligne de titre - var trTitre1=createElem("tr"); - var trTitre2=createElem("tr"); - var tdTitre1=createElem("th",{"colspan":gridWidth}); - var tdTitre2=createElem("th",{"colspan":gridWidth}); - tdTitre1.innerHTML = bot1IdName[1]; - tdTitre2.innerHTML = bot2IdName[1]; - trTitre1.appendChild(tdTitre1); - tableAdv.appendChild(trTitre1); - trTitre2.appendChild(tdTitre2); - tableMe.appendChild(trTitre2); - + //ligne de titre + var trTitre1=createElem("tr"); + var trTitre2=createElem("tr"); + var tdTitre1=createElem("th",{"colspan":gridWidth}); + var tdTitre2=createElem("th",{"colspan":gridWidth}); + tdTitre1.innerHTML = bot1IdName[1]; + tdTitre2.innerHTML = bot2IdName[1]; + trTitre1.appendChild(tdTitre1); + tableAdv.appendChild(trTitre1); + trTitre2.appendChild(tdTitre2); + tableMe.appendChild(trTitre2); + for (var i=0; i < gridHeight ; i++){ - var trAdv=createElem("tr"); - var trMe=createElem("tr"); - for (var j=0; j < gridWidth ; j++){ - var tdAdv=createElem("td",{"id":"bot1-" + i +"-" + j,"class": "empty"}); - var tdMe=createElem("td",{"id":"bot2-" + i +"-" + j,"class": "empty"}); - trAdv.appendChild(tdAdv); - trMe.appendChild(tdMe); - } - tableAdv.appendChild(trAdv); - tableMe.appendChild(trMe); + var trAdv=createElem("tr"); + var trMe=createElem("tr"); + for (var j=0; j < gridWidth ; j++){ + var tdAdv=createElem("td",{"id":"bot1-" + i +"-" + j,"class": "empty"}); + var tdMe=createElem("td",{"id":"bot2-" + i +"-" + j,"class": "empty"}); + trAdv.appendChild(tdAdv); + trMe.appendChild(tdMe); + } + tableAdv.appendChild(trAdv); + tableMe.appendChild(trMe); } document.getElementById('fightResult').appendChild(tableAdv); document.getElementById('fightResult').appendChild(tableMe); var divLogs=createElem("div",{"id":"logs"}); document.getElementById('fightResult').appendChild(divLogs); - - - + + + 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 ++){ - 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; - - } - } - }}; - xhr.open("POST", '/Battleship', true); - xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded"); - xhr.send( - 'act=initGame&bot1=' + bot1IdName[0] - + '&bot2=' + bot2IdName[0] - + '&gridWidth=' + gridWidth - + '&gridHeight=' + gridHeight - + '&nbShip1=' + nbShip1 - + '&nbShip2=' + nbShip2 - + '&nbShip3=' + nbShip3 - + '&nbShip4=' + nbShip4 - + '&nbShip5=' + nbShip5 - + '&nbShip6=' + nbShip6 - + '&xd_check=' + xd_check - ); - + 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; + + } + } + }}; + xhr.open("POST", '/Battleship', true); + xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded"); + xhr.send( + 'act=initGame&bot1=' + bot1IdName[0] + + '&bot2=' + bot2IdName[0] + + '&gridWidth=' + gridWidth + + '&gridHeight=' + gridHeight + + '&nbShip1=' + nbShip1 + + '&nbShip2=' + nbShip2 + + '&nbShip3=' + nbShip3 + + '&nbShip4=' + nbShip4 + + '&nbShip5=' + nbShip5 + + '&nbShip6=' + nbShip6 + + '&xd_check=' + xd_check + ); + } \ No newline at end of file diff --git a/src/arenas/Battleship/style.css b/src/arenas/Battleship/style.css index 3f7b941..062c39d 100644 --- a/src/arenas/Battleship/style.css +++ b/src/arenas/Battleship/style.css @@ -3,6 +3,7 @@ article p label {float:left; text-align:right; width:60%} article p select {} td{min-width: 15px; height: 15px;} .battleshipGrid{float: left; border-collapse:collapse; margin: 20px 20px 20px 20px;} +.nofloat{float: none;} .battleshipGrid tr{} .battleshipGrid tr td{border: 1px dashed green;} .battleshipGrid tr th{text-align: center;}