Chess Engine
C++ chess engine with movegen, bitboards, and Arduino-friendly docs
Loading...
Searching...
No Matches
attack.cpp
Go to the documentation of this file.
1#include <cstdio>
2#include "defs.h"
3
4const int knightDirection[8] = { -8, -19, -21, -12, 8, 19, 21, 12 };
5const int rookDirection[4] = { -1, -10, 1, 10 };
6const int bishopDirection[4] = { -9, -11, 11, 9 };
7const int kingDirection[8] = { -1, -10, 1, 10, -9, -11, 11, 9 };
8
9bool isSquareAttacked(const int square, const int side, const S_BOARD* pos) {
10
11 ASSERT(SqOnBoard(square));
12 ASSERT(SideValid(side));
14
15 // pawns
16 if (side == WHITE) {
17 if (pos->pieces[square - 11] == wP || pos->pieces[square - 9] == wP) {
18 return true;
19 }
20 } else {
21 if (pos->pieces[square + 11] == bP || pos->pieces[square + 9] == bP) {
22 return true;
23 }
24 }
25
26 // knights
27 for (int i = 0; i < 8; ++i) {
28 int piece = pos->pieces[square + knightDirection[i]];
30 if (piece != OFFBOARD && IsKn(piece) && pieceColor[piece] == side) {
31 return true;
32 }
33 }
34
35 // rooks, queens
36 for (int i = 0; i < 4; ++i) {
37 int dir = rookDirection[i];
38 int targetSq = square + dir;
39 ASSERT(SqIs120(targetSq));
40 int piece = pos->pieces[targetSq];
42 while (piece != OFFBOARD) {
43 if (piece != EMPTY) {
44 if (IsRQ(piece) && pieceColor[piece] == side) {
45 return true;
46 }
47 break;
48 }
49 targetSq += dir;
50 ASSERT(SqIs120(targetSq));
51 piece = pos->pieces[targetSq];
52 }
53 }
54
55 // bishops, queens
56 for (int i = 0; i < 4; ++i) {
57 int dir = bishopDirection[i];
58 int targetSq = square + dir;
59 ASSERT(SqIs120(targetSq));
60 int piece = pos->pieces[targetSq];
62 while (piece != OFFBOARD) {
63 if (piece != EMPTY) {
64 if (IsBQ(piece) && pieceColor[piece] == side) {
65 return true;
66 }
67 break;
68 }
69 targetSq += dir;
70 ASSERT(SqIs120(targetSq));
71 piece = pos->pieces[targetSq];
72 }
73 }
74
75 // kings
76 for (int i = 0; i < 8; ++i) {
77 int piece = pos->pieces[square + kingDirection[i]];
79 if (piece != OFFBOARD && IsKi(piece) && pieceColor[piece] == side) {
80 return true;
81 }
82 }
83
84 return false;
85}
const int knightDirection[8]
Knight movement directions in 120-square board representation.
Definition attack.cpp:4
const int bishopDirection[4]
Bishop movement directions in 120-square board representation.
Definition attack.cpp:6
bool isSquareAttacked(const int square, const int side, const S_BOARD *pos)
Determines if a given square is attacked by a given side.
Definition attack.cpp:9
const int rookDirection[4]
Rook movement directions in 120-square board representation.
Definition attack.cpp:5
const int kingDirection[8]
King movement directions in 120-square board representation.
Definition attack.cpp:7
bool isBoardStateValid(const S_BOARD *pos)
Performs a full internal consistency check of the board state.
Definition board.cpp:28
int pieceColor[13]
Color of each piece type.
#define IsKn(p)
Definition defs.h:189
@ EMPTY
Definition defs.h:40
@ bP
Definition defs.h:40
@ wP
Definition defs.h:40
#define IsBQ(p)
Definition defs.h:187
@ WHITE
Definition defs.h:44
#define ASSERT(n)
Definition defs.h:14
#define IsRQ(p)
Definition defs.h:188
@ OFFBOARD
Definition defs.h:54
#define IsKi(p)
Definition defs.h:190
int pieces[BRD_SQ_NUM]
Definition defs.h:102
int SqIs120(const int sq)
Checks if a given square index is within the 120-based range.
Definition validate.cpp:21
int PceValidEmptyOffbrd(const int pce)
Checks if the given piece code is empty, valid, or OFFBOARD.
Definition validate.cpp:25
int SqOnBoard(const int sq)
Checks if a given square index refers to a valid on-board square.
Definition validate.cpp:29
int SideValid(const int side)
Checks if the given side identifier is valid.
Definition validate.cpp:34