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' => '');
$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;
while($end==false){
switch($playerPlayingNow){
case 1:
$playerURL=$bots[$bot1]['url'];
$playerCHAR='X';
$playerName=$bots[$bot1]['name'];
$playerIndex=0;
break;
case 2:
$playerURL=$bots[$bot2]['url'];
$playerCHAR='O';
$playerName=$bots[$bot2]['name'];
$playerIndex=1;
break;
default:
error(500,"oups");
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
if((isset($map[$playerResponse])) && ($map[$playerResponse]=="")){
//reponse conforme

View File

@ -16,6 +16,9 @@ function get_Post_Params($botsCount){
return array('bot1' => $_POST['bot1'],'bot2' => $_POST['bot2']);
}
/*
function get_Bots_Array(){
//Recupérer la liste des Bots
@ -31,20 +34,31 @@ function get_Bots_Array(){
return $bots;
}
*/
function get_IA_Response($youChar,$iaBaseUrl,$grille){
/*transforme la grille en parametres http GET
* et fait la requete vers $iaBaseUrl
* Retourne la réponse de l'IA
*/
$paramsGrille="";
foreach($grille as $key => $case){
$paramsGrille.="&".$key."=".$case;
}
$url=$iaBaseUrl."?you=".$youChar.$paramsGrille;
function get_IA_Response($iaUrl,$postParams){
//send params JSON as body
$data_string = json_encode($postParams);
/*
$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);
$output = curl_exec($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);
}