2016-06-13 10:02:22 +02:00
|
|
|
<?php
|
|
|
|
header('Access-Control-Allow-Origin: *');
|
|
|
|
header('Access-Control-Allow-Methods: GET, POST');
|
|
|
|
header('Access-Control-Allow-Headers: Origin, X-Requested-With, Content-Type, Accept');
|
|
|
|
|
|
|
|
/*
|
|
|
|
* stupid IA for battle ship
|
|
|
|
* choose by random a free column
|
|
|
|
*/
|
2016-06-13 11:17:52 +02:00
|
|
|
|
|
|
|
function score($board,$me,$colToPlay){
|
|
|
|
|
|
|
|
$newBoard = $board;
|
|
|
|
//add cell
|
|
|
|
for($y = 0; $board[$y][$colToPlay] <> ""; $y++){
|
|
|
|
}
|
|
|
|
$newBoard[$y,$colToPlay] = $me;
|
|
|
|
|
|
|
|
//do I win?
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
function better_col($board,$me){
|
|
|
|
$betterScore= -1000;
|
|
|
|
$betterCol= -1;
|
|
|
|
for( $i = 0; $i < 7; $i++){
|
|
|
|
if($board[5][$i] == ""){
|
|
|
|
$sc = score($board,$me,$i)
|
|
|
|
if( score($board,$me,$i) > $betterScore){
|
|
|
|
$betterScore = $sc;
|
|
|
|
$betterCol = $i;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return $i;
|
|
|
|
}
|
|
|
|
|
2016-06-13 10:02:22 +02:00
|
|
|
$in=file_get_contents('php://input');
|
|
|
|
$params=json_decode($in, TRUE);
|
|
|
|
switch($params['action']){
|
|
|
|
case "init":
|
|
|
|
echo '{"name":"Gnieark"}';
|
|
|
|
break;
|
|
|
|
case "play-turn":
|
2016-06-13 11:17:52 +02:00
|
|
|
echo '{"play":"'.better_col($params['board'],$params['you']).'"}';
|
2016-06-13 10:02:22 +02:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|