Need some verifies/helps with my MacOS chess engine

Discussion of chess software programming and technical issues.

Moderators: hgm, Rebel, chrisw

User avatar
phhnguyen
Posts: 1437
Joined: Wed Apr 21, 2010 4:58 am
Location: Australia
Full name: Nguyen Hong Pham

Need some verifies/helps with my MacOS chess engine

Post by phhnguyen »

I have modified and compiled my chess engine from Windows to MacOS, using XCode. It (the engine) can run well alone but keeps crashing under XBoard (for Mac) after making successfully a move. Other chess engines which are released with XBoard still run well.

To find out the problem, I have created a super simple chess engine as the following code, then compile by XCode 8.1. I (human) play on the white side and it plays on the black side. For the 1st move, everything good: I play the first move (f2f4), it reply a move (b8c6) - no crash. However, when I played the second one (g1f3), it keeps crashing after receiving my 2nd move.

Can someone help:
- verify if the bellow engine can run well (survive after making over 3 moves) on your systems with your compilers
- find out what wrong with the code and/or my system

Thanks a lot.

Code: Select all

//
//  main.cpp
//  myengine
//

#include <iostream>

int main&#40;int argc, const char * argv&#91;&#93;) &#123;

    std&#58;&#58;string line;
    while &#40;getline&#40;std&#58;&#58;cin, line&#41;) &#123;

        if &#40;line.empty&#40;)) &#123;
            continue;
        &#125;

        char command&#91;256&#93;;
        sscanf&#40;line.c_str&#40;), "%s", command&#41;;

        if (!strcmp&#40;command, "xboard")) &#123;
            printf&#40;"\nfeature myname=\"MyEngine\"\n");
            fflush&#40;stdout&#41;;
            continue;
        &#125;

        if (!strcmp&#40;command, "go") || &#40;strlen&#40;command&#41;>=4 && isalpha&#40;command&#91;0&#93;) && isdigit&#40;command&#91;1&#93;) && isalpha&#40;command&#91;2&#93;) && isdigit&#40;command&#91;3&#93;))) &#123;
            static const char* moveList&#91;&#93; = &#123; "b8c6", "c6b8" &#125;;
            static int moveIdx = 0;

            const char* move = moveList&#91;moveIdx % 2&#93;;
            moveIdx ++;

            printf&#40;"move %s\n", move&#41;;
            fflush&#40;stdout&#41;;
        &#125;
    &#125;
    
    return 0;
&#125;
Debug file:

Code: Select all

locale = en_AU.UTF-8
recognized 'normal' (-1&#41; as variant normal
recognized 'normal' (-1&#41; as variant normal
recognized 'normal' (-1&#41; as variant normal
shuffleOpenings = 0
shuffleOpenings = 0
Version&#58; xboard 4.9.1 + myengine ()
Reset&#40;1, 0&#41; from gameMode 0
recognized 'normal' (-1&#41; as variant normal
GameEnds&#40;0, &#40;null&#41;, 2&#41;
shuffleOpenings = 0
StartChildProcess &#40;dir="/Applications/XBoard.app/Contents/Resources/share/xboard/../../bin/fairymax") /Users/Pham/myengine
888 >first &#58; xboard
protover 2
896 <first &#58; 
896 <first &#58; feature myname="MyEngine"
896 >first &#58; accepted myname
10893 >first &#58; new
random
10893 >first &#58; level 40 3 0
10893 >first &#58; post
10893 >first &#58; hard
Impossible move , type = 0
nps&#58; w=-1, b=-1
nps&#58; w=-1, b=-1
15506 >first &#58; time 18000
15506 >first &#58; otim 18000
book hit = &#40;NULL&#41;
15506 >first &#58; f2f4
15516 <first &#58; move b8c6
Interrupting first
19475 >first &#58; time 17997
19475 >first &#58; otim 17604
book hit = &#40;NULL&#41;
19475 >first &#58; g1f3
GameEnds&#40;27, Error&#58; first chess program (/Users/Pham/myengine/myengine&#41; exited unexpectedly, 2&#41;
Interrupting first
22784 >first &#58; result 0-1 &#123;Error&#58; first chess program (/Users/Pham/myengine/myengine&#41; exited unexpectedly&#125;
22787 >first &#58; force
22789 >first &#58; quit

User avatar
hgm
Posts: 27808
Joined: Fri Mar 10, 2006 10:06 am
Location: Amsterdam
Full name: H G Muller

Re: Need some verifies/helps with my MacOS chess engine

Post by hgm »

Have you made the engine insensitive for SIGINT, or sent feature sigint=0 at startup? XBoard sends an interrupt signal to engines just before it sends them a move on Linux, and I suppose also on Mac. (Presumably so the engine could use it to stop pondering without polling for input.)
User avatar
phhnguyen
Posts: 1437
Joined: Wed Apr 21, 2010 4:58 am
Location: Australia
Full name: Nguyen Hong Pham

Re: Need some verifies/helps with my MacOS chess engine

Post by phhnguyen »

Wow, another great feature of XBoard I have missed. After adding "feature sigint=0" my engine can work well with XBoard. Thank you!

BTW, how can I catch and work with SIGINT from XBoard? Thanks again.
User avatar
hgm
Posts: 27808
Joined: Fri Mar 10, 2006 10:06 am
Location: Amsterdam
Full name: H G Muller

Re: Need some verifies/helps with my MacOS chess engine

Post by hgm »

During engine initialization you can call

Code: Select all

signal&#40;SIGINT, &Handler&#41;;
and define a function

Code: Select all

void Handler&#40;)
&#123;
  // do whatever needs to be done when the signal is received
  if&#40;!pondering&#41; return; // ignore if not pondering
  inputLine = ReadLine&#40;); // read the promised input
  if&#40;sscanf&#40;inputLine, "usermove %s", inputMove&#41; &&
     !strcmp&#40;inputMove, ponderMove&#41;) &#123;
    pondering = FALSE; // ponder hit, continue as timed search
    inputLine&#91;0&#93; = 0; // command now treated, so erase it from input buffer
    return;
  &#125;
  abortFlag = TRUE; // ponder miss, or other command&#58; abort ponder search
&#125;
Under Windows this would not work, however, because Windows does not support any inter-process signals.

(Note that the code above just is a simplified example to illustrate the idea. In practice the input move would be preceded by 'time' and 'otim' commands, which you would have to read away too before you will be able to see the move, and determine if it was a ponder hit or not.)
User avatar
phhnguyen
Posts: 1437
Joined: Wed Apr 21, 2010 4:58 am
Location: Australia
Full name: Nguyen Hong Pham

Re: Need some verifies/helps with my MacOS chess engine

Post by phhnguyen »

Thanks a lot. I got the idea now!

So the code (for catching SIGINT) will be useful for engines which run without a thread to listen the input, right?

My engine always has a thread to listen the input, beside others for chess computing thus the SIGINT seems to be useless for it.
User avatar
hgm
Posts: 27808
Joined: Fri Mar 10, 2006 10:06 am
Location: Amsterdam
Full name: H G Muller

Re: Need some verifies/helps with my MacOS chess engine

Post by hgm »

Indeed, using a separate input thread is another way to instantaneous respond to arrival of input, without any polling.

The SIGINT mechanism dates from before Unix supported multi-threading. In fact it sort of simulates multi-threading, where the engine itself is responsible for scheduling the threads, and the Handler() routine corresponds to the input thread, woken up only when input arrives.