#!/usr/bin/env python3 """Pre-compute knight ray bitboards for each square.""" TEMPLATE = """\ /// Pre-computed knight rays. const KNIGHT_RAYS: [Bitboard; 64] = [ {} ]; """ 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())))