some crafty questions

Discussion of chess software programming and technical issues.

Moderators: hgm, Rebel, chrisw

adams161
Posts: 626
Joined: Sun May 13, 2007 9:55 pm
Location: Bay Area, CA USA
Full name: Mike Adams

some crafty questions

Post by adams161 »

Hi.

I'm looking into seeing if I can get crafty to work in an iPhone app. My theory is if I start crafty's main function on a thread, which then causes it to go look for input in the standard input, I can just use on another thread printf and scanf/read to communicate with it.

A few issues. One and this is big does anyone know the terms of use / legality of using crafty? Is it enough to give credit that you use crafty in the app and publish all the minor changes I have to make to make it work? I read a few other rules that i'd respect that dont seem to apply to me such as my use could not lead to it entering a chess engine tournament.

The second is I had to rename it's main as mainiphone() because you cant have two main methods in an iOS app, and when I call mainiphone with no program arguments I get this error:

unable to open book file [./book.bin].
book is disabled
unable to open book file [./books.bin].
ERROR, unable to open game history file, exiting

I don't want to play any games or use any books but just do some analysis. I looked at the crafty.doc and was not finding any way to surpress these.

The final problem is it really did exit my whole program probably cause it called something like a c function exit(); I need a way to make it exit without exiting the app, I just want the thread to die and memory to free. Seems I might be able when I get to the point that first it starts, and second it takes input figure out how to do that but just a heads up it's an issue if any ideas. I figure I can alter where it reads quit\n and guide it to not exit() but return from the function and reach the end of main iphone and exit that thread.

Mike
bob
Posts: 20943
Joined: Mon Feb 27, 2006 7:30 pm
Location: Birmingham, AL

Re: some crafty questions

Post by bob »

adams161 wrote:Hi.

I'm looking into seeing if I can get crafty to work in an iPhone app. My theory is if I start crafty's main function on a thread, which then causes it to go look for input in the standard input, I can just use on another thread printf and scanf/read to communicate with it.

A few issues. One and this is big does anyone know the terms of use / legality of using crafty? Is it enough to give credit that you use crafty in the app and publish all the minor changes I have to make to make it work? I read a few other rules that i'd respect that dont seem to apply to me such as my use could not lead to it entering a chess engine tournament.

The second is I had to rename it's main as mainiphone() because you cant have two main methods in an iOS app, and when I call mainiphone with no program arguments I get this error:

unable to open book file [./book.bin].
book is disabled
unable to open book file [./books.bin].
ERROR, unable to open game history file, exiting

I don't want to play any games or use any books but just do some analysis. I looked at the crafty.doc and was not finding any way to surpress these.

The final problem is it really did exit my whole program probably cause it called something like a c function exit(); I need a way to make it exit without exiting the app, I just want the thread to die and memory to free. Seems I might be able when I get to the point that first it starts, and second it takes input figure out how to do that but just a heads up it's an issue if any ideas. I figure I can alter where it reads quit\n and guide it to not exit() but return from the function and reach the end of main iphone and exit that thread.

Mike
What you are doing is fine. And yes, communicating with Crafty via stdin/stdout will work just fine, just as it does with xboard/winboard.

The game history file is a bit of a problem as it needs that (it is very small and is created when the game starts) to save the game moves. It then uses that file if you try to back up in the game using the reset command. You could probably eliminate all references to that if it is not possible to create such a file under iOS (I would hope that is not true however). But you will give up the ability to move backward and forward through the game.

The usual exit strategy is to call CraftyExit() which handles book learning and then does an exit(); This tiny procedure is in utility.c
adams161
Posts: 626
Joined: Sun May 13, 2007 9:55 pm
Location: Bay Area, CA USA
Full name: Mike Adams

Re: some crafty questions

Post by adams161 »

i disabled that and it gets to a point where it prints

White(1):

then it exits. If i disable craftyExit() by placing return; at top it then loops and prints White(1):White(1):White(1):White(1): etc over and over.

CraftyExit could be a problem because i need it to only exit the threads it lives on not the main iPhone thread which shuts down the app but i'd love to simply see it take some commands as a proof of concept point for now.

Mike
adams161
Posts: 626
Joined: Sun May 13, 2007 9:55 pm
Location: Bay Area, CA USA
Full name: Mike Adams

Re: some crafty questions

Post by adams161 »

i did this in init

/*
id = InitializeGetLogID();
sprintf(log_filename, "%s/log.%03d", log_path, id);
sprintf(history_filename, "%s/game.%03d", log_path, id);
log_file = fopen(log_filename, "w");
history_file = fopen(history_filename, "w+");
if (!history_file) {
printf("ERROR, unable to open game history file, exiting\n");
CraftyExit(1);
}
*/

it looks like some other references to history file are already disabled if the iPhone preprocessor is set.

Mike
adams161
Posts: 626
Joined: Sun May 13, 2007 9:55 pm
Location: Bay Area, CA USA
Full name: Mike Adams

Re: some crafty questions

Post by adams161 »

apparently it's hitting this

else if (OptionMatch("end", *args) || OptionMatch("quit", *args)) {
abort_search = 1;
quit = 1;
last_search_value =
(crafty_is_white) ? last_search_value : -last_search_value;
if (moves_out_of_book)
LearnBook();
if (thinking || pondering)
return (1);
printf("args indicated quit");
CraftyExit(0);
}
adams161
Posts: 626
Joined: Sun May 13, 2007 9:55 pm
Location: Bay Area, CA USA
Full name: Mike Adams

Re: some crafty questions

Post by adams161 »

well more specifically in the iPhone app it looks like there is something wrong with reading the standard input. this code is finding readstat < 0 or something

readstat = Read(1, buffer);
if (log_file)
fprintf(log_file, "%s(%d): %s\n", SideToMove(wtm), move_number,
buffer);
if (readstat < 0 && input_stream == stdin) {
strcpy(buffer, "end");
(void) Option(tree);
}
bob
Posts: 20943
Joined: Mon Feb 27, 2006 7:30 pm
Location: Birmingham, AL

Re: some crafty questions

Post by bob »

adams161 wrote:i disabled that and it gets to a point where it prints

White(1):

then it exits. If i disable craftyExit() by placing return; at top it then loops and prints White(1):White(1):White(1):White(1): etc over and over.

CraftyExit could be a problem because i need it to only exit the threads it lives on not the main iPhone thread which shuts down the app but i'd love to simply see it take some commands as a proof of concept point for now.

Mike
Just disabling opening the file is not enough. You need to find every place where that file is read or written and comment those lines out as well, otherwise they will crash and burn when reading/writing a file descriptor that is not open.

The normal way to exit a thread is to do a return. Edit utility.c and change that exit() in CraftyExit() to a return. And are you REALLY using threads here or are you actually using fork() to create a real process, which is a bit different.
bob
Posts: 20943
Joined: Mon Feb 27, 2006 7:30 pm
Location: Birmingham, AL

Re: some crafty questions

Post by bob »

adams161 wrote:apparently it's hitting this

else if (OptionMatch("end", *args) || OptionMatch("quit", *args)) {
abort_search = 1;
quit = 1;
last_search_value =
(crafty_is_white) ? last_search_value : -last_search_value;
if (moves_out_of_book)
LearnBook();
if (thinking || pondering)
return (1);
printf("args indicated quit");
CraftyExit(0);
}
So it is being sent a "quit" or "end" command? That will certainly cause it to exit.
adams161
Posts: 626
Joined: Sun May 13, 2007 9:55 pm
Location: Bay Area, CA USA
Full name: Mike Adams

Re: some crafty questions

Post by adams161 »

no the program appears to be writing "End" as a result of this line failing:

readstat = Read(1, buffer);
readstat == -1

that happens in main.c in the main method after the do loop. Is the default standard input and output of whatever i print to the console not really working? Maybe i need to define the standard input and output before i call crafty.

mike
adams161
Posts: 626
Joined: Sun May 13, 2007 9:55 pm
Location: Bay Area, CA USA
Full name: Mike Adams

Re: some crafty questions

Post by adams161 »

yea i've been going through and commenting out every mention of history file name. that may take more work but i get the idea. lot of lines like fopen then fseek.