GNU chess and PHP

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
danielrs1
Forum Commoner
Posts: 29
Joined: Wed Jun 24, 2009 5:30 pm

GNU chess and PHP

Post 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
User avatar
Weirdan
Moderator
Posts: 5978
Joined: Mon Nov 03, 2003 6:13 pm
Location: Odessa, Ukraine

Re: GNU chess and PHP

Post 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.
Eric!
DevNet Resident
Posts: 1146
Joined: Sun Jun 14, 2009 3:13 pm

Re: GNU chess and PHP

Post 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.
danielrs1
Forum Commoner
Posts: 29
Joined: Wed Jun 24, 2009 5:30 pm

Re: GNU chess and PHP

Post 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.
danielrs1
Forum Commoner
Posts: 29
Joined: Wed Jun 24, 2009 5:30 pm

Re: GNU chess and PHP

Post 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) ;
}
 
User avatar
Weirdan
Moderator
Posts: 5978
Joined: Mon Nov 03, 2003 6:13 pm
Location: Odessa, Ukraine

Re: GNU chess and PHP

Post 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) ;
 
danielrs1
Forum Commoner
Posts: 29
Joined: Wed Jun 24, 2009 5:30 pm

Re: GNU chess and PHP

Post by danielrs1 »

It worked :!:

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

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