allowing invalid positions

Discussion of chess software programming and technical issues.

Moderators: hgm, Rebel, chrisw

ericlangedijk
Posts: 15
Joined: Thu Aug 08, 2013 5:13 pm

allowing invalid positions

Post by ericlangedijk »

Some positions are not possible in chess.
For example check by a pawn AND a knight.

Code: Select all

2k3q1/8/2N1P3/8/3n4/3p4/2K5/8 w - - 0 2
How to handle these illegal positions when entered in an engine?
Prepare for the worst and accept?
AlvaroBegue
Posts: 931
Joined: Tue Mar 09, 2010 3:46 pm
Location: New York
Full name: Álvaro Begué (RuyDos)

Re: allowing invalid positions

Post by AlvaroBegue »

I don't think an engine should be required to accept illegal positions, but I think it is nicer to do so. You usually don't need to sacrifice much to support impossible things, including positions with more than 9 queens. If your user wants to play around with impossible positions, why not allow it?
ericlangedijk
Posts: 15
Joined: Thu Aug 08, 2013 5:13 pm

Re: allowing invalid positions

Post by ericlangedijk »

What about this one? Impossible and some engines I tried do not find the move exf6ep

Code: Select all

3b4/8/8/4PpKB/8/8/k7/8 w - f6 0 2 
syzygy
Posts: 5569
Joined: Tue Feb 28, 2012 11:56 pm

Re: allowing invalid positions

Post by syzygy »

I would say an engine is free to reject any unreachable position it likes. It should e.g. not be required to accept positions with 10 queens of the same color or with 9 pawns of the same color.

In practice it is not feasible for an engine to detect all cases where a position given to it is unreachable. As long as the engine does not crash, I do not see a problem in an engine accepting some and rejecting other unreachable positions.

So just test whether the given position is one that your data structures can cope with.
ericlangedijk
Posts: 15
Joined: Thu Aug 08, 2013 5:13 pm

Re: allowing invalid positions

Post by ericlangedijk »

Well. Some very good engines DO crash in some unreachable positions.
syzygy
Posts: 5569
Joined: Tue Feb 28, 2012 11:56 pm

Re: allowing invalid positions

Post by syzygy »

ericlangedijk wrote:Well. Some very good engines DO crash in some unreachable positions.
In my view they should not do that. :-)

One could argue that engines are free to crash whenever being fed input that does not fully comply with the UCI protocol (and the rules of chess), but personally I don't agree with that.

But then again, I don't really like the UCI protocol. As far as I can tell, it makes it impossible to easily test basic engine features from the command line. I don't understand why there is no easy way to just input e2e4 into these engines. It gives me the cramped feeling of Windows even when I'm in a Linux terminal that normally gives me full freedom.
User avatar
lucasart
Posts: 3232
Joined: Mon May 31, 2010 1:29 pm
Full name: lucasart

Re: allowing invalid positions

Post by lucasart »

ericlangedijk wrote:Some positions are not possible in chess.
For example check by a pawn AND a knight.

Code: Select all

2k3q1/8/2N1P3/8/3n4/3p4/2K5/8 w - - 0 2
How to handle these illegal positions when entered in an engine?
Prepare for the worst and accept?
In general, behaviour should be undefined, if the position is impossible. For example, if there are pawns on the 1st 8-th rank, I'm sure some engines would crash or behave strangely.

And it's a waste of time to do input validation. Input should be correct, as it is supposed to come from a GUI not the fat fingers of the user.

However in the position you give, I don't see any god reason for an engine to have problems. Why should a programmer add an extra logic that detects that a double check by a knight and a pawn is impossible ?
Theory and practice sometimes clash. And when that happens, theory loses. Every single time.
ericlangedijk
Posts: 15
Joined: Thu Aug 08, 2013 5:13 pm

Re: allowing invalid positions

Post by ericlangedijk »

For small reasons. Checking by a pawn and a kniqht is impossible so in some function we could skip code.

Code: Select all

// ...........

  checkers.&or(bba_knight[wksq] and b_knights);
  if checkers = 0 then // (skipping code example)
    checkers.&or((bba_whitepawnhit[wksq] and b_pawns));

// .........
Last edited by ericlangedijk on Wed Aug 14, 2013 1:17 am, edited 1 time in total.
JVMerlino
Posts: 1357
Joined: Wed Mar 08, 2006 10:15 pm
Location: San Francisco, California

Re: allowing invalid positions

Post by JVMerlino »

ericlangedijk wrote:What about this one? Impossible and some engines I tried do not find the move exf6ep

Code: Select all

3b4/8/8/4PpKB/8/8/k7/8 w - f6 0 2 
Correct me if I'm stupid, but I don't see why an engine WOULD necessarily find exf6ep over any other move, since all moves for White draw.

Or do you mean that some engines do not see that exf6ep is LEGAL in that position? If so, then that's a bug, IMHO.

jm
ericlangedijk
Posts: 15
Joined: Thu Aug 08, 2013 5:13 pm

Re: allowing invalid positions

Post by ericlangedijk »

They do not see it as an option. Even checkmates are seen in these kind of illegal positions :)