bhlangonijr / chesslib

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

perftTest - Exception #29

Closed LociStar closed 4 years ago

LociStar commented 4 years ago

I got an Exception from prft test with depth 5 from fen "8/1pp3p1/4pq1p/PP1bpk2/1Q2p3/4P1P1/2B2P2/6K1 b - - 2 33".

Code:

public void testPerft1() throws MoveGeneratorException {
        long nodes = testPerft("8/1pp3p1/4pq1p/PP1bpk2/1Q2p3/4P1P1/2B2P2/6K1 b - - 2 33", 5);
        assertEquals(4865609, nodes);
    }

Output:

"C:\Program Files\Java\jdk-14.0.2\bin\java.exe" -ea -Didea.test.cyclic.buffer.size=1048576 -javaagent:C:\Users\LociStar\AppData\Local\JetBrains\Toolbox\apps\IDEA-U\ch-0\202.6397.94\lib\idea_rt.jar=59153:C:\Users\LociStar\AppData\Local\JetBrains\Toolbox\apps\IDEA-U\ch-0\202.6397.94\bin -Dfile.encoding=UTF-8 -classpath C:\Users\LociStar\AppData\Local\JetBrains\Toolbox\apps\IDEA-U\ch-0\202.6397.94\lib\idea_rt.jar;C:\Users\LociStar\AppData\Local\JetBrains\Toolbox\apps\IDEA-U\ch-0\202.6397.94\plugins\junit\lib\junit5-rt.jar;C:\Users\LociStar\AppData\Local\JetBrains\Toolbox\apps\IDEA-U\ch-0\202.6397.94\plugins\junit\lib\junit-rt.jar;D:\Programms\Java\IdeaProjects\chesslib\target\test-classes;D:\Programms\Java\IdeaProjects\chesslib\target\classes;C:\Users\LociStar\.m2\repository\junit\junit\4.8.2\junit-4.8.2.jar com.intellij.rt.junit.JUnitStarter -ideVersion5 -junit4 com.github.bhlangonijr.chesslib.PerftTest,testPerft1
h6h5: 308869
b7b6: 331767
c7c5: 240775
c7c6: 286383
g7g5: 244940
g7g6: 261434
d5a2: 291547
d5b3: 265493
d5c4: 316180
d5c6: 276039
f6h4: 305289
f6g5: 304930
f6g6: 241227
f6e7: 386238
f6f7: 335636
f6d8: 448502
f6f8: 452807
depth 2 - ply 4
com.github.bhlangonijr.chesslib.move.MoveGeneratorException: Couldn't generate Legal moves: 
    at com.github.bhlangonijr.chesslib.move.MoveGenerator.generateLegalMoves(MoveGenerator.java:353)
    at com.github.bhlangonijr.chesslib.PerftTest.perft(PerftTest.java:217)
    at com.github.bhlangonijr.chesslib.PerftTest.perft(PerftTest.java:223)
    at com.github.bhlangonijr.chesslib.PerftTest.perft(PerftTest.java:223)
    at com.github.bhlangonijr.chesslib.PerftTest.perft(PerftTest.java:223)
    at com.github.bhlangonijr.chesslib.PerftTest.perft(PerftTest.java:223)
    at com.github.bhlangonijr.chesslib.PerftTest.testPerft(PerftTest.java:200)
    at com.github.bhlangonijr.chesslib.PerftTest.testPerft1(PerftTest.java:26)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.base/java.lang.reflect.Method.invoke(Method.java:564)
    at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:44)
    at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)
    at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:41)
    at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:20)
    at org.junit.runners.BlockJUnit4ClassRunner.runNotIgnored(BlockJUnit4ClassRunner.java:79)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:71)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:49)
    at org.junit.runners.ParentRunner$3.run(ParentRunner.java:193)
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:52)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:191)
    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:42)
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:184)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:236)
    at org.junit.runner.JUnitCore.run(JUnitCore.java:157)
    at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:69)
    at com.intellij.rt.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:33)
    at com.intellij.rt.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:220)
    at com.intellij.rt.junit.JUnitStarter.main(JUnitStarter.java:53)
Caused by: java.lang.ArrayIndexOutOfBoundsException: Index 64 out of bounds for length 64
    at com.github.bhlangonijr.chesslib.Bitboard.getPawnAttacks(Bitboard.java:469)
    at com.github.bhlangonijr.chesslib.Board.squareAttackedBy(Board.java:1057)
    at com.github.bhlangonijr.chesslib.Board.isKingAttacked(Board.java:1137)
    at com.github.bhlangonijr.chesslib.move.MoveGenerator.generateCastleMoves(MoveGenerator.java:278)
    at com.github.bhlangonijr.chesslib.move.MoveGenerator.generatePseudoLegalMoves(MoveGenerator.java:314)
    at com.github.bhlangonijr.chesslib.move.MoveGenerator.generateLegalMoves(MoveGenerator.java:346)
    ... 29 more
depth 3 - ply 3
  ..........

Test Results — PerftTest.testPerft1.pdf

bhlangonijr commented 4 years ago

Hello @LociStar , I found a bug in the method that checks for move legality. It was allowing en passant move which was leaving the black king in check: FEN 8/1pp3p1/4pq1p/PP1bp3/1Q2pPk1/4P1P1/2B5/6K1 b - f3 0 34 was generating the move e4f3 which lead to an invalid position and caused the exception.

Added tests for the perft reported and also for the invalid move generation: https://github.com/bhlangonijr/chesslib/pull/30

Thanks for reporting the bug!

bhlangonijr commented 4 years ago

I am closing it, tag 1.1.22 fixes de issue.