IAS/connectfour.php

141 lines
2.5 KiB
PHP
Raw Normal View History

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++){
}
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++){
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;
}
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--){
if($newBoard[$jy][$jx] == ""){
$line.=" ";
}else{
$line.=$newBoard[$jy][$jx];
}
}
if(strpos($searchValue,$line) > -1){
return 42;
}
2016-06-13 13:46:21 +02:00
2016-06-13 12:13:33 +02:00
/*
//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;
}
2016-06-13 12:18:23 +02:00
*/
2016-06-13 11:17:52 +02:00
}
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 13:46:21 +02:00
$in=preg_replace('/""/','" "',file_get_contents('php://input');
echo $in;
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 11:17:52 +02:00
echo '{"play":"'.better_col($params['board'],$params['you']).'"}';
2016-06-13 10:02:22 +02:00
break;
default:
break;
}