Illegal Move handling

Discussion of chess software programming and technical issues.

Moderators: hgm, Rebel, chrisw

whittenizer
Posts: 85
Joined: Sun May 29, 2011 11:56 pm
Location: San Diego

Illegal Move handling

Post by whittenizer »

Hi there,

I have Stockfish working well with a silverlight project. If an illegal move gets entered somehow the program straight up crashes even if I have a try catch. In the C++ code there are asserts, like is_ok, move_isok(I think thats what its called). Anyways, I wasn;t sure what to do so in the C++ code if the move happens to be MOVE_NULL I simply do a cout << "Invalid move" just before the asserts and exit out right away. My UI can handle the Invalid Move text coming back. Other chess GUI's probably handle the asserts other ways so the program doesnt crash but I'm not sure how it's done.

Thanks much for any ideas other than what I've done.

David
Sven
Posts: 4052
Joined: Thu May 15, 2008 9:57 pm
Location: Berlin, Germany
Full name: Sven Schüle

Re: Illegal Move handling

Post by Sven »

whittenizer wrote:Hi there,

I have Stockfish working well with a silverlight project. If an illegal move gets entered somehow the program straight up crashes even if I have a try catch. In the C++ code there are asserts, like is_ok, move_isok(I think thats what its called). Anyways, I wasn;t sure what to do so in the C++ code if the move happens to be MOVE_NULL I simply do a cout << "Invalid move" just before the asserts and exit out right away. My UI can handle the Invalid Move text coming back. Other chess GUI's probably handle the asserts other ways so the program doesnt crash but I'm not sure how it's done.

Thanks much for any ideas other than what I've done.
The assert's are only active in a debug version. Also successfully using try/catch requires to compile with exception handling enabled, which is not the default for most chess programs.

I guess you are talking about a "position fen XXX moves YYY" command where YYY contains any illegal move. Stockfish seems to rely on the GUI to provide legal moves only. In uci.cpp it has:

Code: Select all

pos.do_setup_move&#40;move_from_uci&#40;pos, token&#41;);
which passes the result of move_from_uci() (which can be MOVE_NONE in case of an illegal move) directly to do_setup_move() (which requires the move not to be MOVE_NONE). I think this looks slightly inconsistent but may also be part of fundamental philosophy.

To solve your problem I would perhaps try something like this in uci.cpp, at the end of function set_position():

Code: Select all

while &#40;up >> token&#41;
&#123;
    Move m = move_from_uci&#40;pos, token&#41;;
    if &#40;m != MOVE_NONE&#41;
    &#123;
        pos.do_setup_move&#40;m&#41;;
    &#125;
    else return;
&#125;
Printing an error message is not the usual way to go in the UCI protocol, so I would not do it but just ignore the illegal move and all subsequent moves.

Sven
mcostalba
Posts: 2684
Joined: Sat Jun 14, 2008 9:17 pm

Re: Illegal Move handling

Post by mcostalba »

Sven Schüle wrote: I think this looks slightly inconsistent but may also be part of fundamental philosophy.
The fundamental philosphy seems a bit changhed in the latest version ;-)

Code: Select all

// Parse move list &#40;if any&#41;
while &#40;is >> token && &#40;m = move_from_uci&#40;pos, token&#41;) != MOVE_NONE&#41;
&#123;
    pos.do_move&#40;m, *SetupState&#41;;

whittenizer
Posts: 85
Joined: Sun May 29, 2011 11:56 pm
Location: San Diego

Re: Illegal Move handling

Post by whittenizer »

Hi there,

I actually did something very similar. In the while loop I check for null move and simply do a cout for my UI and then return.

Awesome. Thanks for the info
whittenizer
Posts: 85
Joined: Sun May 29, 2011 11:56 pm
Location: San Diego

Re: Illegal Move handling

Post by whittenizer »

HI,

This is pretty much what I've done but added the cout for my UI.

Thanks so much for the reply.

David
whittenizer
Posts: 85
Joined: Sun May 29, 2011 11:56 pm
Location: San Diego

Re: Illegal Move handling

Post by whittenizer »

Just wanted to add that my Silverlight GUI is not UCI compliant. It's able to handle most interactions from the engine. As I progress I'll make it fully UCI compliant. For now I'm finally able to interact with the engine in some form.

Thanks for the ideas.

David
Sven
Posts: 4052
Joined: Thu May 15, 2008 9:57 pm
Location: Berlin, Germany
Full name: Sven Schüle

Re: Illegal Move handling

Post by Sven »

mcostalba wrote:
Sven Schüle wrote: I think this looks slightly inconsistent but may also be part of fundamental philosophy.
The fundamental philosphy seems a bit changhed in the latest version ;-)

Code: Select all

// Parse move list &#40;if any&#41;
while &#40;is >> token && &#40;m = move_from_uci&#40;pos, token&#41;) != MOVE_NONE&#41;
&#123;
    pos.do_move&#40;m, *SetupState&#41;;

I looked at 2.1. Of course I should have used the most recent version (now I downloaded 2.2.1), but the behaviour reported by David matched what I saw so I was lazy and assumed he had the latest SF which would the same code. I know I should not "assume" ...

Thanks also for your fundamental change ;-)

Sven
Sven
Posts: 4052
Joined: Thu May 15, 2008 9:57 pm
Location: Berlin, Germany
Full name: Sven Schüle

Re: Illegal Move handling

Post by Sven »

whittenizer wrote:I actually did something very similar. In the while loop I check for null move and simply do a cout for my UI and then return.
Note that "null move" (MOVE_NULL) and "no move" (MOVE_NONE) is not the same, look at "types.h". In case of illegal move handling of Stockfish the relevant constant is MOVE_NONE.

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

Re: Illegal Move handling

Post by hgm »

Also known as MOVE_NONCOMPLIANT. :wink:
whittenizer
Posts: 85
Joined: Sun May 29, 2011 11:56 pm
Location: San Diego

Re: Illegal Move handling

Post by whittenizer »

Yes, I made a typo. Imeant MOVE_NONE.

Thanks much,

David