full JSON for tictactoe

This commit is contained in:
gnieark 2016-05-29 22:09:53 +02:00
parent 612265d8c6
commit 555e774e37
2 changed files with 61 additions and 12 deletions

View File

@ -21,25 +21,60 @@ switch ($_POST['act']){
'2-0' => '','2-1' => '','2-2' => ''); '2-0' => '','2-1' => '','2-2' => '');
$end=false; $end=false;
//send init message to bots
$game_id=get_unique_id();
for($player = 0; $player < 2; $player ++;){
$params[$player]=array(
'game-id' => $game_id,
'action' => 'init',
'game' => 'tictactoe',
'players' => 2,
'board' => '',
'player-index' => $player
);
}
get_IA_Response($bots[$bot1]['url'],$params[0]); //don't care about result
get_IA_Response($bots[$bot2]['url'],$params[1]); //don't care about result
//"game-id":"gameid","action":"init","game":"tictactoe","players":2,"board":null,"you":null,"player-index":0}
//[0000] body> {"name":"moul-tictactoe","play":null,"error":null}
$playerPlayingNow=1; $playerPlayingNow=1;
while($end==false){ while($end==false){
switch($playerPlayingNow){ switch($playerPlayingNow){
case 1: case 1:
$playerURL=$bots[$bot1]['url']; $playerURL=$bots[$bot1]['url'];
$playerCHAR='X'; $playerCHAR='X';
$playerName=$bots[$bot1]['name']; $playerName=$bots[$bot1]['name'];
$playerIndex=0;
break; break;
case 2: case 2:
$playerURL=$bots[$bot2]['url']; $playerURL=$bots[$bot2]['url'];
$playerCHAR='O'; $playerCHAR='O';
$playerName=$bots[$bot2]['name']; $playerName=$bots[$bot2]['name'];
$playerIndex=1;
break; break;
default: default:
error(500,"oups"); error(500,"oups");
die; die;
} }
$playerResponse=get_IA_Response($playerCHAR,$playerURL,$map);
$paramsToSend=array(
'game-id' => $game_id,
'action' => 'play-turn',
'game' => 'tictactoe',
'players' => 2,
'board' => $map,
'you' => $playerCHAR,
'player-index' =>$playerIndex
);
$httpResponse=json_decode(get_IA_Response($playerURL,$paramsToSend));
$playerResponse=$httpResponse['play'];
//tester la validité de la réponse //tester la validité de la réponse
if((isset($map[$playerResponse])) && ($map[$playerResponse]=="")){ if((isset($map[$playerResponse])) && ($map[$playerResponse]=="")){
//reponse conforme //reponse conforme

View File

@ -16,6 +16,9 @@ function get_Post_Params($botsCount){
return array('bot1' => $_POST['bot1'],'bot2' => $_POST['bot2']); return array('bot1' => $_POST['bot1'],'bot2' => $_POST['bot2']);
} }
/* /*
function get_Bots_Array(){ function get_Bots_Array(){
//Recupérer la liste des Bots //Recupérer la liste des Bots
@ -31,20 +34,31 @@ function get_Bots_Array(){
return $bots; return $bots;
} }
*/ */
function get_IA_Response($youChar,$iaBaseUrl,$grille){ function get_IA_Response($iaUrl,$postParams){
/*transforme la grille en parametres http GET //send params JSON as body
* et fait la requete vers $iaBaseUrl
* Retourne la réponse de l'IA $data_string = json_encode($postParams);
*/ /*
$paramsGrille="";
foreach($grille as $key => $case){
$paramsGrille.="&".$key."=".$case;
}
$url=$iaBaseUrl."?you=".$youChar.$paramsGrille;
$ch = curl_init(); $ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_URL, $iaUrl);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $postParams);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$output = curl_exec($ch); $output = curl_exec($ch);
curl_close($ch); curl_close($ch);
*/
$ch = curl_init($iaUrl);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Content-Type: application/json',
'Content-Length: ' . strlen($data_string))
);
$output= curl_exec($ch);
curl_close($ch);
return htmlentities($output); return htmlentities($output);
} }