cohérance taill grille et nombre/taille des navires
This commit is contained in:
parent
b717a85e53
commit
30cd1bc199
|
@ -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();
|
||||||
|
|
|
@ -21,85 +21,107 @@ 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 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);
|
|
||||||
|
|
||||||
for (var i=0; i < gridHeight ; i++){
|
var shipsArea=nbShip1 + 2 * nbShip2 + 3 * nbShip3 + 4 * nbShip4 + 5 * nbShip5 + 6 * nbShip6;
|
||||||
var trAdv=createElem("tr");
|
if(shipsArea > gridWidth * gridHeight / 2 ){
|
||||||
var trMe=createElem("tr");
|
alert("Map is too small. Sum of ships areas must be under 50% of the map.");
|
||||||
for (var j=0; j < gridWidth ; j++){
|
return;
|
||||||
var tdAdv=createElem("td",{"id":"bot1-" + i +"-" + j,"class": "empty"});
|
}
|
||||||
var tdMe=createElem("td",{"id":"bot2-" + i +"-" + j,"class": "empty"});
|
var ships= [0,nbShip1,nbShip2,nbShip3,nbShip4,nbShip5,nbShip6];
|
||||||
trAdv.appendChild(tdAdv);
|
var longestShip=0;
|
||||||
trMe.appendChild(tdMe);
|
for(var i = 6; i > 0; i--){
|
||||||
}
|
if(ships[i] > 0){
|
||||||
tableAdv.appendChild(trAdv);
|
longestShip=ships[i];
|
||||||
tableMe.appendChild(trMe);
|
break;
|
||||||
}
|
}
|
||||||
document.getElementById('fightResult').appendChild(tableAdv);
|
}
|
||||||
document.getElementById('fightResult').appendChild(tableMe);
|
if((longestShip==0)
|
||||||
var divLogs=createElem("div",{"id":"logs"});
|
||((longestShip > gridWidth) && (longestShip > gridHeight))
|
||||||
document.getElementById('fightResult').appendChild(divLogs);
|
){
|
||||||
|
alert ("Map is too small. Grow it or reduce ships");
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
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);
|
||||||
|
|
||||||
|
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);
|
||||||
|
}
|
||||||
|
document.getElementById('fightResult').appendChild(tableAdv);
|
||||||
|
document.getElementById('fightResult').appendChild(tableMe);
|
||||||
|
var divLogs=createElem("div",{"id":"logs"});
|
||||||
|
document.getElementById('fightResult').appendChild(divLogs);
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
var xhr = Ajx();
|
var xhr = Ajx();
|
||||||
xhr.onreadystatechange = function(){if(xhr.readyState == 4){
|
xhr.onreadystatechange = function(){if(xhr.readyState == 4){
|
||||||
if(xhr.status == 200) {
|
if(xhr.status == 200) {
|
||||||
//debug
|
//debug
|
||||||
//alert(xhr.responseText);
|
//alert(xhr.responseText);
|
||||||
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");
|
var p=createElem("p");
|
||||||
p.innerHTML='Reponse joueurs:' + xhr.responseText;
|
p.innerHTML='Reponse joueurs:' + xhr.responseText;
|
||||||
document.getElementById('logs').appendChild(p);
|
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){
|
||||||
document.getElementById( 'bot' + player + '-' + y + '-' + x).className="shipOn";
|
document.getElementById( 'bot' + player + '-' + y + '-' + x).className="shipOn";
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
catch(e){
|
catch(e){
|
||||||
document.getElementById('logs').innerHTML = xhr.responseText;
|
document.getElementById('logs').innerHTML = xhr.responseText;
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}};
|
}};
|
||||||
xhr.open("POST", '/Battleship', true);
|
xhr.open("POST", '/Battleship', true);
|
||||||
xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
|
xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
|
||||||
xhr.send(
|
xhr.send(
|
||||||
'act=initGame&bot1=' + bot1IdName[0]
|
'act=initGame&bot1=' + bot1IdName[0]
|
||||||
+ '&bot2=' + bot2IdName[0]
|
+ '&bot2=' + bot2IdName[0]
|
||||||
+ '&gridWidth=' + gridWidth
|
+ '&gridWidth=' + gridWidth
|
||||||
+ '&gridHeight=' + gridHeight
|
+ '&gridHeight=' + gridHeight
|
||||||
+ '&nbShip1=' + nbShip1
|
+ '&nbShip1=' + nbShip1
|
||||||
+ '&nbShip2=' + nbShip2
|
+ '&nbShip2=' + nbShip2
|
||||||
+ '&nbShip3=' + nbShip3
|
+ '&nbShip3=' + nbShip3
|
||||||
+ '&nbShip4=' + nbShip4
|
+ '&nbShip4=' + nbShip4
|
||||||
+ '&nbShip5=' + nbShip5
|
+ '&nbShip5=' + nbShip5
|
||||||
+ '&nbShip6=' + nbShip6
|
+ '&nbShip6=' + nbShip6
|
||||||
+ '&xd_check=' + xd_check
|
+ '&xd_check=' + xd_check
|
||||||
);
|
);
|
||||||
|
|
||||||
}
|
}
|
Loading…
Reference in New Issue
Block a user