This commit is contained in:
Gnieark 2016-06-13 17:46:54 +02:00
parent 1f6e135c1a
commit c97f0620e2
3 changed files with 133 additions and 16 deletions

View File

@ -12,7 +12,7 @@ function score($board,$me,$opponent,$colToPlay,$depth){
$newBoard = $board;
//add cell
for($y = 0; $board[$y][$colToPlay] <> " "; $y++){
for($y = 0; $board[$y][$colToPlay] <> "+"; $y++){
}
$newBoard[$y][$colToPlay] = $me;
@ -22,7 +22,6 @@ function score($board,$me,$opponent,$colToPlay,$depth){
for($i = 0 ; $i < 4; $i++){
$searchValue.=$me;
}
//horizontaly
$line="";
for ($i=0; $i < 7; $i++){
@ -69,11 +68,12 @@ function score($board,$me,$opponent,$colToPlay,$depth){
$ix = -$b;
}
$line="";
for ($jx = $ix , $jy = $iy ; ($jx < 7) && ($jy < 6) ; $jx++ , $jy++){
$line.=$newBoard[$jy][$jx];
}
}
if(strpos($searchValue,$line) > -1){
echo "|->".htmlentities($line).strpos($searchValue,$line)."|\n";
print_r($newBoard);
return 42;
}
@ -86,11 +86,11 @@ function score($board,$me,$opponent,$colToPlay,$depth){
}
}
if($full){
return 0;
return 0;
}
if($depth < 3){
return 0 - better_col($newBoard,$opponent,$me,$depth + 1);
if($depth < 5){
return 0 - better_col($newBoard,$opponent,$me,$depth + 1);
}else{
return 0;
@ -101,20 +101,24 @@ function better_col($board,$me,$opponent,$depth){
$betterScore= -1000;
$betterCol= -1;
for( $i = 0; $i < 7; $i++){
if($board[5][$i] == " "){
if($board[5][$i] == "+"){
$sc = score($board,$me,$opponent,$i,$depth);
if($depth == 0){
echo $i." ".$sc."\n";
}
if( $sc > $betterScore){
$betterScore = $sc;
$betterCol = $i;
$betterCol = $i - $depth;
}
}
}
return $i;
return $betterCol;
}
//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);
switch($params['action']){
@ -125,7 +129,7 @@ switch($params['action']){
//find $opponent
for($x = 0; $x < 7 ; $x++){
for($y = 0; $y < 6 ; $y++){
if(($params['board'][$y][$x] <> " " ) && ($params['board'][$y][$x] <> $params['you'] )){
if(($params['board'][$y][$x] <> "+" ) && ($params['board'][$y][$x] <> $params['you'] )){
$opponent= $params['board'][$y][$x];
}
}
@ -135,7 +139,8 @@ switch($params['action']){
}elseif(!isset($opponent)){
$opponent="X";
}
echo '{"play":"'.better_col($params['board'],$params['you'],$opponent,0).'"}';
echo '{"play":"'.better_col($params['board'],$params['you'],$opponent,0).'"}';
break;
default:
break;

View File

@ -19,7 +19,15 @@ if($message['action'] == "init"){
function score_case($map,$me,$him,$case,$depth=0){
if($depth%2 == 0){
$isOpponent = false;
}else{
$isOpponent = true;
}
//test si la case est gagnante
$newMap=$map;
$newMap[$case] = $me;
@ -33,13 +41,18 @@ function score_case($map,$me,$him,$case,$depth=0){
OR (($newMap['0-0']==$newMap['1-1'])&&($newMap['1-1']==$newMap['2-2'])&&($newMap['2-2']!==""))
OR (($newMap['0-2']==$newMap['1-1'])&&($newMap['1-1']==$newMap['2-0'])&&($newMap['2-0']!==""))
){
return 10 - $depth;
if($isOpponent){
return $depth - 10;
}else{
return 10 - $depth;
}
}else{
if($depth == 9){
return 0;
}else{
$sc=choose_better_cell($newMap,$him,$me,$depth + 1);
return -$depth + $sc[1];
return -$sc[1];
}
}
}

99
tictactoe2.php Normal file
View File

@ -0,0 +1,99 @@
<?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');
/*
* Tic Tac Toe gnieark's IA V2
* Gnieark 2016
* licensed under the Do What the Fuck You Want to Public License http://www.wtfpl.net/
*/
//get the query
$message=json_decode(file_get_contents('php://input'), TRUE);
if($message['action'] == "init"){
//juste le petit message d'init
echo '{"name":"gnieark"}';
die;
}
function score_case($map,$me,$him,$case,$depth=0){
if($depth%2 == 0){
$isOpponent = false;
}else{
$isOpponent = true;
}
//test si la case est gagnante
$newMap=$map;
$newMap[$case] = $me;
if(
(($newMap['0-0']==$newMap['0-1'])&&($newMap['0-1']==$newMap['0-2'])&&($newMap['0-2']!==""))
OR (($newMap['1-0']==$newMap['1-1'])&&($newMap['1-1']==$newMap['1-2'])&&($newMap['1-2']!==""))
OR (($newMap['2-0']==$newMap['2-1'])&&($newMap['2-1']==$newMap['2-2'])&&($newMap['2-2']!==""))
OR (($newMap['0-0']==$newMap['1-0'])&&($newMap['1-0']==$newMap['2-0'])&&($newMap['2-0']!==""))
OR (($newMap['0-1']==$newMap['1-1'])&&($newMap['1-1']==$newMap['2-1'])&&($newMap['2-1']!==""))
OR (($newMap['0-2']==$newMap['1-2'])&&($newMap['1-2']==$newMap['2-2'])&&($newMap['2-2']!==""))
OR (($newMap['0-0']==$newMap['1-1'])&&($newMap['1-1']==$newMap['2-2'])&&($newMap['2-2']!==""))
OR (($newMap['0-2']==$newMap['1-1'])&&($newMap['1-1']==$newMap['2-0'])&&($newMap['2-0']!==""))
){
if($isOpponent){
return $depth - 10;
}else{
return 10 - $depth;
}
}else{
if($depth == 4){
return 0;
}else{
if($isOpponent){
$sc=choose_better_cell($newMap,$him,$me,$depth);
}else{
$sc=choose_better_cell($newMap,$him,$me,$depth + 1);
}
return -$sc[1];
}
}
}
function choose_better_cell($map,$me,$him,$depth=0){
$betterCell="";
$betterScore=-100;
static $arrCells= array('0-0','0-1','0-2','1-0','1-1','1-2','2-0','2-1','2-2');
foreach($arrCells as $cellKey){
if($map[$cellKey] == ""){
$sc = score_case($map, $me,$him,$cellKey,$depth);
if( $sc > $betterScore ){
$betterCell = $cellKey;
$betterScore= $sc;
}
}
}
return array($betterCell,$betterScore);
}
//count free cases
$freeCells=0;
for($x = 0; $x < 3; $x++){
for($y = 0; $y < 3; $y++){
if($message['board'][$x."-".$y] == ""){
$freeCells ++;
}elseif($message['board'][$x."-".$y] !== $message['you']){
$hisSymbol=$message['board'][$x."-".$y];
}
}
}
if(!isset($hisSymbol)){
if($message['you'] == 'X'){
$hisSymbol='O';
}else{
$hisSymbol="X";
}
}
echo '{"play":"'.choose_better_cell($message['board'],$message['you'], $hisSymbol,9 - $freeCells)[0].'"}';