loop for the two players

This commit is contained in:
Gnieark 2015-12-14 22:30:10 +01:00
parent 0c4e3396e8
commit 5349d192a8

View File

@ -62,51 +62,66 @@ switch ($_POST['act']){
$_SESSION['matchId']=get_unique_id();
for($player = 1; $player <= 2; $player++){
// get_IA_Response($iaUrl,$postParams)
//array à envoyer au bot 1
if($player==1){
$opponentName=$bot2['name'];
$currentBot=$bot1;
}else{
$opponentName=$bot1['name'];
$currentBot=$bot2;
}
$bot1ParamsToSend=array(
'game' => 'Battleship',
'match_id' => $_SESSION['matchId']."-1",
'act' => 'init',
'opponent' => $bot2['name'],
'width' => $postValues['gridWidth'],
'height' => $postValues['gridHeight'],
'ship1' => $postValues['nbShip1'],
'ship2' => $postValues['nbShip2'],
'ship3' => $postValues['nbShip3'],
'ship4' => $postValues['nbShip4'],
'ship5' => $postValues['nbShip5'],
'ship6' => $postValues['nbShip6']
$botParamsToSend=array(
'game' => 'Battleship',
'match_id' => $_SESSION['matchId']."-1",
'act' => 'init',
'opponent' => $opponentName,
'width' => $postValues['gridWidth'],
'height' => $postValues['gridHeight'],
'ship1' => $postValues['nbShip1'],
'ship2' => $postValues['nbShip2'],
'ship3' => $postValues['nbShip3'],
'ship4' => $postValues['nbShip4'],
'ship5' => $postValues['nbShip5'],
'ship6' => $postValues['nbShip6']
);
$anwserPlayer1=get_IA_Response($bot1['url'],$bot1ParamsToSend);
$boatsPlayer1 = json_decode( html_entity_decode($anwserPlayer1));
if(!$boatsPlayer1){
echo $bot1['name']." a fait une réponse non conforme, il perd.".$anwserPlayer1;
save_battle('Battleship',$bot1['name'],$bot2['name'],2);
}
//vérifier si'il y a le bon nombre de bateaux
$nbBoatsIwant=array(0,$postValues['nbShip1'],$postValues['nbShip2'],$postValues['nbShip3'],
$postValues['nbShip4'],$postValues['nbShip5'],$postValues['nbShip6']);
foreach($boatsPlayer1 as $boat){
list($startCoord,$endCoord) = explode("-",$boat);
list($xStart,$yStart)=explode(",",$startCoord);
list($xEnd,$yEnd)=explode(",",$endCoord);
if($xStart == $xEnd){
$long=abs($yStart - $yEnd);
);
$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.";
if($player==1){
save_battle('Battleship',$bot1['name'],$bot2['name'],2);
}else{
$long=abs($xStart - $xEnd);
save_battle('Battleship',$bot1['name'],$bot2['name'],1);
}
}
//vérifier si'il y a le bon nombre de bateaux
$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);
}else{
$long=abs($xStart - $xEnd);
}
$nbBoatsIwant[$long]-=1;
}
foreach($nbBoatsIwant as $nb){
if($nb <> 0){
echo $currentBot['name']." n'a pas placé correctement ses bateaux. Il perd";
if($player==1){
save_battle('Battleship',$bot1['name'],$bot2['name'],2);
}else{
save_battle('Battleship',$bot1['name'],$bot2['name'],1);
}
}
$nbBoatsIwant[$long]-=1;
}
foreach($nbBoatsIwant as $nb){
if($nb <> 0){
echo $bot1['name']." n'a pas placé correctement ses bateaux. Il perd";
save_battle('Battleship',$bot1['name'],$bot2['name'],2);
}
}