rchess 2.3.0

A Chess Library Written In Rust
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
use super::movelist::{MoveList, PieceMoves};
use crate::chessboard::tables::{
    get_bishop_attacks, get_connection_axis, get_direct_connection, get_king_attacks,
    get_knight_attacks, get_pawn_attacks, get_rook_attacks,
};
use crate::chessboard::ChessBoard;
use crate::defs::*;

/// Get a [`MoveList`] of moves for a chessboard.
pub fn generate_moves<const CAPTURES_ONLY: bool>(chessboard: &ChessBoard) -> MoveList {
    let mut moves = MoveList::new();

    if chessboard.checkers().is_empty() {
        // The king is not in check.
        generate_pawn_moves::<CAPTURES_ONLY, false>(&mut moves, chessboard);
        generate_knight_moves::<CAPTURES_ONLY, false>(&mut moves, chessboard);
        generate_king_moves::<CAPTURES_ONLY, false>(&mut moves, chessboard);
        generate_bishop_moves::<CAPTURES_ONLY, false>(&mut moves, chessboard);
        generate_rook_moves::<CAPTURES_ONLY, false>(&mut moves, chessboard);
        generate_queen_moves::<CAPTURES_ONLY, false>(&mut moves, chessboard);
    } else if chessboard.checkers().popcnt() == 1 {
        // The king is in check by one piece.
        generate_pawn_moves::<CAPTURES_ONLY, true>(&mut moves, chessboard);
        generate_knight_moves::<CAPTURES_ONLY, true>(&mut moves, chessboard);
        generate_king_moves::<CAPTURES_ONLY, true>(&mut moves, chessboard);
        generate_bishop_moves::<CAPTURES_ONLY, true>(&mut moves, chessboard);
        generate_rook_moves::<CAPTURES_ONLY, true>(&mut moves, chessboard);
        generate_queen_moves::<CAPTURES_ONLY, true>(&mut moves, chessboard);
    } else {
        // The king is checked by multiple pieces.
        generate_king_moves::<false, true>(&mut moves, chessboard);
    }

    moves
}

/// Gets a [`BitBoard`] of moves for the piece on the given [`Square`].
///
/// If there was no piece on the square, or it is not that piece's turn, an empty [`BitBoard`] is returned.
pub fn generate_square_moves<const CAPTURES_ONLY: bool>(
    chessboard: &ChessBoard,
    square: Square,
) -> BitBoard {
    let piece = match chessboard.piece_at(square) {
        None => return BitBoard::EMPTY,
        Some(piece) => piece,
    };

    if piece.color != chessboard.turn() {
        return BitBoard::EMPTY;
    }

    if chessboard.checkers().popcnt() > 1 {
        if piece.kind != PieceType::King {
            return BitBoard::EMPTY;
        }
        return generate_king_attacks::<false, true>(
            chessboard,
            chessboard.get_king_square(chessboard.turn()),
        );
    }

    if chessboard.checkers().is_empty() {
        match piece.kind {
            PieceType::Pawn => generate_pawn_attacks::<CAPTURES_ONLY, false>(chessboard, square),
            PieceType::Knight => {
                generate_knight_attacks::<CAPTURES_ONLY, false, false>(chessboard, square)
            }
            PieceType::Bishop => {
                generate_bishop_attacks::<CAPTURES_ONLY, false>(chessboard, square)
            }
            PieceType::Rook => generate_rook_attacks::<CAPTURES_ONLY, false>(chessboard, square),
            PieceType::Queen => generate_queen_attacks::<CAPTURES_ONLY, false>(chessboard, square),
            PieceType::King => generate_king_attacks::<CAPTURES_ONLY, false>(chessboard, square),
        }
    } else {
        match piece.kind {
            PieceType::Pawn => generate_pawn_attacks::<CAPTURES_ONLY, true>(chessboard, square),
            PieceType::Knight => {
                generate_knight_attacks::<CAPTURES_ONLY, true, false>(chessboard, square)
            }
            PieceType::Bishop => generate_bishop_attacks::<CAPTURES_ONLY, true>(chessboard, square),
            PieceType::Rook => generate_rook_attacks::<CAPTURES_ONLY, true>(chessboard, square),
            PieceType::Queen => generate_queen_attacks::<CAPTURES_ONLY, true>(chessboard, square),
            PieceType::King => generate_king_attacks::<CAPTURES_ONLY, true>(chessboard, square),
        }
    }
}

/// Generates the pawn moves for a given [`ChessBoard`].
fn generate_pawn_moves<const CAPTURES_ONLY: bool, const IN_CHECK: bool>(
    moves: &mut MoveList,
    chessboard: &ChessBoard,
) {
    // Get extra data about the chess board.
    let us = chessboard.turn();

    // The pawns that can potentially move.
    let pawns = chessboard.query((PieceType::Pawn, us));
    for pawn_sq in pawns {
        // Get the pawn attacks.
        let attacks = generate_pawn_attacks::<CAPTURES_ONLY, IN_CHECK>(chessboard, pawn_sq);

        // Add pawn moves to the move list.
        moves.push(PieceMoves::new(pawn_sq, attacks));
    }
}

/// Generates the pawn attacks for a given square.
fn generate_pawn_attacks<const CAPTURES_ONLY: bool, const IN_CHECK: bool>(
    chessboard: &ChessBoard,
    square: Square,
) -> BitBoard {
    // Get extra data about the chess board.
    let us = chessboard.turn();
    let them = !chessboard.turn();
    let king_sq = chessboard.get_king_square(us);

    let en_passant_bb = match chessboard.en_passant_sq() {
        None => BitBoard::EMPTY,
        Some(sq) => sq.bitboard(),
    };

    let start_rank = match chessboard.turn() {
        Color::White => Rank::Second,
        Color::Black => Rank::Seventh,
    };

    let mut targets = BitBoard::EMPTY;

    // Look for pins.
    let (is_pinned, is_pinned_forward) = if square.bitboard().overlaps(chessboard.pinned()) {
        (true, king_sq.file() == square.file())
    } else {
        (false, false)
    };

    // Generate pawn non-captures.
    if !CAPTURES_ONLY || IN_CHECK {
        if !is_pinned || is_pinned_forward {
            let forward = match us {
                Color::White => square.bitboard().up(),
                Color::Black => square.bitboard().down(),
            };

            if !forward.overlaps(chessboard.occupancy()) {
                targets |= forward;
                if square.rank() == start_rank {
                    let double = match us {
                        Color::White => forward.up(),
                        Color::Black => forward.down(),
                    };

                    if !double.overlaps(chessboard.occupancy()) {
                        targets |= double;
                    }
                }
            }
        }
    }

    let mut en_passant_move = BitBoard::EMPTY;
    if !is_pinned_forward {
        // What the pawn attacks.
        let attacks = if is_pinned {
            get_pawn_attacks(square, us) & get_connection_axis(king_sq, square)
        } else {
            get_pawn_attacks(square, us)
        };

        // Add normal captures to targets.
        targets |= attacks & chessboard.color_occupancy(them);

        // Handle en passant.
        if attacks.overlaps(en_passant_bb) {
            let en_passanted = match us {
                Color::White => en_passant_bb.down(),
                Color::Black => en_passant_bb.up(),
            };

            let new_occupancy =
                chessboard.occupancy() ^ (square.bitboard() | en_passant_bb | en_passanted);

            let bishop_check_spots = get_bishop_attacks(king_sq, new_occupancy);
            let enemy_bishops = chessboard.query((PieceType::Bishop, them))
                | chessboard.query((PieceType::Queen, them));

            let rook_check_spots = get_rook_attacks(king_sq, new_occupancy);
            let enemy_rooks = chessboard.query((PieceType::Rook, them))
                | chessboard.query((PieceType::Queen, them));

            if !enemy_rooks.overlaps(rook_check_spots)
                && !enemy_bishops.overlaps(bishop_check_spots)
            {
                // King is not in check after en passant,
                en_passant_move = en_passant_bb;
            }
        }
    }

    if IN_CHECK {
        // We must defend the king.
        let king_sq = chessboard.get_king_square(us);
        let checker_sq = chessboard.checkers().b_scan_forward().unwrap();
        let defending = get_direct_connection(king_sq, checker_sq) | chessboard.checkers();
        targets &= defending;
    }

    targets | en_passant_move
}

/// Generates the knight moves for a given [`ChessBoard`].
fn generate_knight_moves<const CAPTURES_ONLY: bool, const IN_CHECK: bool>(
    moves: &mut MoveList,
    chessboard: &ChessBoard,
) {
    // Get extra data about the chess board.
    let us = chessboard.turn();

    // The knights that can potentially move.
    let knights = chessboard.query((PieceType::Knight, us)) & !chessboard.pinned();

    // Generate moves for each unpinned knight.
    for knight_sq in knights {
        let attacks =
            generate_knight_attacks::<CAPTURES_ONLY, IN_CHECK, true>(chessboard, knight_sq);

        // Add knight moves to the move list.
        moves.push(PieceMoves::new(knight_sq, attacks));
    }
}

/// Generates the knight attacks for a given square.
fn generate_knight_attacks<
    const CAPTURES_ONLY: bool,
    const IN_CHECK: bool,
    const CHECKED_PIN: bool,
>(
    chessboard: &ChessBoard,
    square: Square,
) -> BitBoard {
    // Get extra data about the chess board.
    let us = chessboard.turn();
    let them = !chessboard.turn();

    if !CHECKED_PIN {
        if chessboard.pinned().contains(square) {
            return BitBoard::EMPTY;
        }
    }

    // Get the squares the knight attacks.
    let attacks = get_knight_attacks(square);

    // Remove the squares the knight cannot attack.
    let attacks = if IN_CHECK {
        // We must defend the king.
        let king_sq = chessboard.get_king_square(us);
        let checker_sq = chessboard.checkers().b_scan_forward().unwrap();
        let defending = get_direct_connection(king_sq, checker_sq) | chessboard.checkers();
        attacks & defending
    } else {
        if CAPTURES_ONLY {
            // We have to capture an enemy piece.
            attacks & chessboard.color_occupancy(them)
        } else {
            // We can go anywhere except for where our friendly pieces are located.
            let our_pieces = chessboard.color_occupancy(us);
            attacks & !our_pieces
        }
    };

    attacks
}

/// Generates the king moves for a given [`ChessBoard`].
fn generate_king_moves<const CAPTURES_ONLY: bool, const IN_CHECK: bool>(
    moves: &mut MoveList,
    chessboard: &ChessBoard,
) {
    // Get extra data about the chess board.
    let us = chessboard.turn();

    // Get the king square.
    let king_sq = chessboard.get_king_square(us);

    // Generate the king attacks.
    let attacks = generate_king_attacks::<CAPTURES_ONLY, IN_CHECK>(chessboard, king_sq);

    // King moves to the move list.
    moves.push(PieceMoves::new(king_sq, attacks));
}

/// Generates the king attacks for a given square.
fn generate_king_attacks<const CAPTURES_ONLY: bool, const IN_CHECK: bool>(
    chessboard: &ChessBoard,
    square: Square,
) -> BitBoard {
    // Get extra data about the chess board.
    let us = chessboard.turn();
    let them = !chessboard.turn();

    // The squares attacked by the king.
    let attacks = get_king_attacks(square);

    // Make sure we only attack non-friendly pieces and follow capture only rule.
    let mut attacks = if CAPTURES_ONLY && !IN_CHECK {
        // We have to capture an enemy piece.
        attacks & chessboard.color_occupancy(them)
    } else {
        // We can move anywhere but where our friendly pieces are.
        let our_pieces = chessboard.color_occupancy(us);
        attacks & !our_pieces
    };

    // Remove checked squares from king attacks.
    for target in attacks {
        let no_king_occupancy = chessboard.occupancy() ^ square.bitboard();
        if is_square_attacked_with_occupancy(target, no_king_occupancy, them, chessboard) {
            attacks ^= target.bitboard();
        }
    }

    // Add castle moves to king targets.
    if !CAPTURES_ONLY {
        attacks |= generate_castle_moves::<IN_CHECK>(chessboard);
    }

    attacks
}

/// Generates the bishop moves for a given [`ChessBoard`].
fn generate_bishop_moves<const CAPTURES_ONLY: bool, const IN_CHECK: bool>(
    moves: &mut MoveList,
    chessboard: &ChessBoard,
) {
    // Get extra data about the chess board.
    let us = chessboard.turn();

    // Get the bishops that can potentially move.
    let bishops = chessboard.query((PieceType::Bishop, us));

    for bishop_sq in bishops {
        // Generate the bishop attacks.
        let attacks = generate_bishop_attacks::<CAPTURES_ONLY, IN_CHECK>(chessboard, bishop_sq);

        // Add bishop moves to the move list.
        moves.push(PieceMoves::new(bishop_sq, attacks));
    }
}

/// Generates the bishop attacks for a given square.
fn generate_bishop_attacks<const CAPTURES_ONLY: bool, const IN_CHECK: bool>(
    chessboard: &ChessBoard,
    square: Square,
) -> BitBoard {
    // Get extra data about the chess board.
    let us = chessboard.turn();
    let them = !chessboard.turn();

    // Get the squares attacked by the bishop.
    let attacks = get_bishop_attacks(square, chessboard.occupancy());

    // Remove squares that the bishop cannot attack.
    let attacks = if IN_CHECK {
        // We must defend the king.
        let king_sq = chessboard.get_king_square(us);
        let checker_sq = chessboard.checkers().b_scan_forward().unwrap();
        let defending = get_direct_connection(king_sq, checker_sq) | chessboard.checkers();

        if square.bitboard().overlaps(chessboard.pinned()) {
            // We are also pinned :(
            let pinned_axis = get_connection_axis(king_sq, square);
            attacks & defending & pinned_axis
        } else {
            attacks & defending
        }
    } else {
        if CAPTURES_ONLY {
            if square.bitboard().overlaps(chessboard.pinned()) {
                // We are pinned.
                let king_sq = chessboard.get_king_square(us);
                let pinned_axis = get_connection_axis(king_sq, square);
                attacks & chessboard.color_occupancy(them) & pinned_axis
            } else {
                attacks & chessboard.color_occupancy(them)
            }
        } else {
            let our_pieces = chessboard.color_occupancy(us);
            if square.bitboard().overlaps(chessboard.pinned()) {
                // We are pinned.
                let king_sq = chessboard.get_king_square(us);
                let pinned_axis = get_connection_axis(king_sq, square);
                attacks & !our_pieces & pinned_axis
            } else {
                attacks & !our_pieces
            }
        }
    };

    attacks
}

/// Generates the rook moves for a given [`ChessBoard`].
fn generate_rook_moves<const CAPTURES_ONLY: bool, const IN_CHECK: bool>(
    moves: &mut MoveList,
    chessboard: &ChessBoard,
) {
    // Get extra data about the chess board.
    let us = chessboard.turn();

    // Get the rooks that can potentially move.
    let rooks = chessboard.query((PieceType::Rook, us));

    for rook_sq in rooks {
        // Get the rook attacks.
        let attacks = generate_rook_attacks::<CAPTURES_ONLY, IN_CHECK>(chessboard, rook_sq);

        // Add rook moves to the move list.
        moves.push(PieceMoves::new(rook_sq, attacks));
    }
}

/// Generates the rook moves for a given square.
fn generate_rook_attacks<const CAPTURES_ONLY: bool, const IN_CHECK: bool>(
    chessboard: &ChessBoard,
    square: Square,
) -> BitBoard {
    // Get extra data about the chess board.
    let us = chessboard.turn();
    let them = !chessboard.turn();

    // Get the squares attacked by the rook.
    let attacks = get_rook_attacks(square, chessboard.occupancy());

    // Remove squares that the bishop cannot attack.
    let attacks = if IN_CHECK {
        // We must defend the king.
        let king_sq = chessboard.get_king_square(us);
        let checker_sq = chessboard.checkers().b_scan_forward().unwrap();
        let defending = get_direct_connection(king_sq, checker_sq) | chessboard.checkers();

        if square.bitboard().overlaps(chessboard.pinned()) {
            // We are also pinned :(
            let pinned_axis = get_connection_axis(king_sq, square);
            attacks & defending & pinned_axis
        } else {
            attacks & defending
        }
    } else {
        if CAPTURES_ONLY {
            if square.bitboard().overlaps(chessboard.pinned()) {
                // We are pinned.
                let king_sq = chessboard.get_king_square(us);
                let pinned_axis = get_connection_axis(king_sq, square);
                attacks & chessboard.color_occupancy(them) & pinned_axis
            } else {
                attacks & chessboard.color_occupancy(them)
            }
        } else {
            let our_pieces = chessboard.color_occupancy(us);
            if square.bitboard().overlaps(chessboard.pinned()) {
                // We are pinned.
                let king_sq = chessboard.get_king_square(us);
                let pinned_axis = get_connection_axis(king_sq, square);
                attacks & !our_pieces & pinned_axis
            } else {
                attacks & !our_pieces
            }
        }
    };

    attacks
}

/// Generates the queen moves for a given [`ChessBoard`].
fn generate_queen_moves<const CAPTURES_ONLY: bool, const IN_CHECK: bool>(
    moves: &mut MoveList,
    chessboard: &ChessBoard,
) {
    // Get extra data about the chess board.
    let us = chessboard.turn();

    // Get the queens that can potentially move.
    let queens = chessboard.query((PieceType::Queen, us));

    for queen_sq in queens {
        // Generate the queen attacks.
        let attacks = generate_queen_attacks::<CAPTURES_ONLY, IN_CHECK>(chessboard, queen_sq);

        // Add queen moves to the move list.
        moves.push(PieceMoves::new(queen_sq, attacks));
    }
}

/// Generates the queen attacks for a given square.
fn generate_queen_attacks<const CAPTURES_ONLY: bool, const IN_CHECK: bool>(
    chessboard: &ChessBoard,
    square: Square,
) -> BitBoard {
    generate_bishop_attacks::<CAPTURES_ONLY, IN_CHECK>(chessboard, square)
        | generate_rook_attacks::<CAPTURES_ONLY, IN_CHECK>(chessboard, square)
}

/// Generates the castle moves for a given [`ChessBoard`].
fn generate_castle_moves<const IN_CHECK: bool>(chessboard: &ChessBoard) -> BitBoard {
    // We can't castle while in check.
    if IN_CHECK {
        return BitBoard::EMPTY;
    }

    // Get extra data about the chess board.
    let us = chessboard.turn();
    let them = !chessboard.turn();

    // Where the castle moves are stored.
    let mut castles = BitBoard::EMPTY;

    // Look for kingside castle
    if chessboard.is_castle_right_set(CastleSide::Kingside, us) {
        // The squares that must be empty for the king to castle.
        let empty_squares = match chessboard.turn() {
            Color::White => BitBoard::from_squares(&[Square::F1, Square::G1]),
            Color::Black => BitBoard::from_squares(&[Square::F8, Square::G8]),
        };

        // Make sure the empty squares are empty.
        if !empty_squares.overlaps(chessboard.occupancy()) {
            // Make sure the king does not travel through a check
            let mut checked = false;
            for square in empty_squares {
                if is_square_attacked(square, them, chessboard) {
                    checked = true;
                    break;
                }
            }

            // If the king is not checked, it can castle.
            if !checked {
                // Where the king end up while castling.
                let castle_target = match chessboard.turn() {
                    Color::White => BitBoard::from_square(Square::G1),
                    Color::Black => BitBoard::from_square(Square::G8),
                };

                // Add the castle target to the castle targets.
                castles |= castle_target;
            }
        }
    }

    // Look for queenside castle
    if chessboard.is_castle_right_set(CastleSide::Queenside, us) {
        // The squares that must un checked for the king to castle.
        let un_checked = match chessboard.turn() {
            Color::White => BitBoard::from_squares(&[Square::C1, Square::D1]),
            Color::Black => BitBoard::from_squares(&[Square::C8, Square::D8]),
        };

        // The squares that must be empty for the king to castle.
        let empty_squares = match chessboard.turn() {
            Color::White => BitBoard::from_squares(&[Square::B1, Square::C1, Square::D1]),
            Color::Black => BitBoard::from_squares(&[Square::B8, Square::C8, Square::D8]),
        };

        // Make sure the empty squares are empty.
        if !empty_squares.overlaps(chessboard.occupancy()) {
            // Make sure the king does not travel through a check
            let mut checked = false;
            for square in un_checked {
                if is_square_attacked(square, them, chessboard) {
                    checked = true;
                    break;
                }
            }

            // If the king is not checked, it can castle.
            if !checked {
                // Where the king end up while castling.
                let castle_target = match chessboard.turn() {
                    Color::Black => BitBoard::from_square(Square::C8),
                    Color::White => BitBoard::from_square(Square::C1),
                };

                // Add the castle target to the castle targets.
                castles |= castle_target;
            }
        }
    }

    castles
}

/// Checks if a square is under attack by a given color.
fn is_square_attacked(square: Square, by: Color, chessboard: &ChessBoard) -> bool {
    is_square_attacked_with_occupancy(square, chessboard.occupancy(), by, chessboard)
}

/// Checks if a square is under attack by a given color with a given occupancy.
fn is_square_attacked_with_occupancy(
    square: Square,
    occupancy: BitBoard,
    by: Color,
    chessboard: &ChessBoard,
) -> bool {
    // Look for pawn attackers.
    let pawn_check_locations = get_pawn_attacks(square, !by);
    if !(chessboard.query((PieceType::Pawn, by)) & pawn_check_locations).is_empty() {
        return true;
    };

    // Look for knight attackers.
    let knight_check_locations = get_knight_attacks(square);
    if !(chessboard.query((PieceType::Knight, by)) & knight_check_locations).is_empty() {
        return true;
    };

    // Look for king attackers.
    let king_check_locations = get_king_attacks(square);
    if !(chessboard.query((PieceType::King, by)) & king_check_locations).is_empty() {
        return true;
    };

    // Look for bishop & queen attackers.
    let bishop_check_locations = get_bishop_attacks(square, occupancy);
    if !((chessboard.query((PieceType::Bishop, by)) | chessboard.query((PieceType::Queen, by)))
        & bishop_check_locations)
        .is_empty()
    {
        return true;
    };

    // Look for rook & queen attackers.
    let rook_check_locations = get_rook_attacks(square, occupancy);
    if !((chessboard.query((PieceType::Rook, by)) | chessboard.query((PieceType::Queen, by)))
        & rook_check_locations)
        .is_empty()
    {
        return true;
    };

    // The square is safe since no enemy pieces were found attacking it.
    false
}