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

Damn subtle bug #32

Closed
AlgoTrader opened this issue Apr 10, 2013 · 3 comments
Closed

Damn subtle bug #32

AlgoTrader opened this issue Apr 10, 2013 · 3 comments

Comments

@AlgoTrader
Copy link
Contributor

var pos=new Chess('b3k2r/5p2/4p3/1p5p/6p1/2PR2P1/BP3qNP/6QK b k - 2 28')
undefined

pos.fen()
"b3k2r/5p2/4p3/1p5p/6p1/2PR2P1/BP3qNP/6QK b k - 2 28"

pos.move({from:'a8', to:'g2'})
Object {color: "b", from: "a8", to: "g2", flags: "c", piece: "b"…}

pos.fen()
"4k2r/5p2/4p3/1p5p/6p1/2PR2P1/BP3qbP/6QK w kq - 0 29"

The black king obtained the queenside castle! Magic! Losing castle status may happen, but restoring castle status is something very new in the chess!

@AlgoTrader
Copy link
Contributor Author

The code

          castling[turn] =
            castling[turn] ^= ROOKS[turn][i].flag;

is not the bug itself, but it is capable to do magic of "restoring" castle status. There was bishop at a8 moving to g2 that was erroneously detected as rook. The rook is also can restore castle status if rook moves twice from a8. The first move from a8 will reset castle flag, then if rook returns back to a8 and leave it again - we should check it, I am not sure but it can be also very surprising

I also wonder why castling[turn] is assigned twice, = and ^= are both assignments

@jhlywa
Copy link
Owner

jhlywa commented Apr 11, 2013

Good find. Looking back on some of this old castling code is a little embarrassing. I've got a fix that I'll post shortly along with a regression test or two. Thanks for the report.

@jhlywa jhlywa closed this as completed in b7c9788 Apr 11, 2013
@AlgoTrader
Copy link
Contributor Author

Great, this bug was very tough to find

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants