This commit is contained in:
Gnieark 2015-12-18 21:22:43 +01:00
parent 306413458e
commit 403b9db5de
3 changed files with 58 additions and 12 deletions

View File

@ -187,8 +187,8 @@ switch ($_POST['act']){
$_SESSION['ship4']=$postValues['nbShip4'];
$_SESSION['ship5']=$postValues['nbShip5'];
$_SESSION['ship6']=$postValues['nbShip6'];
$_SESSION['hits'][1]=array();
$_SESSION['hits'][2]=array();
$_SESSION['strikes'][1]=array();
$_SESSION['strikes'][2]=array();
$_SESSION['width']=$postValues['gridWidth'];
$_SESSION['height']=$postValues['gridHeight'];
echo json_encode($grid); die;
@ -229,13 +229,50 @@ switch ($_POST['act']){
'ship4' => $_SESSION['ship4'],
'ship5' => $_SESSION['ship5'],
'ship6' => $_SESSION['ship6'],
'your_hits' => json_encode($_SESSION['hits'][$currentPlayer]),
'his_hits' => json_encode($_SESSION['hits'][$opponent])
'your_strikes' => json_encode($_SESSION['strikes'][$currentPlayer]),
'his_strikes' => json_encode($_SESSION['strikes'][$opponent])
);
$anwserPlayer=get_IA_Response($currentBot['url'],$botParamsToSend);
echo $anwserPlayer;
if(!preg_match('/^[0-9]+,[0-9]$/',$anwserPlayer){
echo json_encode(array(
'target' => '',
'log' => $currentBot['name']." a fait une réponse non conforme, il perd"
));
save_battle('Battleship',$bot1['name'],$bot2['name'],$opponent);
die;
}
list($x,$y)=explode(",",$anwserPlayer);
//check if shot is under map's limits
if(($x >= $_SESSION['width']) OR ($y >= $_SESSION['height'])){
echo json_encode(array(
'target' => '',
'log' => $currentBot['name']." a fait un tir en dehors des limites de la carte. C'est interdit par les conventions de Genève. Il perd"
));
save_battle('Battleship',$bot1['name'],$bot2['name'],$opponent);
}
//do this shot hit a boat
$result='';
foreach($_SESSION['ships'][$opponent] as $ennemyBoat{
if(in_array($x.",".$y, $ennemyBoat)){
$result='hit';
break;
}
}
//save the shot
$_SESSION['strikes'][$currentPlayer][]=array(
'target' => $x.",".$y,
'result' => $result
);
echo json_encode(array(
'opponent'=> $opponent,
'target' => $x.",".$y,
'log' => $currentBot['name']."tire en ".$x.",".$y." ".$result
));
die;
break;

View File

@ -23,8 +23,17 @@ function fight(xd_check){
var xhr = Ajx();
xhr.onreadystatechange = function(){if(xhr.readyState == 4){
if(xhr.status == 200) {
try{
var strike = JSON.parse(xhr.responseText);
}catch(e){
document.getElementById('logs').innerHTML = 'erreur' +xhr.responseText;
return;
}
var coords = strike['target'].split(",");
document.getElementById( 'bot' + strike['opponent'] + '-' + coords[1] + '-' + coords[0]).innerHTML = "X";
var p=createElem("p");
p.innerHTML=xhr.responseText;
p.innerHTML=strike['log'];
document.getElementById('logs').appendChild(p);
//fight(xd_check);

View File

@ -7,4 +7,4 @@ td{min-width: 15px; height: 15px;}
.battleshipGrid tr{}
.battleshipGrid tr td{border: 1px dashed green;}
.battleshipGrid tr th{text-align: center;}
.shipOn{background-color: black;}
.shipOn{background-color: black; color: #fff;}