Skip to content

Commit

Permalink
feat: chess960 castling and perft tests
Browse files Browse the repository at this point in the history
  • Loading branch information
leonhfr committed Jul 2, 2023
1 parent e183601 commit 3c5de8d
Show file tree
Hide file tree
Showing 6 changed files with 1,169 additions and 73 deletions.
6 changes: 3 additions & 3 deletions chess/castling.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,9 +99,9 @@ func newCastleCheck(c Color, s side, kings [2]Square, cf [2]File, cr castlingRig
rook1 := newSquare(cf[s], castleRank[c])
rook2 := newSquare(rookFinalFile[s], castleRank[c])

bbKingTravel := bbInBetweens[king1][king2] | king2.bitboard()
bbRookTravel := bbInBetweens[rook1][rook2] | rook2.bitboard()
bbNoCheck := bbKingTravel | king1.bitboard()
bbKingTravel := (bbInBetweens[king1][king2] | (king2.bitboard() & ^king1.bitboard())) & ^rook1.bitboard()
bbRookTravel := (bbInBetweens[rook1][rook2] | (rook2.bitboard() & ^rook1.bitboard())) & ^king1.bitboard()
bbNoCheck := bbInBetweens[king1][king2] | king1.bitboard() | king2.bitboard()

var bbNoEnemyPawn, bbNoEnemyKnight, bbNoEnemyKing bitboard
for bb := bbNoCheck; bb > 0; bb = bb.resetLSB() {
Expand Down
95 changes: 95 additions & 0 deletions chess/castling_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,98 @@ func TestCastling_String(t *testing.T) {
})
}
}

func TestNewCastleCheck(t *testing.T) {
type args struct {
c Color
s side
kings [2]Square
cf [2]File
cr castlingRights
}

all := castleBlackA | castleBlackH | castleWhiteA | castleWhiteH

tests := []struct {
name string
fen string
args
want castleCheck
}{
{
"Chess960 580",
"qbb1rkrn/1ppppppp/p7/7n/8/P2P4/1PP1PPPP/QBBRNKRN w Gg - 0 9",
args{White, hSide, [2]Square{F8, F1}, [2]File{FileD, FileG}, castleWhiteH},
castleCheck{
bbKingTravel: bbEmpty,
bbRookTravel: bbEmpty,
bbNoEnemyPawn: 61440,
bbNoEnemyKnight: 15767552,
bbNoEnemyKing: 61680,
bbNoCheck: F1.bitboard() | G1.bitboard(),
king1: F1,
king2: G1,
rook1: G1,
rook2: F1,
},
},
{
"Chess960 865",
"bqkr1rnn/1ppp1ppp/p4b2/4p3/P7/3PP2N/1PP2PPP/BQRBKR1N w FC - 3 9",
args{White, hSide, [2]Square{C8, E1}, [2]File{FileC, FileF}, castleWhiteA | castleWhiteH},
castleCheck{
bbKingTravel: G1.bitboard(),
bbRookTravel: bbEmpty,
bbNoEnemyPawn: 63488,
bbNoEnemyKnight: 16309248,
bbNoEnemyKing: 63736,
bbNoCheck: E1.bitboard() | F1.bitboard() | G1.bitboard(),
king1: E1,
king2: G1,
rook1: F1,
rook2: F1,
},
},
{
"Chess960 877",
"qrk1rnb1/p1pp1ppp/1p2Bbn1/8/4P3/6P1/PPPP1P1P/QRK1RNBN w EBeb - 1 9",
args{White, aSide, [2]Square{C8, C1}, [2]File{FileB, FileE}, all},
castleCheck{
bbKingTravel: bbEmpty,
bbRookTravel: D1.bitboard(),
bbNoEnemyPawn: 2560,
bbNoEnemyKnight: 659712,
bbNoEnemyKing: 3594,
bbNoCheck: C1.bitboard(),
king1: C1,
king2: C1,
rook1: B1,
rook2: D1,
},
},
{
"Chess960 944",
"b2krn1q/p1rppppp/1Q3n2/2p1b3/1P4P1/8/P1PPPP1P/BBRKRNN1 w - - 3 9",
args{White, aSide, [2]Square{D8, D1}, [2]File{FileC, FileE}, castleBlackH | castleWhiteA | castleWhiteH},
castleCheck{
bbKingTravel: bbEmpty,
bbRookTravel: bbEmpty,
bbNoEnemyPawn: 7680,
bbNoEnemyKnight: 1979136,
bbNoEnemyKing: 7710,
bbNoCheck: C1.bitboard() | D1.bitboard(),
king1: D1,
king2: C1,
rook1: C1,
rook2: D1,
},
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got := newCastleCheck(tt.c, tt.s, tt.kings, tt.cf, tt.cr)
assert.Equal(t, tt.want, got)
})
}
}
4 changes: 2 additions & 2 deletions chess/perft.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ func perft(pos *Position, depth int) int {
return nodes
}

// String implements fmt.Stringer.
// String implements the Stringer interface.
//
// Returns the perft result as an output accepted by the perftree cli.
func (pr PerftResult) String() string {
Expand All @@ -91,7 +91,7 @@ func (pr PerftResult) String() string {
return b.String()
}

// String implements fmt.Stringer.
// String implements the Stringer interface.
//
// Returns the move in UCI notation followed by a space, then the number of nodes
// of which this move is an ancestor.
Expand Down
Loading

0 comments on commit 3c5de8d

Please sign in to comment.