Chess Engine
A Chess Engine project written in C++.
Loading...
Searching...
No Matches
attacks.hpp
1#pragma once
2
3#include <array>
4
5#include "bitboard.hpp"
6#include "magic.hpp"
7#include "navigation.hpp"
8#include "piece.hpp"
9
11 public:
12 static constexpr Bitboard pawnAttacksBB(Color color, Square square) {
13 static constexpr auto table = []() constexpr {
14 std::array<std::array<Bitboard, Square::NB>, Color::NB> t{};
15 for (auto square : Square::all()) {
16 t[Color::WHITE][square] =
17 Bitboard::destinationBB(square, Direction::NW) | Bitboard::destinationBB(square, Direction::NE);
18 t[Color::BLACK][square] =
19 Bitboard::destinationBB(square, Direction::SW) | Bitboard::destinationBB(square, Direction::SE);
20 }
21 return t;
22 }();
23 return table[color][square];
24 }
25
26 static constexpr Bitboard knightAttacksBB(Square square) {
27 static constexpr auto table = []() constexpr {
28 std::array<Bitboard, Square::NB> t{};
29
30 constexpr std::array<Direction, 8> knightDirections = {Direction::NNE, Direction::NNW, Direction::ENE,
31 Direction::WNW, Direction::SSE, Direction::SSW,
32 Direction::ESE, Direction::WSW};
33
34 for (auto square : Square::all()) {
35 Bitboard attacks = Bitboard::zero();
36 for (auto direction : knightDirections) {
37 attacks |= Bitboard::destinationBB(square, direction);
38 }
39 t[square] = attacks;
40 }
41 return t;
42 }();
43 return table[square];
44 }
45
46 static constexpr Bitboard kingAttacksBB(Square square) {
47 static constexpr auto table = []() constexpr {
48 std::array<Bitboard, Square::NB> t{};
49
50 constexpr std::array<Direction, 8> kingDirections = {Direction::E, Direction::N, Direction::W,
51 Direction::S, Direction::NE, Direction::NW,
52 Direction::SE, Direction::SW};
53
54 for (auto square : Square::all()) {
55 Bitboard attacks = Bitboard::zero();
56 for (auto direction : kingDirections) {
57 attacks |= Bitboard::destinationBB(square, direction);
58 }
59 t[square] = attacks;
60 }
61 return t;
62 }();
63 return table[square];
64 }
65
66 template <uint8_t PIECETYPE>
67 static constexpr Bitboard attacksBB(Square square, Bitboard occupied = Bitboard::zero()) {
68 if constexpr (PIECETYPE == PieceType::KNIGHT) {
69 return knightAttacksBB(square);
70 } else if constexpr (PIECETYPE == PieceType::BISHOP) {
71 return bishopAttacksBB(square, occupied);
72 } else if constexpr (PIECETYPE == PieceType::ROOK) {
73 return rookAttacksBB(square, occupied);
74 } else if constexpr (PIECETYPE == PieceType::QUEEN) {
75 return queenAttacksBB(square, occupied);
76 } else if constexpr (PIECETYPE == PieceType::KING) {
77 return kingAttacksBB(square);
78 } else {
79 static_assert(PIECETYPE != PieceType::PAWN, "Use pawnAttacksBB with color parameter for pawn attacks");
80 return Bitboard::zero();
81 }
82 }
83
84 template <uint8_t PIECETYPE>
85 static constexpr Bitboard attacksBB(Color color, Square square, Bitboard occupied = Bitboard::zero()) {
86 if constexpr (PIECETYPE == PieceType::PAWN) {
87 return pawnAttacksBB(color, square);
88 } else {
89 return attacksBB<PIECETYPE>(square, occupied);
90 }
91 }
92
93 static constexpr Bitboard rookAttacks(Square square, Bitboard occupied) { return rookAttacksBB(square, occupied); }
94
95 static constexpr Bitboard bishopAttacks(Square square, Bitboard occupied) {
96 return bishopAttacksBB(square, occupied);
97 }
98
99 static constexpr Bitboard queenAttacks(Square square, Bitboard occupied) {
100 return queenAttacksBB(square, occupied);
101 }
102
103 static constexpr Bitboard knightAttacks(Square square) { return knightAttacksBB(square); }
104
105 static constexpr Bitboard kingAttacks(Square square) { return kingAttacksBB(square); }
106
107 static constexpr Bitboard pawnAttacks(Color color, Square square) { return pawnAttacksBB(color, square); }
108
109 template <uint8_t PIECETYPE>
110 static constexpr bool isAttackedBy(Square target, Square attacker, Bitboard occupied = Bitboard::zero()) {
111 return (attacksBB<PIECETYPE>(attacker, occupied) & Bitboard::squareBB(target)) != Bitboard::zero();
112 }
113
114 template <uint8_t PIECETYPE>
115 static constexpr bool isAttackedBy(Color color, Square target, Square attacker,
116 Bitboard occupied = Bitboard::zero()) {
117 if constexpr (PIECETYPE == PieceType::PAWN) {
118 return (pawnAttacksBB(color, attacker) & Bitboard::squareBB(target)) != Bitboard::zero();
119 } else {
120 return isAttackedBy<PIECETYPE>(target, attacker, occupied);
121 }
122 }
123};
Definition attacks.hpp:10
Definition bitboard.hpp:9
Definition piece.hpp:42
Definition navigation.hpp:118