Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- function getEval($fenPosition, $movetime, $uciEnginePath = "/Volumes/Extra\ Stuff/Dev/ChessDatabase/stockfish-3-mac/Mac/stockfish-3-32"){
- $descriptorspec = array(
- 0 => array("pipe", "r"), // stdin is a pipe that the child will read from
- 1 => array("pipe", "w"), // stdout is a pipe that the child will write to
- 2 => array("file", "/tmp/error-output.txt", "a") // stderr is a file to write to
- );
- $cwd = '/tmp';
- $env = array('some_option' => 'aeiou');
- $process = proc_open("$uciEnginePath", $descriptorspec, $pipes, $cwd, $env);
- if (is_resource($process)) {
- fwrite($pipes[0], "uci\n");
- fwrite($pipes[0], "ucinewgame\n");
- fwrite($pipes[0], "isready\n");
- fwrite($pipes[0], "position fen $fenPosition\n");
- fwrite($pipes[0], "go movetime $movetime\n");
- usleep(1000 * $movetime);
- fclose($pipes[0]);
- preg_match_all("/cp (-?[0-9]*?)(?= )/", stream_get_contents($pipes[1]), $matches);
- preg_match("/-?[0-9]+/", end($matches[0]), $output);
- fclose($pipes[1]);
- }
- return $output[0];
- }
- function getEvalBulk($fenPositions, $movetime, $uciEnginePath = "/Volumes/Extra\ Stuff/Dev/ChessDatabase/stockfish-3-mac/Mac/stockfish-3-32"){
- foreach ($fenPositions as $key => $fenPosition) {
- $descriptorspec = array(
- 0 => array("pipe", "r"), // stdin is a pipe that the child will read from
- 1 => array("pipe", "w"), // stdout is a pipe that the child will write to
- 2 => array("file", "/tmp/error-output.txt", "a") // stderr is a file to write to
- );
- $cwd = '/tmp';
- $env = array('some_option' => 'aeiou');
- $process = proc_open("$uciEnginePath", $descriptorspec, $pipes, $cwd, $env);
- if (is_resource($process)) {
- fwrite($pipes[0], "uci\n");
- fwrite($pipes[0], "ucinewgame\n");
- fwrite($pipes[0], "isready\n");
- fwrite($pipes[0], "position fen $fenPosition\n");
- fwrite($pipes[0], "go movetime $movetime\n");
- usleep(1000 * $movetime);
- fclose($pipes[0]);
- preg_match_all("/cp (-?[0-9]*?)(?= )/", stream_get_contents($pipes[1]), $matches);
- //print_r(end($matches[0]));
- preg_match("/-?[0-9]+/", end($matches[0]), $output);
- //print_r($output);
- $eval[$key] = $output[0];
- fclose($pipes[1]);
- }
- }
- return $eval;
- }
- function extractFENPositions($pgnFile, $stripCounter = TRUE, $pgnExtractPath = "./../pgn-extract/pgn-extract"){
- $fen = shell_exec("$pgnExtractPath --fencomments -s ".$pgnFile);
- preg_match_all("/\{(\n|\r|.)*?\}/", $fen, $fenTemp);
- $matches = array("/\n/", "/({ )|( })/");
- $replaces = array(" ", "");
- $output = array();
- foreach($fenTemp[0] as $value){
- $value = preg_replace($matches, $replaces, $value);
- if($stripCounter == TRUE){
- $value = explode(" ", $value);
- array_pop($value);
- array_pop($value);
- array_pop($value);
- $value = implode(" ", $value);
- }
- $output[] = $value;
- }
- return $output;
- }
- function extractPGNHeader($pgn){
- //$pgn = file_get_contents($pgnFile);
- preg_match_all("/\[.*?\]/", $pgn, $matches);
- $output = array();
- foreach ($matches[0] as $key => $value) {
- preg_match("/(?<=\[).*?(?= )/", $value, $title);
- preg_match("/(?<=\").*?(?=\")/", $value, $data);
- $output[$title[0]] = $data[0];
- }
- return $output;
- }
- function extractPGNMoves($pgn){
- //$pgn = file_get_contents($pgnFile);
- $match = array("/\[.*?\]/", "/\n|\r/", "/\. /", "/\(.*?\)/", "/\{.*\}/");
- $replace = array("", " ", ".", "", "");
- $pgn = explode(" ", trim(preg_replace($match, $replace, $pgn)));
- array_pop($pgn);
- foreach ($pgn as $key => $value) {
- if(empty($value)){
- unset($pgn[$key]);
- }
- }
- $pgn = array_values($pgn);
- return $pgn;
- }
- $pgnExtractPath = "./../pgn-extract/pgn-extract";
- $uciEnginePath = "/Volumes/Extra\ Stuff/Dev/ChessDatabase/stockfish-3-mac/Mac/stockfish-3-32"; //absolute path to uci engine
- $movetime = 500; //thousandths of a second
- /*
- $pgnFile = "test.pgn";
- $pgnHeader = extractPGNHeader($pgnFile);
- $pgnMoves = extractPGNMoves($pgnFile);
- $fenPositions = extractFENPositions($pgnFile, $pgnExtractPath, TRUE);
- $evaluations = getEvalBulk($fenPositions, $movetime, $uciEnginePath);
- */
Advertisement
Add Comment
Please, Sign In to add comment