Page 1 of 1

GNU chess and PHP

Posted: Wed Jun 24, 2009 5:36 pm
by danielrs1
I'm developing a chess app using PHP and I would like to use GNU Chess as an engine.

I have some experience with PHP but I never used proc_open(). Whatever I do, it always hangs when I call gnuchess.

I need help :banghead: Just give me a piece of PHP code that interacts with GNU chess and I can do all my work.

Thanks for reading this

Re: GNU chess and PHP

Posted: Wed Jun 24, 2009 5:54 pm
by Weirdan
No, these forums work the other way around: you post what you have written so far and tell us what is not working - then we may suggest some improvements and fixes.

Re: GNU chess and PHP

Posted: Wed Jun 24, 2009 10:51 pm
by Eric!
Take a look at examples for proc_open on the web to get started on your code. I think that will work better for you.

http://cr.php.net/proc_open

I found a php chess server with code. You might get some gui ideas from it (no engine) http://lgames.sourceforge.net/index.php?project=OCC

However in general around here: no code, no service.

Re: GNU chess and PHP

Posted: Thu Jun 25, 2009 4:27 am
by danielrs1
I don't have any code right now, I want to start from the engine.
This app is going to run inside eyeOS (http://eyeos.org]) and will be released under the AGPL license (I'll put everything on eyeos-apps.org).

In the website: http://www.molecularsciences.org/PHP/pr ... d_examples, I've found the code:

Code: Select all

$descriptorspec = array(
0 => array("pipe","r"),
1 => array("pipe","w"),
2 => array("file","./error.log","a")
) ;
 
// define current working directory where files would be stored
$cwd = './' ;
// open process /bin/sh
$process = proc_open('/bin/sh', $descriptorspec, $pipes, $cwd) ;
 
if (is_resource($process)) {
 
  // anatomy of $pipes: 0 => stdin, 1 => stdout, 2 => error log
  fwrite($pipes[0], 'cal -3') ;
  fclose($pipes[0]) ;
 
  // print pipe output
  echo stream_get_contents($pipes[1]) ;
 
  // close pipe
  fclose($pipes[1]) ;
 
  // all pipes must be closed before calling proc_close. 
  // proc_close() to avoid deadlock
  proc_close($process) ;
}
 
This code worked with no problems on my computer, then I modified it to execute gnuchess instead of sh. My modified code is:

Code: Select all

<?php
 
$descriptorspec = array(
0 => array("pipe","r"),
1 => array("pipe","w"),
2 => array("file","./error.log","a")
) ;
 
// define current working directory where files would be stored
$cwd = './' ;
// open process /bin/sh
$process = proc_open('/usr/games/gnuchess', $descriptorspec, $pipes, $cwd) ;
 
if (is_resource($process)) {
 
  // anatomy of $pipes: 0 => stdin, 1 => stdout, 2 => error log
  fwrite($pipes[0], 'exit') ;
  fclose($pipes[0]) ;
 
  // print pipe output
  echo stream_get_contents($pipes[1]) ;
 
  // close pipe
  fclose($pipes[1]) ;
 
  // all pipes must be closed before calling proc_close. 
  // proc_close() to avoid deadlock
  proc_close($process) ;
}
It worked, now I can start working on my Chess app.

Re: GNU chess and PHP

Posted: Thu Jun 25, 2009 4:44 am
by danielrs1
Seams that I was wrong, my problem is not yet solved. That code works, but it hangs if I try input a move. The code I'm using now is:

Code: Select all

<?php
 
$descriptorspec = array(
0 => array("pipe","r"),
1 => array("pipe","w"),
2 => array("file","./error.log","a")
) ;
 
// define current working directory where files would be stored
$cwd = './' ;
// open process /bin/sh
$process = proc_open('/usr/games/gnuchess', $descriptorspec, $pipes, $cwd) ;
 
if (is_resource($process)) {
 
  // anatomy of $pipes: 0 => stdin, 1 => stdout, 2 => error log
  fwrite($pipes[0], 'e2e4') ;
  fwrite($pipes[0], 'exit') ;
  fclose($pipes[0]) ;
 
  // print pipe output
  echo stream_get_contents($pipes[1]) ;
 
  // close pipe
  fclose($pipes[1]) ;
 
  // all pipes must be closed before calling proc_close. 
  // proc_close() to avoid deadlock
  proc_close($process) ;
}
 

Re: GNU chess and PHP

Posted: Thu Jun 25, 2009 12:28 pm
by Weirdan
I guess gnuchess process is waiting for you to send a newline character after the move:

Code: Select all

 
//....
fwrite($pipes[0], 'e2e4' . PHP_EOL) ;
 

Re: GNU chess and PHP

Posted: Thu Jun 25, 2009 12:34 pm
by danielrs1
It worked :!:

Thanks a lot :drunk:
You're the best :bow:

I think I'll stay around to help other users. 8)