IAS/stupidIATictactoe.php

45 lines
1.2 KiB
PHP
Raw Permalink Normal View History

2016-05-31 12:52:03 +02:00
<?php
2016-06-04 23:02:03 +02:00
/*
* Stupid artificial intelligence for tictactoe
* Bot for https://botsarena.tinad.fr
* By Gnieark 2016
* licensed under the Do What the Fuck You Want to Public License http://www.wtfpl.net/
*/
2016-06-04 22:58:46 +02:00
//headers for permit CORS,
//This permit me to test this bot, using this web page
//https://botsarena.tinad.fr/testBotScripts/tictactoe.html
2016-06-02 23:29:30 +02:00
header('Access-Control-Allow-Origin: *');
header('Access-Control-Allow-Methods: GET, POST');
2016-06-02 23:35:23 +02:00
header('Access-Control-Allow-Headers: Origin, X-Requested-With, Content-Type, Accept');
2016-06-02 23:29:30 +02:00
2016-06-04 22:58:46 +02:00
//get the query onto $message
$message=json_decode(file_get_contents('php://input'), TRUE);
if($message['action'] == "init"){
//It's the init message
echo '{"name":"stupidAI"}';
die;
}
$cellsKeys=array("0-0","0-1","0-2","1-0","1-1","1-2","2-0","2-1","2-2");
//list the free cells
$freeCells=array();
foreach($cellsKeys as $key){
if (!isset($message['board'][$key])){
2016-05-31 12:52:03 +02:00
echo "wrong parameters ".$case; die;
}
2016-06-04 22:58:46 +02:00
if($message['board'][$key] == ""){
$freeCells[] = $key;
2016-05-31 12:52:03 +02:00
}
}
2016-06-04 22:58:46 +02:00
if (count($freeCells) == 0){
echo "error. Board is full i can't play";
2016-05-31 12:52:03 +02:00
die;
}
2016-06-04 22:58:46 +02:00
//Stupid IA, juste random
shuffle($freeCells);
echo '{"play":"'.$freeCells[0].'"}';