fix
This commit is contained in:
parent
697395fe60
commit
3b4c14d782
|
@ -14,17 +14,33 @@ switch($params['action']){
|
|||
echo '{"name":"Stupid AI"}';
|
||||
break;
|
||||
case "play-turn":
|
||||
//to do
|
||||
//{"game-id":"","action":"play-turn","game":"tron","board":[[[619,240]],[[329,353]]],"player-index":0,"players":2}
|
||||
|
||||
//Input JSON exemple:
|
||||
/*{
|
||||
"game-id":"1647",
|
||||
"action":"play-turn",
|
||||
"game":"tron",
|
||||
"board":[
|
||||
[
|
||||
[425,763],[424,763],[423,763],[422,763],[421,763],[420,763],[419,763]
|
||||
],
|
||||
[
|
||||
[858,501],[857,501],[856,501],[855,501],[854,501],[853,501],[852,501]
|
||||
]
|
||||
],
|
||||
"player-index":0,
|
||||
"players":2}
|
||||
*/
|
||||
|
||||
//put all non empty coords on array
|
||||
$busyCells = array();
|
||||
|
||||
foreach($params['board'] as $tails){
|
||||
foreach($tails as $coords){
|
||||
$busyCells[] = $coords;
|
||||
foreach($params['board'] as $tail){
|
||||
foreach($tail as $coord){
|
||||
$busyCells[] = $coord[0].",".$coord[1];
|
||||
}
|
||||
}
|
||||
|
||||
//get my head coords
|
||||
$myCoords = end($params['board'][$params['player-index']]);
|
||||
|
||||
|
@ -32,18 +48,18 @@ switch($params['action']){
|
|||
$y = $myCoords[1];
|
||||
|
||||
$availablesDirs = array();
|
||||
if (in_array(($x + 1).",".$y, $busyCells)){
|
||||
$availablesDirs[] = "x+";
|
||||
}
|
||||
if (in_array(($x -1 ).",".$y, $busyCells)){
|
||||
if (!in_array(($x + 1).",".$y, $busyCells)){
|
||||
$availablesDirs[] = "x-";
|
||||
}
|
||||
if (in_array($x.",".($y + 1), $busyCells)){
|
||||
$availablesDirs[] = "y+";
|
||||
if (!in_array(($x -1 ).",".$y, $busyCells)){
|
||||
$availablesDirs[] = "x+";
|
||||
}
|
||||
if (in_array($x.",".($y - 1), $busyCells)){
|
||||
if (!in_array($x.",".($y + 1), $busyCells)){
|
||||
$availablesDirs[] = "y-";
|
||||
}
|
||||
if (!in_array($x.",".($y - 1), $busyCells)){
|
||||
$availablesDirs[] = "y+";
|
||||
}
|
||||
|
||||
if(count($availablesDirs) == 0){
|
||||
echo '{"play":"x+","comment":"I Loose"}';
|
||||
|
@ -51,7 +67,7 @@ switch($params['action']){
|
|||
shuffle($availablesDirs);
|
||||
echo '{"play":"'.$availablesDirs[0].'"}';
|
||||
}
|
||||
|
||||
//error_log(json_encode($availablesDirs));
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
|
|
Loading…
Reference in New Issue
Block a user