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,8 +1,12 @@
<?php
/*
function place_ship_on_map($x1,$y1,$x2,$y2,$map){
if (($x1 <> $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:

View File

@ -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.='<style type="text/css"><!--'."\n".file_get_contents("../src/arenas/".$currentArena."/".$currentArenaArr['cssFile'])."\n--></style>";
}
$jsAdditionalScript="";
}elseif(isset($_GET['page'])){

View File

@ -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<pre>".$anwserPlayer." ".$xStart.$yStart.$xEnd.$yEnd."</pre>";
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: <pre>".$anwserPlayer."</pre>";
if($player==1){
save_battle('Battleship',$bot1['name'],$bot2['name'],2);
}else{

View File

@ -39,5 +39,36 @@
<h4>"ship6"</h4>
<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>
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
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');
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]))

View File

@ -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
);
}

View File

@ -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;}