bhlangonijr / chesslib

chess library for legal move generation, FEN/PGN parsing and more
Apache License 2.0
223 stars 78 forks source link

Bug regarding insufficient material #67

Closed dlbbld closed 3 years ago

dlbbld commented 3 years ago

The method Board.isInsufficientMaterial returns true for KNvKN, KBvKN and KNvKB, but should return false (checkmate with cooperation of opponent always possible).

final Board board = new Board();
board.loadFromFen("8/8/4K3/8/1n6/8/5k1N/8 w - - 0 50"); // KNvKN
assertFalse(board.isInsufficientMaterial()); // should be false but is true

board.loadFromFen("1n6/8/8/2k1K3/8/8/8/5B2 w - - 0 50"); // KBvKN, bishop light squares
assertFalse(board.isInsufficientMaterial()); // should be false but is true

board.loadFromFen("1n6/8/8/8/4K3/1k6/1B6/8 w - - 0 50"); // KBvKN, bishop dark squares
assertFalse(board.isInsufficientMaterial()); // should be false but is true

board.loadFromFen("8/k2b4/8/8/2K5/8/8/1N6 w - - 0 50"); // KNvKB, bishop light squares
assertFalse(board.isInsufficientMaterial()); // should be false but is true

board.loadFromFen("5b2/8/3k4/8/8/4K3/8/1N6 w - - 0 50"); // KNvKB, bishop dark squares
assertFalse(board.isInsufficientMaterial()); // should be false but is true
ZeroOne3010 commented 3 years ago

I'm not affiliated with this project, but just found this issue anyway. It seems that different organizations have different rules on what is considered "insufficient material": https://support.chess.com/article/128-what-does-insufficient-mating-material-mean Namely, "USCF rule specifies that the game is drawn because there is no forced mate" but "FIDE rule specifies that the game is drawn only when there is no possible mate".

At the very least it seems that the isInsufficientMaterial method must have the assumptions specified in its Javadocs. In addition, one might want to create a new method (or two) for querying the sufficiency of material by different rules. The current one could even be deprecated and removed in some future major version.

dlbbld commented 3 years ago

This topic is very involved. To start with, the article you quote is incorrect. The FIDE and USCF rules are the same for the situation here. They are different when timeout is involved. This method is about the first situation. For the second situation (see also #41), the side as an argument would have to be supplied.

Without any comment, I assume the method implements the FIDE / USCF rule. Then there are the mentioned bugs.

dlbbld commented 3 years ago

Please check pull request https://github.com/bhlangonijr/chesslib/pull/68. Also contains the "one-sided" insufficient material checks mentioned in #41.

dlbbld commented 3 years ago

Closing the issue as it became stale.