commit
3295ae7196
|
@ -206,6 +206,16 @@ switch($_POST['act']){
|
|||
//print_r($map);
|
||||
echo json_encode($shipsCoords);
|
||||
break;
|
||||
case "fight":
|
||||
// if((is_int(settype($_POST['width'],"integer"))) && (is_int(settype($_POST['height'],"integer")))){
|
||||
echo rand(0,$_POST['width'] -1).",".rand(0,$_POST['height'] -1);
|
||||
// }else{
|
||||
// echo "boaf".$_POST['width']."-".$_POST['height'];
|
||||
// }
|
||||
|
||||
|
||||
die;
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
|
||||
|
|
|
@ -4,6 +4,11 @@ $bots=get_Bots_Array('Battleship');
|
|||
|
||||
switch ($_POST['act']){
|
||||
case "initGame":
|
||||
//remove $_SESSION less xd_check
|
||||
$xd=$_SESSION['xd_check'];
|
||||
session_unset();
|
||||
$_SESSION['xd_check']=$xd;
|
||||
|
||||
//verifier parametres POST
|
||||
$postParamsWanted=array(
|
||||
// key,min,max
|
||||
|
@ -43,10 +48,12 @@ switch ($_POST['act']){
|
|||
if($bot['id'] == $_POST['bot1']){
|
||||
|
||||
$bot1 = $bot;
|
||||
$_SESSION['bot1']=$bot;
|
||||
$bot1Exists =true;
|
||||
}
|
||||
if($bot['id'] == $_POST['bot2']){
|
||||
$bot2 = $bot;
|
||||
$_SESSION['bot2']=$bot;
|
||||
$bot2Exists =true;
|
||||
}
|
||||
if ($bot1Exists && $bot2Exists){
|
||||
|
@ -134,6 +141,33 @@ switch ($_POST['act']){
|
|||
}
|
||||
die;
|
||||
}
|
||||
//remember each cases of each boats
|
||||
$boatListOfCases=array();
|
||||
if($xStart == $xEnd){
|
||||
if($yStart <= $yEnd ){
|
||||
$start=$yStart;
|
||||
$end=$yEnd;
|
||||
}else{
|
||||
$start=$yEnd;
|
||||
$end=$yStart;
|
||||
}
|
||||
for($i = $start; $i <= $end; $i++){
|
||||
$boatListOfCases[]=$xStart.",".$i;
|
||||
}
|
||||
}else{
|
||||
if($xStart <= $xEnd ){
|
||||
$start=$xStart;
|
||||
$end=$xEnd;
|
||||
}else{
|
||||
$start=$xEnd;
|
||||
$end=$xStart;
|
||||
}
|
||||
for($i = $start; $i <= $end; $i++){
|
||||
$boatListOfCases[]=$i.",".$yStart;
|
||||
}
|
||||
|
||||
}
|
||||
$_SESSION['ships'][$player][]=$boatListOfCases;
|
||||
}
|
||||
foreach($nbBoatsIwant as $nb){
|
||||
if($nb <> 0){
|
||||
|
@ -147,12 +181,107 @@ switch ($_POST['act']){
|
|||
}
|
||||
}
|
||||
}
|
||||
|
||||
//$_SESSION['grids']=$grid;
|
||||
$_SESSION['ship1']=$postValues['nbShip1'];
|
||||
$_SESSION['ship2']=$postValues['nbShip2'];
|
||||
$_SESSION['ship3']=$postValues['nbShip3'];
|
||||
$_SESSION['ship4']=$postValues['nbShip4'];
|
||||
$_SESSION['ship5']=$postValues['nbShip5'];
|
||||
$_SESSION['ship6']=$postValues['nbShip6'];
|
||||
$_SESSION['strikes'][1]=array();
|
||||
$_SESSION['strikes'][2]=array();
|
||||
$_SESSION['width']=$postValues['gridWidth'];
|
||||
$_SESSION['height']=$postValues['gridHeight'];
|
||||
echo json_encode($grid); die;
|
||||
|
||||
die;
|
||||
|
||||
break;
|
||||
case "fight";
|
||||
|
||||
/*
|
||||
print_r($_SESSION);
|
||||
Array ( [xd_check] => VSlWXLQVbYL2sCBwqetQdorR9 [bot1] => Array ( [id] => 10 [name] => stupidIA [url] => http://botsArena.tinad.fr/StupidIABattleship.php [description] => ) [bot2] => Array ( [id] => 10 [name] => stupidIA [url] => http://botsArena.tinad.fr/StupidIABattleship.php [description] => ) [matchId] => 702 [ships] => Array ( [1] => Array ( [0] => Array ( [0] => 3,0 [1] => 3,1 [2] => 3,2 [3] => 3,3 [4] => 3,4 ) [1] => Array ( [0] => 1,5 [1] => 2,5 [2] => 3,5 [3] => 4,5 ) [2] => Array ( [0] => 7,5 [1] => 8,5 [2] => 9,5 ) [3] => Array ( [0] => 0,0 [1] => 1,0 [2] => 2,0 ) [4] => Array ( [0] => 4,4 [1] => 5,4 ) [5] => Array ( [0] => 5,3 [1] => 6,3 [2] => 7,3 [3] => 8,3 [4] => 9,3 ) [6] => Array ( [0] => 2,7 [1] => 3,7 [2] => 4,7 [3] => 5,7 ) [7] => Array ( [0] => 1,9 [1] => 2,9 [2] => 3,9 ) [8] => Array ( [0] => 7,4 [1] => 8,4 [2] => 9,4 ) [9] => Array ( [0] => 0,2 [1] => 0,3 ) [10] => Array ( [0] => 3,9 [1] => 4,9 [2] => 5,9 [3] => 6,9 [4] => 7,9 ) [11] => Array ( [0] => 2,2 [1] => 2,3 [2] => 2,4 [3] => 2,5 ) [12] => Array ( [0] => 0,8 [1] => 1,8 [2] => 2,8 ) [13] => Array ( [0] => 7,7 [1] => 8,7 [2] => 9,7 ) [14] => Array ( [0] => 8,3 [1] => 9,3 ) [15] => Array ( [0] => 4,5 [1] => 5,5 [2] => 6,5 [3] => 7,5 [4] => 8,5 ) [16] => Array ( [0] => 3,2 [1] => 4,2 [2] => 5,2 [3] => 6,2 ) [17] => Array ( [0] => 0,7 [1] => 1,7 [2] => 2,7 ) [18] => Array ( [0] => 6,1 [1] => 7,1 [2] => 8,1 ) [19] => Array ( [0] => 2,3 [1] => 3,3 ) ) [2] => Array ( [0] => Array ( [0] => 0,9 [1] => 1,9 [2] => 2,9 [3] => 3,9 [4] => 4,9 ) [1] => Array ( [0] => 1,4 [1] => 1,5 [2] => 1,6 [3] => 1,7 ) [2] => Array ( [0] => 3,0 [1] => 4,0 [2] => 5,0 ) [3] => Array ( [0] => 0,0 [1] => 1,0 [2] => 2,0 ) [4] => Array ( [0] => 5,2 [1] => 6,2 ) [5] => Array ( [0] => 2,0 [1] => 3,0 [2] => 4,0 [3] => 5,0 [4] => 6,0 ) [6] => Array ( [0] => 2,1 [1] => 3,1 [2] => 4,1 [3] => 5,1 ) [7] => Array ( [0] => 7,3 [1] => 8,3 [2] => 9,3 ) [8] => Array ( [0] => 0,2 [1] => 1,2 [2] => 2,2 ) [9] => Array ( [0] => 5,3 [1] => 5,4 ) [10] => Array ( [0] => 5,3 [1] => 6,3 [2] => 7,3 [3] => 8,3 [4] => 9,3 ) [11] => Array ( [0] => 0,2 [1] => 0,3 [2] => 0,4 [3] => 0,5 ) [12] => Array ( [0] => 7,2 [1] => 8,2 [2] => 9,2 ) [13] => Array ( [0] => 0,1 [1] => 1,1 [2] => 2,1 ) [14] => Array ( [0] => 7,7 [1] => 7,8 ) [15] => Array ( [0] => 4,1 [1] => 4,2 [2] => 4,3 [3] => 4,4 [4] => 4,5 ) [16] => Array ( [0] => 3,9 [1] => 4,9 [2] => 5,9 [3] => 6,9 ) [17] => Array ( [0] => 7,3 [1] => 8,3 [2] => 9,3 ) [18] => Array ( [0] => 6,8 [1] => 7,8 [2] => 8,8 ) [19] => Array ( [0] => 0,5 [1] => 0,6 ) ) ) [shots] => Array ( [1] => Array ( ) [2] => Array ( ) ) [width] => 10 [height] => 10 )
|
||||
*/
|
||||
if(count($_SESSION['strikes'][1]) == count($_SESSION['strikes'][2])){
|
||||
//player 1 has to fight
|
||||
$currentPlayer=1;
|
||||
$currentBot=$_SESSION['bot1'];
|
||||
$opponent=2;
|
||||
$opponentName=$_SESSION['bot2']['name'];
|
||||
}else{
|
||||
//it's player2
|
||||
$currentPlayer=2;
|
||||
$currentBot=$_SESSION['bot2'];
|
||||
$opponentName=$_SESSION['bot1']['name'];
|
||||
$opponent=1;
|
||||
}
|
||||
|
||||
$botParamsToSend=array(
|
||||
'game' => 'Battleship',
|
||||
'match_id' => $_SESSION['matchId']."-".$currentPlayer,
|
||||
'act' => 'fight',
|
||||
'opponent' => $opponentName,
|
||||
'width' => $_SESSION['width'],
|
||||
'height' => $_SESSION['height'],
|
||||
'ship1' => $_SESSION['ship1'],
|
||||
'ship2' => $_SESSION['ship2'],
|
||||
'ship3' => $_SESSION['ship3'],
|
||||
'ship4' => $_SESSION['ship4'],
|
||||
'ship5' => $_SESSION['ship5'],
|
||||
'ship6' => $_SESSION['ship6'],
|
||||
'your_strikes' => json_encode($_SESSION['strikes'][$currentPlayer]),
|
||||
'his_strikes' => json_encode($_SESSION['strikes'][$opponent])
|
||||
|
||||
);
|
||||
$anwserPlayer=get_IA_Response($currentBot['url'],$botParamsToSend);
|
||||
|
||||
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.".$anwserPlayer
|
||||
));
|
||||
save_battle('Battleship',$_SESSION['bot1']['name'],$_SESSION['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. ".$x.",".$y." C'est interdit par les conventions de Geneve. Il perd"
|
||||
));
|
||||
save_battle('Battleship',$_SESSION['bot1']['name'],$_SESSION['bot2']['name'],$opponent);
|
||||
die;
|
||||
}
|
||||
|
||||
//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
|
||||
);
|
||||
if(count( $_SESSION['strikes'][$currentPlayer]) < 10){
|
||||
$continue=1;
|
||||
}else{
|
||||
$continue=0;
|
||||
}
|
||||
echo json_encode(array(
|
||||
'opponent'=> $opponent,
|
||||
'target' => $x.",".$y,
|
||||
'log' => $currentBot['name']." tire en ".$x.",".$y." ".$result,
|
||||
'continue' => $continue
|
||||
));
|
||||
|
||||
die;
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
|
|
|
@ -19,6 +19,36 @@ function createElem(type,attributes)
|
|||
{elem.setAttribute(i,attributes[i]);}
|
||||
return elem;
|
||||
}
|
||||
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;
|
||||
}
|
||||
if( strike['target'] !== ''){
|
||||
var coords = strike['target'].split(",");
|
||||
document.getElementById( 'bot' + strike['opponent'] + '-' + coords[1] + '-' + coords[0]).innerHTML = "X";
|
||||
}
|
||||
var p=createElem("p");
|
||||
p.innerHTML=strike['log'];
|
||||
document.getElementById('logs').appendChild(p);
|
||||
|
||||
if( strike['continue'] == 1){
|
||||
fight(xd_check);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}};
|
||||
xhr.open("POST", '/Battleship', true);
|
||||
xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
|
||||
xhr.send('act=fight&xd_check=' + xd_check);
|
||||
|
||||
}
|
||||
|
||||
function battleship(bot1,bot2,gridWidth,gridHeight,nbShip1,nbShip2,nbShip3,nbShip4,nbShip5,nbShip6,xd_check){
|
||||
|
||||
|
@ -104,7 +134,7 @@ function battleship(bot1,bot2,gridWidth,gridHeight,nbShip1,nbShip2,nbShip3,nbShi
|
|||
var p=createElem("p");
|
||||
p.innerHTML='players placed theirs ships';
|
||||
document.getElementById('logs').appendChild(p);
|
||||
|
||||
fight(xd_check);
|
||||
}
|
||||
|
||||
}};
|
||||
|
|
|
@ -5,6 +5,6 @@ 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 td{border: 1px dashed green; text-align: center; font-weight: bold;}
|
||||
.battleshipGrid tr th{text-align: center;}
|
||||
.shipOn{background-color: black;}
|
||||
.shipOn{background-color: black; color: #fff;}
|
Loading…
Reference in New Issue
Block a user