botsArena/src/arenas/Battleship/js.js

144 lines
4.6 KiB
JavaScript
Raw Normal View History

2015-12-10 20:50:19 +01:00
function Ajx(){
var request = false;
try {request = new ActiveXObject('Msxml2.XMLHTTP');}
catch (err2) {
try {request = new ActiveXObject('Microsoft.XMLHTTP');}
catch (err3) {
try { request = new XMLHttpRequest();}
catch (err1) {
request = false;
}
}
}
return request;
}
2015-12-11 11:40:36 +01:00
function createElem(type,attributes)
{
var elem=document.createElement(type);
for (var i in attributes)
{elem.setAttribute(i,attributes[i]);}
return elem;
}
2015-12-18 13:18:14 +01:00
function fight(xd_check){
var xhr = Ajx();
xhr.onreadystatechange = function(){if(xhr.readyState == 4){
if(xhr.status == 200) {
var p=createElem("p");
p.innerHTML=xhr.responseText;
document.getElementById('logs').appendChild(p);
2015-12-18 13:21:21 +01:00
//fight(xd_check);
2015-12-18 13:18:14 +01:00
}
}};
xhr.open("POST", '/Battleship', true);
xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
xhr.send('act=fight&xd_check=' + xd_check);
}
2015-12-11 11:40:36 +01:00
2015-12-11 12:57:48 +01:00
function battleship(bot1,bot2,gridWidth,gridHeight,nbShip1,nbShip2,nbShip3,nbShip4,nbShip5,nbShip6,xd_check){
2015-12-17 16:21:15 +01:00
var shipsArea= parseInt(nbShip1) + 2 * parseInt(nbShip2) + 3 * parseInt(nbShip3) + 4 * parseInt(nbShip4) + 5 * parseInt(nbShip5) + 6 * parseInt(nbShip6);
2015-12-17 16:15:59 +01:00
if(shipsArea > parseInt(gridWidth * gridHeight / 2) ){
2015-12-17 16:22:26 +01:00
alert("Map is too small. Sum of ships areas must be under 50% of the map." + shipsArea + " ");
return;
}
2015-12-17 16:22:26 +01:00
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");
2015-12-17 16:13:08 +01:00
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);
2015-12-15 20:47:25 +01:00
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);
2015-12-15 20:47:25 +01:00
var xhr = Ajx();
xhr.onreadystatechange = function(){if(xhr.readyState == 4){
2015-12-17 22:27:17 +01:00
if(xhr.status == 200) {
//debug
//alert(xhr.responseText);
try{
var grids = JSON.parse(xhr.responseText);
2015-12-17 22:28:51 +01:00
}catch(e){
2015-12-17 22:27:17 +01:00
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";
}
}
}
}
2015-12-17 22:31:08 +01:00
var p=createElem("p");
p.innerHTML='players placed theirs ships';
document.getElementById('logs').appendChild(p);
2015-12-18 13:18:14 +01:00
fight(xd_check);
2015-12-17 22:27:17 +01:00
}
}};
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
);
2015-12-15 20:47:25 +01:00
2015-12-10 20:50:19 +01:00
}