Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

stockfish.info returning mate in 1 in a checkmate position #21

Closed
guidopetri opened this issue Feb 16, 2020 · 14 comments · Fixed by #23
Closed

stockfish.info returning mate in 1 in a checkmate position #21

guidopetri opened this issue Feb 16, 2020 · 14 comments · Fixed by #23
Assignees
Labels

Comments

@guidopetri
Copy link
Contributor

Example code:

sf = stockfish.Stockfish(stockfish_executable)
sf.set_fen_position('8/8/8/6pp/8/4k1PP/8/r3K3 w - - 12 53')
sf.get_best_move()
sf.info  # blah blah mate 1

The position in the FEN is checkmate, but info string returns a mate in 1:

'info depth 10 seldepth 2 multipv 1 score mate 1 nodes 204 nps 204000 tbhits 0 time 1 pv a2a1'

The FEN position is the last position in this game.

@guidopetri
Copy link
Contributor Author

This doesn't seem to be the case for the library version 3.0.0. In that version, sf.info returns ''.

@guidopetri
Copy link
Contributor Author

Sorry, never mind - bug is still present in 3.0.0. I must have never implemented the functionality correctly...

@zhelyabuzhsky
Copy link
Owner

Hi, @charlesoblack

I wrote test:

def test_set_fen_position_mate(self, stockfish):
    stockfish.set_fen_position('8/8/8/6pp/8/4k1PP/8/r3K3 w - - 12 53')
    assert stockfish.get_best_move() is None
    assert stockfish.info == ''

I think it's correct behaviour. Do you agree?

@guidopetri
Copy link
Contributor Author

Yes, I think that test displays the intended behavior.

@zhelyabuzhsky
Copy link
Owner

zhelyabuzhsky commented Feb 16, 2020

So ... no bug? Can I close this issue?

@guidopetri
Copy link
Contributor Author

guidopetri commented Feb 16, 2020

While the test itself passes for me, the current behavior still shows up when running through a game with a python.chess Visitor:

class TestVisitor(chess.pgn.BaseVisitor):
    
    def __init__(self, gm):
        self.gm = gm
        self.gm.evals = []
        self.move_count = 1

    def visit_board(self, board):
        self.move_count += 1
        sf.set_fen_position(board.fen())
        best_move = sf.get_best_move()
        print(best_move, sf.info)
        info_string = sf.info
        rating_match = re.search(r'score (cp|mate) (.+?)(?: |$)', info_string)
        if rating_match.group(1) == 'mate':
            original_rating = int(rating_match.group(2))
            if original_rating:
                rating = 9999 * original_rating / abs(original_rating)
            elif self.gm.headers['Result'] == '1-0':
                rating = 9999
            else:
                rating = -9999
        else:
            rating = int(rating_match.group(2))
        if board.turn == chess.BLACK:
            rating *= -1
        self.gm.evals.append(rating)

output ends with:

a2a1 info depth 10 seldepth 2 multipv 1 score mate 1 nodes 204 nps 204000 tbhits 0 time 1 pv a2a1
None info depth 10 seldepth 2 multipv 1 score mate 1 nodes 204 nps 204000 tbhits 0 time 1 pv a2a1

Here is the PGN I am using for the above:

[Event "Rated Blitz game"]
[Site "https://lichess.org/3N2cqCjY"]
[Date "2020.02.14"]
[Round "-"]
[White "Grahtbo"]
[Black "Gorbunok"]
[Result "0-1"]
[BlackElo "1734"]
[BlackRatingDiff "+6"]
[ECO "A13"]
[Opening "English Opening: Agincourt Defense"]
[Termination "Normal"]
[TimeControl "180+0"]
[UTCDate "2020.02.14"]
[UTCTime "22:08:23"]
[Variant "Standard"]
[WhiteElo "1748"]
[WhiteRatingDiff "-6"]

1. c4 { [%clk 0:03:00] } 1... e6 { [%clk 0:03:00] } 2. b3 { [%clk 0:02:59] } 2... c6 { [%clk 0:02:59] } 3. a3 { [%clk 0:02:58] } 3... a5 { [%clk 0:02:58] } 4. Bb2 { [%clk 0:02:57] } 4... Nf6 { [%clk 0:02:55] } 5. e3 { [%clk 0:02:56] } 5... Be7 { [%clk 0:02:54] } 6. d3 { [%clk 0:02:55] } 6... O-O { [%clk 0:02:53] } 7. Nf3 { [%clk 0:02:54] } 7... h6 { [%clk 0:02:51] } 8. Be2 { [%clk 0:02:54] } 8... b6 { [%clk 0:02:50] } 9. O-O { [%clk 0:02:53] } 9... Ba6 { [%clk 0:02:48] } 10. Nbd2 { [%clk 0:02:52] } 10... d5 { [%clk 0:02:46] } 11. cxd5 { [%clk 0:02:51] } 11... cxd5 { [%clk 0:02:45] } 12. Qc2 { [%clk 0:02:50] } 12... Nbd7 { [%clk 0:02:43] } 13. Rac1 { [%clk 0:02:49] } 13... Rc8 { [%clk 0:02:41] } 14. Qb1 { [%clk 0:02:48] } 14... Rxc1 { [%clk 0:02:39] } 15. Qxc1 { [%clk 0:02:45] } 15... e5 { [%clk 0:02:26] } 16. Nxe5 { [%clk 0:02:41] } 16... Nxe5 { [%clk 0:02:25] } 17. Bxe5 { [%clk 0:02:40] } 17... Bd6 { [%clk 0:02:21] } 18. Bb2 { [%clk 0:02:38] } 18... Re8 { [%clk 0:02:18] } 19. Qa1 { [%clk 0:02:34] } 19... Bb7 { [%clk 0:02:07] } 20. Nf3 { [%clk 0:02:31] } 20... d4 { [%clk 0:02:00] } 21. Bxd4 { [%clk 0:02:25] } 21... Bxf3 { [%clk 0:01:57] } 22. Bxf3 { [%clk 0:02:24] } 22... Qe7 { [%clk 0:01:40] } 23. e4 { [%clk 0:02:22] } 23... Rc8 { [%clk 0:01:33] } 24. Bxb6 { [%clk 0:02:21] } 24... Be5 { [%clk 0:01:29] } 25. Bd4 { [%clk 0:02:17] } 25... Bxd4 { [%clk 0:01:28] } 26. Qxd4 { [%clk 0:02:15] } 26... Rc2 { [%clk 0:01:23] } 27. b4 { [%clk 0:02:07] } 27... axb4 { [%clk 0:01:19] } 28. Qxb4 { [%clk 0:02:06] } 28... Qxb4 { [%clk 0:01:15] } 29. axb4 { [%clk 0:02:05] } 29... Rb2 { [%clk 0:01:14] } 30. e5 { [%clk 0:02:03] } 30... Nd7 { [%clk 0:01:12] } 31. Bc6 { [%clk 0:02:01] } 31... Nxe5 { [%clk 0:01:10] } 32. Be4 { [%clk 0:01:58] } 32... Rxb4 { [%clk 0:01:07] } 33. f3 { [%clk 0:01:58] } 33... Rb2 { [%clk 0:01:04] } 34. d4 { [%clk 0:01:55] } 34... Nc4 { [%clk 0:00:59] } 35. d5 { [%clk 0:01:53] } 35... Nd6 { [%clk 0:00:57] } 36. Rc1 { [%clk 0:01:49] } 36... g5 { [%clk 0:00:48] } 37. Rc6 { [%clk 0:01:48] } 37... Nxe4 { [%clk 0:00:42] } 38. fxe4 { [%clk 0:01:47] } 38... Kg7 { [%clk 0:00:40] } 39. d6 { [%clk 0:01:46] } 39... Rd2 { [%clk 0:00:39] } 40. e5 { [%clk 0:01:45] } 40... Kf8 { [%clk 0:00:26] } 41. Rc7 { [%clk 0:01:43] } 41... Ke8 { [%clk 0:00:24] } 42. Kf1 { [%clk 0:01:38] } 42... h5 { [%clk 0:00:13] } 43. Rxf7 { [%clk 0:01:37] } 43... Kxf7 { [%clk 0:00:09] } 44. g3 { [%clk 0:01:36] } 44... Ke6 { [%clk 0:00:08] } 45. d7 { [%clk 0:01:35] } 45... Rxd7 { [%clk 0:00:06] } 46. h3 { [%clk 0:01:34] } 46... Kxe5 { [%clk 0:00:05] } 47. Kf2 { [%clk 0:01:34] } 47... Rd3 { [%clk 0:00:04] } 48. Kg2 { [%clk 0:01:31] } 48... Ra3 { [%clk 0:00:03] } 49. Kf2 { [%clk 0:01:30] } 49... Ke4 { [%clk 0:00:03] } 50. Kg2 { [%clk 0:01:30] } 50... Ra2+ { [%clk 0:00:02] } 51. Kf1 { [%clk 0:01:28] } 51... Ke3 { [%clk 0:00:01] } 52. Ke1 { [%clk 0:01:27] } 52... Ra1# { [%clk 0:00:00] } 0-1

and to run the visitor:

vis = TestVisitor(game)
game.accept(vis)

@guidopetri
Copy link
Contributor Author

Additionally, running the test after allowing the visitor to run over the chess game yields the incorrect behavior.

@zhelyabuzhsky
Copy link
Owner

Can you write test (pull request) to reproduce this bug?

@guidopetri
Copy link
Contributor Author

Created the test in #22 . Now to find out how to fix this bug...

@zhelyabuzhsky
Copy link
Owner

Thank you

I'll try to help you =)

@zhelyabuzhsky
Copy link
Owner

zhelyabuzhsky commented Feb 18, 2020

What about #23?

@zhelyabuzhsky zhelyabuzhsky self-assigned this Feb 18, 2020
@guidopetri
Copy link
Contributor Author

guidopetri commented Feb 18, 2020 via email

@zhelyabuzhsky
Copy link
Owner

Ok, I'll merge this pr. If you have a idea to improve this, I will always be glad your PR =)

@guidopetri
Copy link
Contributor Author

Sorry, I've been busy with school :P will let you know when I've thought about this more. Thanks for merging the test :)

johndoknjas added a commit to johndoknjas/stockfish-1 that referenced this issue Apr 22, 2023
* Update some parameters to be of type bool rather than string.

* Fix bug related to stockfish needing lowercase 'false' and 'true' options.

* Add some backwards compatability safety checks in models.py, for the new bool types of the three params. Also update the README and .gitignore.

* Add docs for simpler way to call get_parameters.

* Do a different method for handling the get_parameters change. Instead, deprecate it in favour of a new function.

* Update some wording in the readme.

* Small edits to docs for recent changes.

* Update test function

Co-authored-by: kieferro <[email protected]>

---------

Co-authored-by: kieferro <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants