You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

142 lines
2.5 KiB
PHTML

<?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
*/
8 years ago
function score($board,$me,$colToPlay){
$newBoard = $board;
//add cell
for($y = 0; $board[$y][$colToPlay] <> ""; $y++){
}
8 years ago
$newBoard[$y][$colToPlay] = $me;
8 years ago
//do I win?
8 years ago
$searchValue="";
for($i = 0 ; $i < 4; $i++){
$searchValue.=$me;
}
//horizontaly
$line="";
for ($i=0; $i < 7; $i++){
if ($newBoard[$y][$i] == ""){
$line.=" ";
}else{
$line.=$newBoard[$y][$i];
}
}
if(strpos($searchValue,$line) > -1){
return 42;
}
//verticaly
$line="";
for ($i=0; $i < 6; $i++){
if ($newBoard[$i][$colToPlay] == ""){
$line.=" ";
}else{
$line.=$newBoard[$i][$colToPlay];
}
}
if(strpos($searchValue,$line) > -1){
return 42;
}
8 years ago
8 years ago
//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--){
if($newBoard[$jy][$jx] == ""){
$line.=" ";
}else{
$line.=$newBoard[$jy][$jx];
}
}
if(strpos($searchValue,$line) > -1){
return 42;
}
8 years ago
8 years ago
/*
//diagonal / affin function like y=x+b
b = parseInt(y - x);
if( b > -1){
//first point has x=0
kx = 0;
ky = b
}else{
//first point has y=0
ky = 0;
kx = -b;
}
var line="";
var lx , ly;
for (lx = kx , ly = ky ; (lx < 7) && (ly < 6) ; lx++ , ly++){
if( board[ly][lx] == ""){
line += " ";
}else{
line += board[ly][lx];
}
}
if (line.indexOf(searchValue) > -1){
wins(currentPlayer);
return;
}
8 years ago
*/
8 years ago
}
function better_col($board,$me){
$betterScore= -1000;
$betterCol= -1;
for( $i = 0; $i < 7; $i++){
if($board[5][$i] == ""){
8 years ago
$sc = score($board,$me,$i);
8 years ago
if( score($board,$me,$i) > $betterScore){
$betterScore = $sc;
$betterCol = $i;
}
}
}
return $i;
}
8 years ago
$in=str_replace('""','" "',file_get_contents('php://input'));
8 years ago
$params=json_decode($in, TRUE);
switch($params['action']){
case "init":
echo '{"name":"Gnieark"}';
break;
case "play-turn":
8 years ago
echo $in;
8 years ago
echo '{"play":"'.better_col($params['board'],$params['you']).'"}';
break;
default:
break;
}