IAS/connectfour.php

226 lines
7.6 KiB
PHP
Raw Normal View History

2016-06-13 10:02:22 +02:00
<?php
2016-06-15 01:09:21 +02:00
/*
Bot for connectfour https://botsarena.tinad.fr/connectFour
by Gnieark https://blog-du-grouik.tinad.fr/ june 2016
GNU GPL License
+--+--+--+--+--+--+--+
5 | | | | | | | |
+--+--+--+--+--+--+--+
4 | | | | | | | |
+--+--+--+--+--+--+--+
3 | | | | | | | |
+--+--+--+--+--+--+--+
2 | | | | | | | |
+--+--+--+--+--+--+--+
1 | | | | | | | |
+--+--+--+--+--+--+--+
0 | | | | | | | |
+--+--+--+--+--+--+--+
0 1 2 3 4 5 6
*/
2016-06-13 10:02:22 +02:00
header('Access-Control-Allow-Origin: *');
header('Access-Control-Allow-Methods: GET, POST');
header('Access-Control-Allow-Headers: Origin, X-Requested-With, Content-Type, Accept');
2016-06-15 01:09:21 +02:00
function can_win($line,$myChar){
//retourne la position du caractere a remplacer dans la ligne pour gagner
2016-06-15 09:21:51 +02:00
if (strpos($line,"+".$myChar.$myChar.$myChar) !== false ){
return strpos($line,"+".$myChar.$myChar.$myChar);
2016-06-13 12:13:33 +02:00
}
2016-06-15 09:21:51 +02:00
if (strpos($line,$myChar."+".$myChar.$myChar) !== false ){
return strpos($line,$myChar."+".$myChar.$myChar) + 1;
}
if (strpos($line,$myChar.$myChar."+".$myChar) !== false ){
return strpos($line,$myChar.$myChar."+".$myChar) + 2;
}
if (strpos($line,$myChar.$myChar.$myChar."+") !== false ){
return strpos($line,$myChar.$myChar.$myChar."+") + 3;
2016-06-13 22:13:07 +02:00
}
2016-06-15 01:09:21 +02:00
return false;
2016-06-13 21:52:45 +02:00
}
2016-06-15 01:09:21 +02:00
function can_loose($line,$hisChar){
//je pourrai perdre aux 2 prochains tours de jeu
// retourne la place du caractere à remplacer pour éviter ça
2016-06-15 09:21:51 +02:00
if (strpos($line,"+".$hisChar.$hisChar.$hisChar) !== false ){
return strpos($line,"+".$hisChar.$hisChar.$hisChar);
}
if (strpos($line,$hisChar."+".$hisChar.$hisChar) !== false ){
return strpos($line,$hisChar."+".$hisChar.$hisChar) + 1;
}
if (strpos($line,$hisChar.$hisChar."+".$hisChar) !== false ){
return strpos($line,$hisChar.$hisChar."+".$hisChar) + 2;
2016-06-13 11:17:52 +02:00
}
2016-06-15 09:21:51 +02:00
if (strpos($line,$hisChar.$hisChar.$hisChar."+") !== false ){
return strpos($line,$hisChar.$hisChar.$hisChar."+") + 3;
2016-06-15 01:09:21 +02:00
}
2016-06-15 09:21:51 +02:00
if (strpos($line,"+".$hisChar.$hisChar."+") !== false ){
return strpos($line,"+".$hisChar.$hisChar."+");
2016-06-15 01:09:21 +02:00
}
return false;
2016-06-13 11:17:52 +02:00
}
2016-06-13 16:12:08 +02:00
//replace "" by " ", it will simplify my code.
2016-06-15 09:54:01 +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-15 01:09:21 +02:00
//find $opponent and clean grid
2016-06-13 16:12:08 +02:00
for($x = 0; $x < 7 ; $x++){
for($y = 0; $y < 6 ; $y++){
2016-06-15 01:09:21 +02:00
//find opponent
2016-06-15 09:54:01 +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];
}
2016-06-15 01:09:21 +02:00
//tester si la case est jouable (s'il y a un support en dessous)
2016-06-15 09:54:01 +02:00
if ($params['board'][$y][$x] == "-" ){
//AND (($y==0) OR ($params['board'][$y - 1][$x] !== "-"))
//){
2016-06-15 01:09:21 +02:00
//la case est jouable, je la marque par un "+"
2016-06-15 09:54:01 +02:00
if($y == 0){
$params['board'][$y][$x] = "+";
}elseif(($params['board'][$y -1 ][$x] !== "-") AND ($params['board'][$y -1 ][$x] !== "+")){
$params['board'][$y][$x] = "+";
}else{}
2016-06-15 01:09:21 +02:00
}
2016-06-13 16:12:08 +02:00
}
}
if((!isset($opponent)) && ($params['you'] == "X")){
$opponent="O";
}elseif(!isset($opponent)){
$opponent="X";
}
2016-06-13 17:46:54 +02:00
2016-06-15 01:09:21 +02:00
//transformer la grille en lignes horizontales, verticales et diagonales
//verticales
for($x = 0; $x <7; $x ++){
$colStr="";
for($y = 0; $y <6; $y ++){
$colStr.= $params['board'][$y][$x];
}
if(can_win($colStr,$params['you']) !== false){
echo '{"play":'.$x.'}';
die;
}
if (can_loose($colStr,$opponent) !== false){
$colForNoLose = $x;
2016-06-15 13:37:15 +02:00
}
2016-06-15 01:09:21 +02:00
}
//horizontales
for($y = 0; $y <6; $y ++){
$lnStr="";
for($x = 0; $x <7; $x ++){
$lnStr.= $params['board'][$y][$x];
}
if(can_win($lnStr,$params['you']) !== false){
echo '{"play":'.can_win($lnStr,$params['you']).'}';
die;
}
if (can_loose($lnStr,$opponent) !== false){
2016-06-15 13:37:15 +02:00
$colForNoLose = can_loose($lnStr,$opponent);
2016-06-15 01:09:21 +02:00
}
}
//tester seulement les diagonales >= 4 cases
for ($k = 0; $k < 4; $k ++){
//diagonale /
$diagStr="";
for($x=$k , $y=0; isset($params['board'][$y][$x]); $x++, $y++){
$diagStr.=$params['board'][$y][$x];
}
if(can_win($diagStr,$params['you']) !== false){
2016-06-15 13:37:15 +02:00
echo '{"play":'.($k + can_win($diagStr,$params['you'])).'}';
2016-06-15 01:09:21 +02:00
die;
}
if (can_loose($diagStr,$opponent) !== false){
2016-06-15 13:37:15 +02:00
$colForNoLose = $k + can_loose($diagStr,$opponent);
2016-06-15 01:09:21 +02:00
}
//diagonale \
$diagStr="";
for($x=$k , $y=5; isset($params['board'][$y][$x]); $x++, $y--){
$diagStr.=$params['board'][$y][$x];
}
if(can_win($diagStr,$params['you']) !== false){
2016-06-15 13:37:15 +02:00
echo '{"play":'.($k + can_win($diagStr,$params['you'])).'}';
2016-06-15 01:09:21 +02:00
die;
}
if (can_loose($diagStr,$opponent) !== false){
2016-06-15 13:37:15 +02:00
$colForNoLose = ($k + can_loose($diagStr,$opponent));
2016-06-15 01:09:21 +02:00
}
}
for ($k = 0; $k < 3; $k ++){
//diagonale /
$diagStr="";
for($x = 0, $y = $k ; isset($params['board'][$y][$x]); $x++, $y++){
$diagStr.=$params['board'][$y][$x];
}
if(can_win($diagStr,$params['you']) !== false){
echo '{"play":'.$x.'}';
die;
}
if (can_loose($diagStr,$opponent) !== false){
$colForNoLose = $x;
}
}
for ($k = 3 ; $k < 6 ; $k++){
//diagonales \
$diagStr="";
for($x=0 , $y=$k; isset($params['board'][$y][$x]); $x++, $y--){
$diagStr.=$params['board'][$y][$x];
}
if(can_win($diagStr,$params['you']) !== false){
echo '{"play":'.$x.'}';
die;
}
if (can_loose($diagStr,$opponent) !== false){
$colForNoLose = $x;
}
}
//si j'arrive là, je ne gagne pas à ce tour
if(isset($colForNoLose)){
echo '{"play":'.$colForNoLose.'}';
die;
}
//still there? random
$colAvailable=array();
//dont play on full colomns
for($i=0;$i<7;$i++){
2016-06-15 09:54:01 +02:00
if(($params['board'][5][$i] == "+") OR ($params['board'][5][$i] == "-")){
2016-06-15 01:09:21 +02:00
$colAvailable[]=$i;
}
}
shuffle($colAvailable);
echo '{"play":'.$colAvailable[0].'}';
//echo '{"play":"'.better_col($params['board'],$params['you'],$opponent,0).'"}';
2016-06-13 10:02:22 +02:00
break;
default:
break;
2016-06-13 17:46:54 +02:00
}