theultraman20

Untitled

Dec 23rd, 2025
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Rust 1.29 KB | None | 0 0
  1. pub fn gen_magic_table(pos: (u8, u8), orthogonal: bool) -> ArrayVec<u64, 4096> {
  2.     let (x, y) = pos;
  3.     let mut range_board = if orthogonal { gen_straight(x, y) } else { gen_diagonal(x, y) };
  4.  
  5.     // remove redundant ranks and files
  6.     if x != 0 {
  7.         range_board &= !column_left;
  8.     }
  9.     if x != 7 {
  10.         range_board &= !(column_left >> 7);
  11.     }
  12.     if y != 0 {
  13.         range_board &= !row_top;
  14.     }
  15.     if y != 7 {
  16.         range_board &= !(row_top >> 56);
  17.     }
  18.  
  19.     let mut table_sz: usize = 12;
  20.     if x == 0 || x == 7 {
  21.         table_sz -= 1;
  22.     }
  23.     if y == 0 || y == 7 {
  24.         table_sz -= 1;
  25.     }
  26.  
  27.     // store positions of each bit from the range board
  28.     // which are going to be toggling
  29.     let mut bit_positions = ArrayVec::<u32, 12>::new();
  30.     while range_board != 0 {
  31.         let next_pos = range_board.trailing_zeros();
  32.         bit_positions.push(next_pos);
  33.         range_board &= !(1u64 << next_pos);
  34.     }
  35.  
  36.     // 1024, 2048, or 4096 permutations of rays
  37.     for i in 0..2u64.pow(table_sz as u32) {
  38.         // generate permutation of blockers along ray
  39.         let mut blocker_board: u64 = 0;
  40.         for j in 0..table_sz {
  41.             blocker_board ^= (i & (1u64 << j)) << (bit_positions[j]);
  42.         }
  43.  
  44.  
  45.     }
  46.  
  47.     ArrayVec::new()
  48. }
  49.  
Advertisement
Add Comment
Please, Sign In to add comment