DOBRO / binbo

Chess representation written in Erlang using Bitboards, ready for use on game servers
Apache License 2.0
124 stars 12 forks source link

50 move rule #9

Closed nextchessmove closed 2 years ago

nextchessmove commented 2 years ago

I think that the 50-move rule isn't being applied correctly. From Wikipedia, I'm reading that:

The fifty-move rule in chess states that a player can claim a draw if no capture has been made and no pawn has been moved in the last fifty moves (for this purpose a "move" consists of a player completing a turn followed by the opponent completing a turn).

Binbo is currently looking at halfmoves instead of fullmoves:

 is_rule50(Game) ->
    case (get_halfmove(Game) < 50) of
        true  ->
            false;
        false ->
            case (get_fullmove(Game) < 75) of
                true  -> false;
                false -> true
            end
    end.

So I think it's as simple as this:

is_rule50(Game) ->
    get_halfmove(Game) >= 2 * 50.

Regarding the 75-move rule, I'm not sure it applies here. If I'm understanding the rules correctly, at 50 full moves without a pawn movement or capture (halfmove clock = 100), either player can (but does not have to) claim a draw, whereas at 75 full moves without a pawn movement or capture (halfmove clock = 150), the draw is mandatory.

Let me know if you agree, and if you'd like me to attempt a pull request with tests! Binbo is great and I'm learning a ton from it.

DOBRO commented 2 years ago

You are absolutely right. Something's wrong with this part of code.

I would rewrite it as:

is_rule50(Game) ->
    get_halfmove(Game) > 99.

it's slightly faster than >= 2 * 50.

Feel free to make a pull request and please don't forget to include tests, since this case is not covered so far. Thank you for diving into the library!

DOBRO commented 2 years ago

BTW, could you attach one or more PGNs where 50-move rule was applied? I would add them to tests also.

nextchessmove commented 2 years ago

Absolutely! Here's the PGN I used:

[Event "Hoogovens Open"]
[Site "Wijk aan Zee NED"]
[Date "1995.01.27"]
[EventDate "?"]
[Round "10"]
[Result "1/2-1/2"]
[White "Jan Timman"]
[Black "Christopher Lutz"]
[ECO "E59"]
[WhiteElo "?"]
[BlackElo "?"]
[PlyCount "242"]

1.d4 Nf6 2.c4 e6 3.Nc3 Bb4 4.e3 O-O 5.Bd3 d5 6.Nf3 c5 7.O-O
Nc6 8.a3 Bxc3 9.bxc3 dxc4 10.Bxc4 Qc7 11.Bb5 a6 12.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7
Re8 98.Be3 Ra8 99.Bc5 Ra4 100.Ke3 Rg4 101.Bd6 Rg6 102.Rf2+ Kh3
103.Be5 Kg4 104.Ke4 Kh5 105.Bf6 Kg4 106.Rf4+ Kg3 107.Ke3 Kh3
108.Rf5 Rg3+ 109.Kf2 Rg2+ 110.Kf1 Rc2 111.Rg5 Rc4 112.Be5 Kh4
113.Rg8 Re4 114.Bg3+ Kh5 115.Kf2 Ra4 116.Kf3 Kh6 117.Be5 Rb4
118.Bf4+ Kh7 119.Rg5 Ra4 120.Kg4 Rb4 121.Kf5 Rb5+ 1/2-1/2

It's from a game referenced from Wikipedia's Fifty-move rule article, and I found the PGN here.

To get the particular FEN and move I used for the test, I ran the PGN through pgn-extract to see when the halfmove clock changed from 99 to 100:

cat test.pgn | ./pgn-extract --fencomments

Here's the PGN from the other games in the Wikipedia article:

https://www.chessgames.com/perl/nph-chesspgn?text=1&gid=1067317 https://www.chessgames.com/perl/nph-chesspgn?text=1&gid=1196357 https://www.chessgames.com/perl/nph-chesspgn?text=1&gid=1486011

DOBRO commented 2 years ago

All these PGNs are added to tests. Binbo 4.0.2 released with the fix. Thanks!