<?php ini_set('include_path', ini_get('include_path') . PATH_SEPARATOR . realpath('PEAR')); require_once 'Connections/conn.php'; require_once 'Games/Chess/Standard.php'; include_once 'functions.php'; include_once 'Chess.class.php'; $standard = new Games_Chess_Standard(); $fen = !empty($_GET['fen']) ? $_GET['fen'] : ''; $standard->resetGame($fen); $toMove = $standard->toMove(); $renderFen = $standard->renderFen(); if (empty($fen)) { //start of the game, find the first move: $chess = new Chess(); $move = $chess->find($renderFen); $from = substr($move, 0, 2); $to = substr($move, 2, 2); $move2 = $standard->_convertSquareToSAN($from, $to); $standard->moveSquare($from, $to); $renderFen = $standard->renderFen(); mysql_select_db($database_conn, $conn); $query_rsView = sprintf("SELECT * FROM games WHERE move = %s and fen = %s and moveby = %s", GetSQLValueString($move2, "text"), GetSQLValueString($renderFen, "text"), GetSQLValueString($toMove, "text")); $rsView = mysql_query($query_rsView, $conn) or die(mysql_error()); $totalRows_rsView = mysql_num_rows($rsView); if ($totalRows_rsView == 0) { $query = sprintf("INSERT INTO games SET pid = 0, move = %s, fen = %s, moveby = %s, result = ''", GetSQLValueString($move2, "text"), GetSQLValueString($renderFen, "text"), GetSQLValueString($toMove, "text")); $rs = mysql_query($query, $conn) or die(mysql_error()); } } echo 'done';
<?php require_once 'Connections/conn.php'; ini_set('include_path', ini_get('include_path') . PATH_SEPARATOR . realpath('PEAR')); require_once 'Games/Chess/Standard.php'; include_once 'functions.php'; include_once 'Chess.class.php'; $chess = new Chess(); $rs = $chess->findpos('finalgames_B'); if ($rs[0] == 0) { echo 'no moves to play'; exit; } $fen = !empty($rs[1]['fenpost']) ? $rs[1]['fenpost'] : NULL; $standard = new Games_Chess_Standard(); $standard->resetGame($fen); $toMove = $standard->toMove(); $fen = $standard->renderFen(); echo 'fen: ' . $fen . '<br>'; $moveby = 'W'; if ($toMove == 'W') { $moveby = 'B'; } pr($rs); if ($toMove == 'B') { $legalMoves = getlegalmoves($standard, $fen); pr($legalMoves); if (empty($legalMoves['success'])) { $result = $chess->findfen($fen, 'finalgames_B'); if ($result[0] == 0) { $move = $chess->find($fen);
if ($black == '') { break; } $standard->moveSAN($black); //pr($standard->getMoveList()); } } $fen = $standard->renderFen(); $moves = $standard->getMoveList(); $toMove = $standard->toMove(); $moveby = 'W'; if ($toMove == 'W') { $moveby = 'B'; } //check db if fen occurs: $chess = new Chess(); $result = $chess->findfen($fen, 'finalgames_W'); $legalMoves = array(); if ($result[0] == 0) { $move = $chess->find($fen); $from = substr($move, 0, 2); $to = substr($move, 2, 2); $move2 = $standard->_convertSquareToSAN($from, $to); $show = $move2; $standard->moveSquare($from, $to); $renderFen = $standard->renderFen(); $result = ''; $process = 0; $legalMoves = getlegalmoves($standard, $renderFen); if (!empty($legalMoves['success']) && $legalMoves['success'] == 1) { $result = $legalMoves['result'];
function process($standard, $fromMove, $toMove, $legalMovesOriginal, $row_rsView, $fen) { if ($fromMove == 'B') { $table = 'games'; } else { $table = 'gamesblack'; } global $database_conn, $conn; if ($toMove == $fromMove) { foreach ($legalMovesOriginal as $k => $v) { $renderFen = $v['fen']; $move2 = $v['move']; //checking result $result = ''; $process = 0; $standard->moveSAN($move2); $legalMoves = getlegalmoves($standard, $renderFen); if (!empty($legalMoves['success']) && $legalMoves['success'] == 1) { $result = $legalMoves['result']; $process = 1; } //end if $standard->resetGame($fen); //end checking result mysql_select_db($database_conn, $conn); echo $query = sprintf("SELECT * FROM {$table} WHERE pid = %s and move = %s and fen = %s and moveby = %s", GetSQLValueString($row_rsView['id'], "int"), GetSQLValueString($move2, "text"), GetSQLValueString($renderFen, "text"), GetSQLValueString($toMove, "text")); echo '<br>'; $rs = mysql_query($query, $conn) or die(mysql_error()); $totalRows = mysql_num_rows($rs); if ($totalRows == 0) { echo $query = sprintf("INSERT INTO {$table} SET pid = %s, move = %s, fen = %s, moveby = %s, result = %s, process = %s", GetSQLValueString($row_rsView['id'], "int"), GetSQLValueString($move2, "text"), GetSQLValueString($renderFen, "text"), GetSQLValueString($toMove, "text"), GetSQLValueString($result, "text"), GetSQLValueString($process, "int")); echo '<br>'; $rs = mysql_query($query, $conn) or die(mysql_error()); } } $query = sprintf("UPDATE {$table} SET process = 1 WHERE id = %s", GetSQLValueString($row_rsView['id'], "int")); $rs = mysql_query($query, $conn) or die(mysql_error()); } else { mysql_select_db($database_conn, $conn); echo $query = sprintf("SELECT * FROM {$table} WHERE pid = %s", GetSQLValueString($row_rsView['id'], "int")); echo '<br>'; $rs = mysql_query($query, $conn) or die(mysql_error()); $totalRows = mysql_num_rows($rs); if ($totalRows == 0) { $chess = new Chess(); $move = $chess->find($row_rsView['fen']); $from = substr($move, 0, 2); $to = substr($move, 2, 2); $move2 = $standard->_convertSquareToSAN($from, $to); $standard->moveSquare($from, $to); $renderFen = $standard->renderFen(); //checking result $result = ''; $process = 0; $legalMoves = getlegalmoves($standard, $renderFen); if (!empty($legalMoves['success']) && $legalMoves['success'] == 1) { $result = $legalMoves['result']; $process = 1; } //end if $standard->resetGame($fen); //end checking result echo $query = sprintf("INSERT INTO {$table} SET pid = %s, move = %s, fen = %s, moveby = %s, result = %s, process = %s", GetSQLValueString($row_rsView['id'], "int"), GetSQLValueString($move2, "text"), GetSQLValueString($renderFen, "text"), GetSQLValueString($toMove, "text"), GetSQLValueString($result, "text"), GetSQLValueString($process, "int")); echo '<br>'; $rs = mysql_query($query, $conn) or die(mysql_error()); } $query = sprintf("UPDATE {$table} SET process = 1 WHERE id = %s", GetSQLValueString($row_rsView['id'], "int")); $rs = mysql_query($query, $conn) or die(mysql_error()); } return $standard; }
<?php include 'Chess.class.php'; $Chess = new Chess(); echo $Chess->find('r1bq4/1p4kp/3p1n2/5pB1/p1pQ4/8/1P4PP/4RRK1 w - - 0 1'); exit; echo '<pre>'; file_put_contents('/Users/naveen/Downloads/stockfish-231-mac/Mac/fen.txt', 'r1bq4/1p4kp/3p1n2/5pB1/p1pQ4/8/1P4PP/4RRK1 w - - 0 1'); exec("/Users/naveen/Downloads/stockfish-231-mac/Mac/stockfish-231-64 bench 128 1 20 /Users/naveen/Downloads/stockfish-231-mac/Mac/fen.txt depth\n", $output, $returnvar); print_r($output); echo $subject = $output[count($output) - 1]; echo '<br>'; $match = preg_match('/^bestmove (.*) ponder (.*)$/siu', $subject, $matches); $move = !empty($matches[1]) ? $matches[1] : 'Not Found'; echo $move; exit; $descriptorspec = array(0 => array("pipe", "r"), 1 => array("pipe", "w"), 2 => array("file", "/tmp/error-output.txt", "a")); $cwd = '/Users/naveen/Downloads/stockfish-231-mac/Mac/'; $env = array('some_option' => 'aeiou'); $process = proc_open('stockfish-231-64', $descriptorspec, $pipes, $cwd, $env); if (is_resource($process)) { // $pipes now looks like this: // 0 => writeable handle connected to child stdin // 1 => readable handle connected to child stdout // Any error output will be appended to /tmp/error-output.txt fwrite($pipes[0], "uci\nisready\nposition startpos moves d2d4 e7e6\ngo depth 20\n"); fclose($pipes[0]); $x = stream_get_contents($pipes[1]); fclose($pipes[1]); fwrite('/Users/naveen/Downloads/stockfish-231-mac/Mac/tmp.txt', $x); print_r($x);
$refresh = isset($_GET['refresh']) ? true : false; if ($data = $Cache_Lite->get($id) && !$refresh) { // cache hit ! $data = json_decode($data, 1); if (empty($side)) { $side = getside($data['toMove']); } $data['process'] = 0; replacemove($side, $data); $time2 = microtime(true); $data['time'] = $time2 - $time1 . ' secs'; $data['id'] = $id; $data = json_encode($data); } else { // page has to be (re)constructed in $data $chess = new Chess(); $data = $chess->process($standard, $fen); if (empty($side)) { $side = getside($data['toMove']); } $data['process'] = 0; replacemove($side, $data); $time2 = microtime(true); $data['time'] = $time2 - $time1 . ' secs'; $data['id'] = $id; $data = json_encode($data); $Cache_Lite->save($data); } if (isset($_GET['jsoncallback'])) { echo $_GET['jsoncallback'] . '(' . $data . ');'; } else {
if ($totalRows == 0) { echo $query = sprintf("INSERT INTO gamespos SET pid = %s, move = %s, fen = %s, moveby = %s, result = %s, process = %s", GetSQLValueString($row_rsView['id'], "int"), GetSQLValueString($move2, "text"), GetSQLValueString($renderFen, "text"), GetSQLValueString($toMove, "text"), GetSQLValueString($result, "text"), GetSQLValueString($process, "int")); echo '<br>'; $rs = mysql_query($query, $conn) or die(mysql_error()); } } $query = sprintf("UPDATE gamespos SET process = 1 WHERE id = %s", GetSQLValueString($row_rsView['id'], "int")); $rs = mysql_query($query, $conn) or die(mysql_error()); } else { mysql_select_db($database_conn, $conn); echo $query = sprintf("SELECT * FROM gamespos WHERE pid = %s", GetSQLValueString($row_rsView['id'], "int")); echo '<br>'; $rs = mysql_query($query, $conn) or die(mysql_error()); $totalRows = mysql_num_rows($rs); if ($totalRows == 0) { $chess = new Chess(); $move = $chess->find($row_rsView['fen']); $from = substr($move, 0, 2); $to = substr($move, 2, 2); $move2 = $standard->_convertSquareToSAN($from, $to); $standard->moveSquare($from, $to); $renderFen = $standard->renderFen(); //checking result $result = ''; $process = 0; $legalMoves = getlegalmoves($standard, $renderFen); if (!empty($legalMoves['success']) && $legalMoves['success'] == 1) { $result = $legalMoves['result']; $process = 1; } //end if