OJD Commit: Fix node id reset
[eidogo-ojd.git] / php / gnugo.php
blobc31eab0a1154f69f6e5db90693a32297374cfe08
1 <?php
3 if ($_SERVER['HTTP_HOST'] == "eidogo.com") {
4 define("PATH_GNUGO", "/var/www/eidogo.com/gnugo/interface/gnugo");
5 } else {
6 define("PATH_GNUGO", "/Users/tin/Sites/eidogo/gnugo/gnugo/interface/gnugo");
9 $sgf = $_REQUEST['sgf'];
10 $color = $_REQUEST['move'] == "W" ? "W" : "B";
11 $size = (int)$_REQUEST['size']; // needed for coordinate conversion
12 $komi = (float)($_REQUEST['komi'] ? $_REQUEST['komi'] : 0);
13 $mn = (int)$_REQUEST['mn'];
15 // give gnugo our current game state
16 $sgf_file = tempnam("/tmp", "eidogo");
17 file_put_contents($sgf_file, $sgf);
19 if ($_REQUEST['move'] == "est") {
20 $result= shell_exec(PATH_GNUGO . " --score --komi $komi --infile $sgf_file --until $mn");
21 unlink($sgf_file);
22 echo $result;
23 exit;
26 // tell gnugo to generate a move
27 $command_file = tempnam("/tmp", "eidogo");
28 file_put_contents($command_file, "genmove $color\nquit\n");
30 $result = shell_exec(PATH_GNUGO . " --level 7 --mode gtp --infile $sgf_file --gtp-input $command_file");
32 unlink($sgf_file);
33 unlink($command_file);
35 $move = substr($result, 2, strpos($result, "\n")-2);
37 if (preg_match("/([A-Z])([0-9]+)/", $move, $matches)) {
38 // convert the move to SGF coordinates
39 list(,$x, $y) = $matches;
40 $x = strtolower($x);
41 if (ord($x) >= ord('i')) {
42 $x = chr(ord($x)-1);
44 $y = chr(($size - $y) + ord('a'));
45 echo "$x$y";
46 } elseif ($move == "PASS") {
47 echo "tt";
48 } elseif (strpos($move, "resign") !== false) {
49 echo "resign";
50 } else {
51 echo $result;