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
|
|
|
|
2016-06-13 16:12:08 +02:00
|
|
|
function score($board,$me,$opponent,$colToPlay,$depth){
|
2016-06-13 11:17:52 +02:00
|
|
|
|
|
|
|
$newBoard = $board;
|
|
|
|
//add cell
|
2016-06-13 16:12:08 +02:00
|
|
|
for($y = 0; $board[$y][$colToPlay] <> " "; $y++){
|
2016-06-13 11:17:52 +02:00
|
|
|
}
|
2016-06-13 13:51:18 +02:00
|
|
|
$newBoard[$y][$colToPlay] = $me;
|
2016-06-13 11:17:52 +02:00
|
|
|
|
|
|
|
//do I win?
|
2016-06-13 12:13:33 +02:00
|
|
|
|
|
|
|
$searchValue="";
|
|
|
|
for($i = 0 ; $i < 4; $i++){
|
|
|
|
$searchValue.=$me;
|
|
|
|
}
|
|
|
|
|
|
|
|
//horizontaly
|
|
|
|
$line="";
|
|
|
|
for ($i=0; $i < 7; $i++){
|
2016-06-13 16:12:08 +02:00
|
|
|
$line.=$newBoard[$y][$i];
|
2016-06-13 12:13:33 +02:00
|
|
|
}
|
|
|
|
if(strpos($searchValue,$line) > -1){
|
|
|
|
return 42;
|
|
|
|
}
|
|
|
|
|
|
|
|
//verticaly
|
|
|
|
$line="";
|
|
|
|
for ($i=0; $i < 6; $i++){
|
|
|
|
$line.=$newBoard[$i][$colToPlay];
|
|
|
|
}
|
|
|
|
if(strpos($searchValue,$line) > -1){
|
|
|
|
return 42;
|
|
|
|
}
|
|
|
|
|
2016-06-13 11:17:52 +02:00
|
|
|
|
2016-06-13 12:13:33 +02:00
|
|
|
//diagonal \
|
|
|
|
$line="";
|
|
|
|
$b = $y + $colToPlay;
|
|
|
|
|
|
|
|
if($b < 6){
|
|
|
|
$ix = 0;
|
|
|
|
$iy = $b;
|
|
|
|
}else{
|
|
|
|
$ix = $b - 5;
|
|
|
|
$iy = 5;
|
|
|
|
}
|
|
|
|
for($jx = $ix, $jy = $iy; ($jx < 7) && ($jy > -1); $jx++, $jy--){
|
|
|
|
$line.=$newBoard[$jy][$jx];
|
|
|
|
}
|
|
|
|
if(strpos($searchValue,$line) > -1){
|
|
|
|
return 42;
|
|
|
|
}
|
2016-06-13 16:12:08 +02:00
|
|
|
//diagonal /
|
|
|
|
$b = $y - $colToPlay;
|
|
|
|
if($b > -1){
|
|
|
|
$ix = 0;
|
|
|
|
$iy = $b;
|
|
|
|
}else{
|
|
|
|
$iy=0;
|
|
|
|
$ix = -$b;
|
|
|
|
}
|
|
|
|
$line="";
|
2016-06-13 13:46:21 +02:00
|
|
|
|
2016-06-13 16:12:08 +02:00
|
|
|
for ($jx = kx , $jy = ky ; ($jx < 7) && ($jy < 6) ; $jx++ , $jy++){
|
|
|
|
$line.=$newBoard[$jy][$jx];
|
|
|
|
}
|
|
|
|
if(strpos($searchValue,$line) > -1){
|
|
|
|
return 42;
|
|
|
|
}
|
2016-06-13 13:46:21 +02:00
|
|
|
|
2016-06-13 16:12:08 +02:00
|
|
|
//if grid is full
|
|
|
|
$full = true;
|
|
|
|
for($i = 0; $i < 7; $i++){
|
|
|
|
if($newBoard == " "){
|
|
|
|
$full = false;
|
|
|
|
break;
|
2016-06-13 12:13:33 +02:00
|
|
|
}
|
2016-06-13 16:12:08 +02:00
|
|
|
}
|
|
|
|
if($full){
|
|
|
|
return 0;
|
|
|
|
}
|
2016-06-13 11:17:52 +02:00
|
|
|
|
2016-06-13 16:19:12 +02:00
|
|
|
if($depth < 3){
|
2016-06-13 16:12:08 +02:00
|
|
|
return 0 - better_col($newBoard,$opponent,$me,$depth + 1);
|
|
|
|
|
|
|
|
}else{
|
|
|
|
return 0;
|
|
|
|
}
|
2016-06-13 11:17:52 +02:00
|
|
|
}
|
|
|
|
|
2016-06-13 16:12:08 +02:00
|
|
|
function better_col($board,$me,$opponent,$depth){
|
2016-06-13 11:17:52 +02:00
|
|
|
$betterScore= -1000;
|
|
|
|
$betterCol= -1;
|
|
|
|
for( $i = 0; $i < 7; $i++){
|
2016-06-13 16:16:41 +02:00
|
|
|
if($board[5][$i] == " "){
|
2016-06-13 16:12:08 +02:00
|
|
|
$sc = score($board,$me,$opponent,$i,$depth);
|
2016-06-13 16:28:20 +02:00
|
|
|
if( $sc > $betterScore){
|
2016-06-13 11:17:52 +02:00
|
|
|
$betterScore = $sc;
|
|
|
|
$betterCol = $i;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return $i;
|
|
|
|
}
|
|
|
|
|
2016-06-13 16:12:08 +02:00
|
|
|
//replace "" by " ", it will simplify my code.
|
2016-06-13 13:57:28 +02:00
|
|
|
$in=str_replace('""','" "',file_get_contents('php://input'));
|
2016-06-13 13:53:28 +02:00
|
|
|
|
2016-06-13 10:02:22 +02:00
|
|
|
$params=json_decode($in, TRUE);
|
|
|
|
switch($params['action']){
|
|
|
|
case "init":
|
|
|
|
echo '{"name":"Gnieark"}';
|
|
|
|
break;
|
|
|
|
case "play-turn":
|
2016-06-13 16:12:08 +02:00
|
|
|
//find $opponent
|
|
|
|
for($x = 0; $x < 7 ; $x++){
|
|
|
|
for($y = 0; $y < 6 ; $y++){
|
2016-06-13 16:13:30 +02:00
|
|
|
if(($params['board'][$y][$x] <> " " ) && ($params['board'][$y][$x] <> $params['you'] )){
|
2016-06-13 16:12:08 +02:00
|
|
|
$opponent= $params['board'][$y][$x];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if((!isset($opponent)) && ($params['you'] == "X")){
|
|
|
|
$opponent="O";
|
|
|
|
}elseif(!isset($opponent)){
|
|
|
|
$opponent="X";
|
|
|
|
}
|
|
|
|
echo '{"play":"'.better_col($params['board'],$params['you'],$opponent,0).'"}';
|
2016-06-13 10:02:22 +02:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|