@@ -23,122 +23,112 @@ namespace Attacks
2323{
2424 // Initializes magic bitboard arrays. Should be called before
2525 // using Attacks::bishop / Attacks::rook
26- inline void init ()
27- {
28- initmagicmoves ();
29- }
30-
31- // Return a bitboard of the knight attacks for a
32- // knight situated on the given square
33- inline uint64_t knight (Square sq)
34- {
35- return BitMask::knight_attacks[sq];
36- }
37-
38- // Return a bitboard of the king attacks for a
39- // king situated on the given square
40- inline uint64_t king (Square sq)
41- {
42- return BitMask::king_attacks[sq];
43- }
44-
45- // Return a bitboard of the bishop attacks for a
46- // bishop situated on the given square
47- // Diagonal and anti-diagonal attacks with respect to the current occupancy
48- inline uint64_t bishop (Square sq, uint64_t occ)
49- {
50- return Bmagic (sq, occ);
51- }
52-
53- // Return a bitboard of the rook attacks for a
54- // rook situated on the given square
55- // Vertical (file) and horizontal(rank) attacks with respect to the current occupancy
56- inline uint64_t rook (Square sq, uint64_t occ)
57- {
58- return Rmagic (sq, occ);
59- }
60-
61- // Return a bitboard of the queen attacks for a
62- // queen situated on the given square
63-
64- // Diagonal and anti-diagonal attacks with respect to the current occupancy
65- // Vertical (file) and horizontal(rank) attacks with respect to the current occupancy
66- inline uint64_t queen (Square sq, uint64_t occ)
67- {
68- return Qmagic (sq, occ);
69- }
70-
71- // Check whether the given square is directly attacked by any of the given color's
72- // pieces in the position.
73- inline bool square_attacked (Position const & position, Square sq, Color enemy, uint64_t occupancy)
74- {
75- assert (is_ok (sq));
76- uint64_t pawns = position.pieces .get_piece_bb <Pawn>(enemy);
77- uint64_t knights = position.pieces .get_piece_bb <Knight>(enemy);
78- uint64_t bishops = position.pieces .get_piece_bb <Bishop>(enemy);
79- uint64_t rooks = position.pieces .get_piece_bb <Rook>(enemy);
80- uint64_t queens = position.pieces .get_piece_bb <Queen>(enemy);
81- uint64_t kings = position.pieces .get_piece_bb <King>(enemy);
82-
83- bishops |= queens;
84- rooks |= queens;
85-
86-
87- return (BitMask::pawn_attacks[!enemy][sq] & pawns)
88- || (bishop (sq, occupancy) & bishops)
89- || (rook (sq, occupancy) & rooks)
90- || (knight (sq) & knights)
91- || (king (sq) & kings);
92- }
26+ inline void init ()
27+ {
28+ initmagicmoves ();
29+ }
30+
31+ // Return a bitboard of the knight attacks for a
32+ // knight situated on the given square
33+ inline uint64_t knight (Square sq)
34+ {
35+ return BitMask::knight_attacks[sq];
36+ }
37+
38+ // Return a bitboard of the king attacks for a
39+ // king situated on the given square
40+ inline uint64_t king (Square sq)
41+ {
42+ return BitMask::king_attacks[sq];
43+ }
44+
45+ // Return a bitboard of the bishop attacks for a
46+ // bishop situated on the given square
47+ // Diagonal and anti-diagonal attacks with respect to the current occupancy
48+ inline uint64_t bishop (Square sq, uint64_t occ)
49+ {
50+ return Bmagic (sq, occ);
51+ }
52+
53+ // Return a bitboard of the rook attacks for a
54+ // rook situated on the given square
55+ // Vertical (file) and horizontal(rank) attacks with respect to the current occupancy
56+ inline uint64_t rook (Square sq, uint64_t occ)
57+ {
58+ return Rmagic (sq, occ);
59+ }
60+
61+ // Return a bitboard of the queen attacks for a
62+ // queen situated on the given square
63+
64+ // Diagonal and anti-diagonal attacks with respect to the current occupancy
65+ // Vertical (file) and horizontal(rank) attacks with respect to the current occupancy
66+ inline uint64_t queen (Square sq, uint64_t occ)
67+ {
68+ return Qmagic (sq, occ);
69+ }
70+
71+ // Check whether the given square is directly attacked by any of the given color's
72+ // pieces in the position.
73+ inline bool square_attacked (Position const &position, Square sq, Color enemy, uint64_t occupancy)
74+ {
75+ assert (is_ok (sq));
76+ uint64_t pawns = position.pieces .get_piece_bb <Pawn>(enemy);
77+ uint64_t knights = position.pieces .get_piece_bb <Knight>(enemy);
78+ uint64_t bishops = position.pieces .get_piece_bb <Bishop>(enemy);
79+ uint64_t rooks = position.pieces .get_piece_bb <Rook>(enemy);
80+ uint64_t queens = position.pieces .get_piece_bb <Queen>(enemy);
81+ uint64_t kings = position.pieces .get_piece_bb <King>(enemy);
82+
83+ bishops |= queens;
84+ rooks |= queens;
85+
86+ return (BitMask::pawn_attacks[!enemy][sq] & pawns) || (bishop (sq, occupancy) & bishops) || (rook (sq, occupancy) & rooks) || (knight (sq) & knights) || (king (sq) & kings);
87+ }
9388
9489 // Return a bitboard of all the attackers to the given square, both black and white pieces
95- // are included.
96- inline uint64_t attackers_to_sq (Position const & position, Square sq)
97- {
98- uint64_t occ = position.total_occupancy ();
99- uint64_t pawn_mask = (BitMask::pawn_attacks[White][sq] & position.pieces .bitboards [Pawn] & position.pieces .colors [Black]);
100- pawn_mask |= (BitMask::pawn_attacks[Black][sq] & position.pieces .bitboards [Pawn] & position.pieces .colors [White]);
101-
102- uint64_t bishops = position.pieces .bitboards [Bishop] | position.pieces .bitboards [Queen];
103- uint64_t rooks = position.pieces .bitboards [Rook] | position.pieces .bitboards [Queen];
104-
105-
106- return (pawn_mask)
107- | (knight (sq) & position.pieces .bitboards [Knight])
108- | (king (sq) & position.pieces .bitboards [King])
109- | (bishop (sq, occ) & bishops)
110- | (rook (sq, occ) & rooks);
111- }
112-
113- inline bool square_attacked (Position const & position, Square sq, Color enemy)
114- {
115- return square_attacked (position, sq, enemy, position.total_occupancy ());
116- }
117-
118- // Check for the piece and generate (no pawns)
119- inline uint64_t generate (PieceType piece, Square sq, uint64_t occ)
120- {
121- switch (piece)
122- {
123- case Knight:
124- return knight (sq);
125-
126- case Bishop:
127- return bishop (sq, occ);
128-
129- case Rook:
130- return rook (sq, occ);
131-
132- case Queen:
133- return queen (sq, occ);
134-
135- case King:
136- return king (sq);
137-
138- default :
139- throw std::invalid_argument (" Invalid argument in generate" );
140- return 0 ;
141- }
142- }
90+ // are included.
91+ inline uint64_t attackers_to_sq (Position const &position, Square sq)
92+ {
93+ uint64_t occ = position.total_occupancy ();
94+ uint64_t pawn_mask = (BitMask::pawn_attacks[White][sq] & position.pieces .bitboards [Pawn] & position.pieces .colors [Black]);
95+ pawn_mask |= (BitMask::pawn_attacks[Black][sq] & position.pieces .bitboards [Pawn] & position.pieces .colors [White]);
96+
97+ uint64_t bishops = position.pieces .bitboards [Bishop] | position.pieces .bitboards [Queen];
98+ uint64_t rooks = position.pieces .bitboards [Rook] | position.pieces .bitboards [Queen];
99+
100+ return (pawn_mask) | (knight (sq) & position.pieces .bitboards [Knight]) | (king (sq) & position.pieces .bitboards [King]) | (bishop (sq, occ) & bishops) | (rook (sq, occ) & rooks);
101+ }
102+
103+ inline bool square_attacked (Position const &position, Square sq, Color enemy)
104+ {
105+ return square_attacked (position, sq, enemy, position.total_occupancy ());
106+ }
107+
108+ // Check for the piece and generate (no pawns)
109+ inline uint64_t generate (PieceType piece, Square sq, uint64_t occ)
110+ {
111+ switch (piece)
112+ {
113+ case Knight:
114+ return knight (sq);
115+
116+ case Bishop:
117+ return bishop (sq, occ);
118+
119+ case Rook:
120+ return rook (sq, occ);
121+
122+ case Queen:
123+ return queen (sq, occ);
124+
125+ case King:
126+ return king (sq);
127+
128+ default :
129+ throw std::invalid_argument (" Invalid argument in generate" );
130+ return 0 ;
131+ }
132+ }
143133
144134}
0 commit comments