Skip to content

Commit

Permalink
Farouk chessly bug/half move clock (#110)
Browse files Browse the repository at this point in the history
* remove castling rights change check for half move increment

* fix broken tests

* write tests for half move clock increment case

* simplified tests

Co-authored-by: Farouk Chousein <[email protected]>
Co-authored-by: Logan Spears <[email protected]>
  • Loading branch information
3 people authored Aug 14, 2022
1 parent 7c0de58 commit 64265d9
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 8 deletions.
4 changes: 2 additions & 2 deletions fixtures/valid_notation_tests.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
},
{
"pos1" : "r2qk2r/pp1n1ppp/2pbpn2/3p4/2PP4/1PNQPN2/P4PPP/R1B1K2R w KQkq - 1 9",
"pos2" : "r2qk2r/pp1n1ppp/2pbpn2/3p4/2PP4/1PNQPN2/P4PPP/R1B2RK1 b kq - 0 9",
"pos2" : "r2qk2r/pp1n1ppp/2pbpn2/3p4/2PP4/1PNQPN2/P4PPP/R1B2RK1 b kq - 2 9",
"algText" : "O-O",
"longAlgText" : "O-O",
"uciText" : "e1g1",
Expand Down Expand Up @@ -73,7 +73,7 @@
},
{
"pos1" : "rnbk1b1r/p3pppp/5n2/2p1p3/5B2/2N2P2/PPP3PP/R3KBNR w KQ - 0 10",
"pos2" : "rnbk1b1r/p3pppp/5n2/2p1p3/5B2/2N2P2/PPP3PP/2KR1BNR b - - 0 10",
"pos2" : "rnbk1b1r/p3pppp/5n2/2p1p3/5B2/2N2P2/PPP3PP/2KR1BNR b - - 1 10",
"algText" : "O-O-O+",
"longAlgText" : "O-O-O+",
"uciText" : "e1c1",
Expand Down
46 changes: 44 additions & 2 deletions move_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ var (
{
m: &Move{s1: E1, s2: G1, tags: KingSideCastle},
pos: unsafeFEN("r3k2r/8/8/8/8/8/8/R3K2R w KQkq - 0 1"),
postPos: unsafeFEN("r3k2r/8/8/8/8/8/8/R4RK1 b kq - 0 1"),
postPos: unsafeFEN("r3k2r/8/8/8/8/8/8/R4RK1 b kq - 1 1"),
},
{
m: &Move{s1: A4, s2: B3, tags: EnPassant},
Expand All @@ -143,7 +143,49 @@ var (
{
m: &Move{s1: E1, s2: G1, tags: KingSideCastle},
pos: unsafeFEN("r2qk2r/pp1n1ppp/2pbpn2/3p4/2PP4/1PNQPN2/P4PPP/R1B1K2R w KQkq - 1 9"),
postPos: unsafeFEN("r2qk2r/pp1n1ppp/2pbpn2/3p4/2PP4/1PNQPN2/P4PPP/R1B2RK1 b kq - 0 9"),
postPos: unsafeFEN("r2qk2r/pp1n1ppp/2pbpn2/3p4/2PP4/1PNQPN2/P4PPP/R1B2RK1 b kq - 2 9"),
},
// half move clock - knight move to f3 from starting position
{
m: &Move{s1: G1, s2: F3},
pos: unsafeFEN("rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1"),
postPos: unsafeFEN("rnbqkbnr/pppppppp/8/8/8/5N2/PPPPPPPP/RNBQKB1R b KQkq - 1 1"),
},
// half move clock - king side castle
{
m: &Move{s1: E1, s2: G1, tags: KingSideCastle},
pos: unsafeFEN("r3k2r/8/8/8/8/8/8/R3K2R w KQkq - 0 1"),
postPos: unsafeFEN("r3k2r/8/8/8/8/8/8/R4RK1 b kq - 1 1"),
},
// half move clock - queen side castle
{
m: &Move{s1: E1, s2: C1, tags: QueenSideCastle},
pos: unsafeFEN("r3k2r/ppqn1ppp/2pbpn2/3p4/2PP4/1PNQPN2/P2B1PPP/R3K2R w KQkq - 3 10"),
postPos: unsafeFEN("r3k2r/ppqn1ppp/2pbpn2/3p4/2PP4/1PNQPN2/P2B1PPP/2KR3R b kq - 4 10"),
},
// half move clock - pawn push
{
m: &Move{s1: E2, s2: E4},
pos: unsafeFEN("rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1"),
postPos: unsafeFEN("rnbqkbnr/pppppppp/8/8/4P3/8/PPPP1PPP/RNBQKBNR b KQkq e3 0 1"),
},
// half move clock - pawn capture
{
m: &Move{s1: E4, s2: D5, tags: Capture},
pos: unsafeFEN("r1bqkbnr/ppp1pppp/2n5/3p4/4P3/5N2/PPPP1PPP/RNBQKB1R w KQkq - 2 3"),
postPos: unsafeFEN("r1bqkbnr/ppp1pppp/2n5/3P4/8/5N2/PPPP1PPP/RNBQKB1R b KQkq - 0 3"),
},
// half move clock - en passant
{
m: &Move{s1: E5, s2: F6, tags: EnPassant},
pos: unsafeFEN("r1bqkbnr/ppp1p1pp/2n5/3pPp2/8/5N2/PPPP1PPP/RNBQKB1R w KQkq f6 0 4"),
postPos: unsafeFEN("r1bqkbnr/ppp1p1pp/2n2P2/3p4/8/5N2/PPPP1PPP/RNBQKB1R b KQkq - 0 4"),
},
// half move clock - piece captured by knight
{
m: &Move{s1: C6, s2: D4, tags: Capture},
pos: unsafeFEN("r1bqkbnr/ppp1p1pp/2n5/3pPp2/3N4/8/PPPP1PPP/RNBQKB1R b KQkq - 1 4"),
postPos: unsafeFEN("r1bqkbnr/ppp1p1pp/8/3pPp2/3n4/8/PPPP1PPP/RNBQKB1R w KQkq - 0 5"),
},
}
)
Expand Down
5 changes: 3 additions & 2 deletions notation_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,8 @@ func TestValidDecoding(t *testing.T) {
}
postPos := test.Pos1.Update(m)
if test.Pos2.String() != postPos.String() {
t.Fatalf("starting from board \n%s\n after move %s\n expected board to be %s\n%s\n but was %s\n%s\n",
t.Fatalf("starting from board \n%s%s\n after move %s\n expected board to be %s\n%s\n but was %s\n%s\n",
test.Pos1.String(),
test.Pos1.board.Draw(), m.String(), test.Pos2.String(),
test.Pos2.board.Draw(), postPos.String(), postPos.board.Draw())
}
Expand Down Expand Up @@ -86,7 +87,7 @@ var (
N: AlgebraicNotation{},
Pos: unsafeFEN("r2qk2r/pp1n1ppp/2pbpn2/3p4/2PP4/1PNQPN2/P4PPP/R1B1K2R w KQkq - 1 9"),
Text: "O-O-O-O",
PostPos: unsafeFEN("r2qk2r/pp1n1ppp/2pbpn2/3p4/2PP4/1PNQPN2/P4PPP/R1B2RK1 b kq - 0 9"),
PostPos: unsafeFEN("r2qk2r/pp1n1ppp/2pbpn2/3p4/2PP4/1PNQPN2/P4PPP/R1B2RK1 b kq - 2 9"),
},
{
// http://en.lichess.org/W91M4jms#23
Expand Down
3 changes: 1 addition & 2 deletions position.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,11 +74,10 @@ func (pos *Position) Update(m *Move) *Position {
if pos.turn == Black {
moveCount++
}
cr := pos.CastleRights()
ncr := pos.updateCastleRights(m)
p := pos.board.Piece(m.s1)
halfMove := pos.halfMoveClock
if p.Type() == Pawn || m.HasTag(Capture) || cr != ncr {
if p.Type() == Pawn || m.HasTag(Capture) {
halfMove = 0
} else {
halfMove++
Expand Down

0 comments on commit 64265d9

Please sign in to comment.