This repository has been archived on 2023-03-04. You can view files and clone it, but cannot push or open issues or pull requests.
Vatu/res/scripts/gen_knight_rays.py

31 lines
791 B
Python
Raw Normal View History

2020-06-21 00:33:05 +02:00
#!/usr/bin/env python3
"""Pre-compute knight ray bitboards for each square."""
TEMPLATE = """\
/// Pre-computed knight rays.
2020-06-21 15:57:58 +02:00
pub const KNIGHT_RAYS: [Bitboard; 64] = [
2020-06-21 00:33:05 +02:00
{}
];
"""
DIRS = [(1, 2), (2, 1), (2, -1), (1, -2), (-1, -2), (-2, -1), (-2, 1), (-1, 2)]
def bit_pos(square):
return 1 << square
def get_rays():
rays = []
for f in range(8):
for r in range(8):
bitboard = 0
for dir_f, dir_r in DIRS:
ray_f = f + dir_f
ray_r = r + dir_r
if ray_f < 0 or ray_f > 7 or ray_r < 0 or ray_r > 7:
continue
bitboard |= bit_pos(ray_f * 8 + ray_r)
rays.append(" 0b{:064b},".format(bitboard))
return rays
print(TEMPLATE.format("\n".join(get_rays())))