-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRayTable.cpp
More file actions
65 lines (54 loc) · 1.71 KB
/
Copy pathRayTable.cpp
File metadata and controls
65 lines (54 loc) · 1.71 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
//
// Created by sahoo on 27-06-2026.
//
#include "RayTable.h"
// initializes the ray lookup table
RayTable::RayTable() {
initializeRays();
}
namespace {
struct Offset {
int dr;
int df;
};
}
// precomputes rays for every square in every direction
void RayTable::initializeRays() {
constexpr Offset offsets[directionCount] = {
{ 1, 0}, // up
{-1, 0}, // down
{ 0, -1}, // left
{ 0, 1}, // right
{ 1, -1}, // upLeft
{ 1, 1}, // upRight
{-1, -1}, // downLeft
{-1, 1} // downRight
};
// generate rays for every square on the board
for (int square = 0; square < squareCount; square++) {
int rank = square >> 3;
int file = square & 7;
// generate rays in all 8 directions from this square
for (int direction = 0; direction < directionCount; direction++) {
int currentRank = rank;
int currentFile = file;
// walk in the current direction until the board edge
while (true) {
currentRank += offsets[direction].dr;
currentFile += offsets[direction].df;
if (currentRank < 0 || currentRank > 7 || currentFile < 0 || currentFile > 7) {
break;
}
// store every reachable square in this ray
rays[square][direction].push_back(currentRank * 8 + currentFile);
}
}
}
}
// returns the precomputed ray from the given square and direction
const std::vector<int>& RayTable::getRay(int square, Direction direction) const {
return rays[square][direction];
}
namespace AttackTables {
RayTable rayTable;
}