This commit is contained in:
Gnieark 2016-06-13 16:12:08 +02:00
parent b41f15f28b
commit a75d70520e

View File

@ -8,7 +8,7 @@ header('Access-Control-Allow-Headers: Origin, X-Requested-With, Content-Type, Ac
* choose by random a free column * choose by random a free column
*/ */
function score($board,$me,$colToPlay){ function score($board,$me,$opponent,$colToPlay,$depth){
$newBoard = $board; $newBoard = $board;
//add cell //add cell
@ -26,12 +26,8 @@ function score($board,$me,$colToPlay){
//horizontaly //horizontaly
$line=""; $line="";
for ($i=0; $i < 7; $i++){ for ($i=0; $i < 7; $i++){
if ($newBoard[$y][$i] == ""){
$line.=" ";
}else{
$line.=$newBoard[$y][$i]; $line.=$newBoard[$y][$i];
} }
}
if(strpos($searchValue,$line) > -1){ if(strpos($searchValue,$line) > -1){
return 42; return 42;
} }
@ -39,12 +35,8 @@ function score($board,$me,$colToPlay){
//verticaly //verticaly
$line=""; $line="";
for ($i=0; $i < 6; $i++){ for ($i=0; $i < 6; $i++){
if ($newBoard[$i][$colToPlay] == ""){
$line.=" ";
}else{
$line.=$newBoard[$i][$colToPlay]; $line.=$newBoard[$i][$colToPlay];
} }
}
if(strpos($searchValue,$line) > -1){ if(strpos($searchValue,$line) > -1){
return 42; return 42;
} }
@ -62,60 +54,55 @@ function score($board,$me,$colToPlay){
$iy = 5; $iy = 5;
} }
for($jx = $ix, $jy = $iy; ($jx < 7) && ($jy > -1); $jx++, $jy--){ for($jx = $ix, $jy = $iy; ($jx < 7) && ($jy > -1); $jx++, $jy--){
if($newBoard[$jy][$jx] == ""){
$line.=" ";
}else{
$line.=$newBoard[$jy][$jx]; $line.=$newBoard[$jy][$jx];
} }
if(strpos($searchValue,$line) > -1){
return 42;
}
//diagonal /
$b = $y - $colToPlay;
if($b > -1){
$ix = 0;
$iy = $b;
}else{
$iy=0;
$ix = -$b;
}
$line="";
for ($jx = kx , $jy = ky ; ($jx < 7) && ($jy < 6) ; $jx++ , $jy++){
$line.=$newBoard[$jy][$jx];
} }
if(strpos($searchValue,$line) > -1){ if(strpos($searchValue,$line) > -1){
return 42; return 42;
} }
//if grid is full
$full = true;
for($i = 0; $i < 7; $i++){
if($newBoard == " "){
$full = false;
break;
}
}
if($full){
return 0;
}
if($depth < 5){
/* return 0 - better_col($newBoard,$opponent,$me,$depth + 1);
//diagonal / affin function like y=x+b
b = parseInt(y - x);
if( b > -1){
//first point has x=0
kx = 0;
ky = b
}else{ }else{
//first point has y=0 return 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){ function better_col($board,$me,$opponent,$depth){
wins(currentPlayer);
return;
}
*/
}
function better_col($board,$me){
$betterScore= -1000; $betterScore= -1000;
$betterCol= -1; $betterCol= -1;
for( $i = 0; $i < 7; $i++){ for( $i = 0; $i < 7; $i++){
if($board[5][$i] == ""){ if($board[5][$i] == ""){
$sc = score($board,$me,$i); $sc = score($board,$me,$opponent,$i,$depth);
if( score($board,$me,$i) > $betterScore){ if( score($board,$me,$i) > $betterScore){
$betterScore = $sc; $betterScore = $sc;
$betterCol = $i; $betterCol = $i;
@ -126,6 +113,7 @@ function better_col($board,$me){
return $i; return $i;
} }
//replace "" by " ", it will simplify my code.
$in=str_replace('""','" "',file_get_contents('php://input')); $in=str_replace('""','" "',file_get_contents('php://input'));
$params=json_decode($in, TRUE); $params=json_decode($in, TRUE);
@ -134,8 +122,20 @@ switch($params['action']){
echo '{"name":"Gnieark"}'; echo '{"name":"Gnieark"}';
break; break;
case "play-turn": case "play-turn":
echo $in; //find $opponent
echo '{"play":"'.better_col($params['board'],$params['you']).'"}'; for($x = 0; $x < 7 ; $x++){
for($y = 0; $y < 6 ; $y++){
if($params['board'][$y][$x] <> " " ) && ($params['board'][$y][$x] <> $params['you'] ){
$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).'"}';
break; break;
default: default:
break; break;