-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathattacks.cpp
More file actions
101 lines (88 loc) · 3.62 KB
/
Copy pathattacks.cpp
File metadata and controls
101 lines (88 loc) · 3.62 KB
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
#include "board.h"
namespace {
bool is_valid_square(int row, int col) {
return row >= 0 && row < BOARD_SIZE && col >= 0 && col < BOARD_SIZE;
}
} // anonymous namespace
bool is_attacked(const Board& board, int target_row, int target_col, Color by_color) {
// 1. Pawn attacks
// If by_color is White: pawns attack from row-1 (they're below, attacking upward)
// If by_color is Black: pawns attack from row+1 (they're above, attacking downward)
int pawn_dir = (by_color == Color::White) ? -1 : 1;
int attack_cols[] = {target_col - 1, target_col + 1};
for (int col : attack_cols) {
int row = target_row + pawn_dir;
if (is_valid_square(row, col)) {
const Piece& p = board.squares[row][col];
if (p.type == PieceType::Pawn && p.color == by_color) return true;
}
}
// 2. Knight attacks
int knight_offsets[8][2] = {{2, 1}, {2, -1}, {-2, 1}, {-2, -1}, {1, 2}, {1, -2}, {-1, 2}, {-1, -2}};
for (auto& offset : knight_offsets) {
int r = target_row + offset[0];
int c = target_col + offset[1];
if (is_valid_square(r, c)) {
const Piece& p = board.squares[r][c];
if (p.type == PieceType::Knight && p.color == by_color) return true;
}
}
// 3. Sliding pieces (Bishop, Rook, Queen)
// Diagonals (Bishop, Queen)
int diag_dirs[4][2] = {{1, 1}, {1, -1}, {-1, 1}, {-1, -1}};
for (auto& dir : diag_dirs) {
for (int dist = 1; dist < BOARD_SIZE; ++dist) {
int r = target_row + dir[0] * dist;
int c = target_col + dir[1] * dist;
if (!is_valid_square(r, c)) break;
const Piece& p = board.squares[r][c];
if (p.type != PieceType::None) {
if (p.color == by_color && (p.type == PieceType::Bishop || p.type == PieceType::Queen)) return true;
break; // Blocked
}
}
}
// Straights (Rook, Queen)
int straight_dirs[4][2] = {{1, 0}, {-1, 0}, {0, 1}, {0, -1}};
for (auto& dir : straight_dirs) {
for (int dist = 1; dist < BOARD_SIZE; ++dist) {
int r = target_row + dir[0] * dist;
int c = target_col + dir[1] * dist;
if (!is_valid_square(r, c)) break;
const Piece& p = board.squares[r][c];
if (p.type != PieceType::None) {
if (p.color == by_color && (p.type == PieceType::Rook || p.type == PieceType::Queen)) return true;
break; // Blocked
}
}
}
// 4. King attacks (adjacent enemy king)
int king_offsets[8][2] = {{1, 0}, {-1, 0}, {0, 1}, {0, -1}, {1, 1}, {1, -1}, {-1, 1}, {-1, -1}};
for (auto& offset : king_offsets) {
int r = target_row + offset[0];
int c = target_col + offset[1];
if (is_valid_square(r, c)) {
const Piece& p = board.squares[r][c];
if (p.type == PieceType::King && p.color == by_color) return true;
}
}
return false;
}
bool is_in_check(const Board& board, Color color) {
int king_row = -1, king_col = -1;
// Find the king
for (int row = 0; row < BOARD_SIZE; ++row) {
for (int col = 0; col < BOARD_SIZE; ++col) {
const Piece& p = board.squares[row][col];
if (p.type == PieceType::King && p.color == color) {
king_row = row;
king_col = col;
row = BOARD_SIZE; // break outer loop
break;
}
}
}
if (king_row == -1) return false; // should never happen
Color enemy = (color == Color::White) ? Color::Black : Color::White;
return is_attacked(board, king_row, king_col, enemy);
}