Advertisement
Nosrep

Untitled

Feb 12th, 2021
1,313
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Rust 7.17 KB | None | 0 0
  1. let mut knight_moves = [0; 64];
  2.         for (idx, knight_move) in knight_moves.iter_mut().enumerate() {
  3.             let idx = idx as i64;
  4.             let possible_knight_moves = [
  5.                 idx + 15,
  6.                 idx + 17,
  7.                 idx + 10,
  8.                 idx - 6,
  9.                 idx - 15,
  10.                 idx - 17,
  11.                 idx - 10,
  12.                 idx + 6,
  13.             ];
  14.             for possible_knight_move in &possible_knight_moves {
  15.                 let possible_knight_move = *possible_knight_move;
  16.                 if possible_knight_move >= 0 && possible_knight_move < 64 {
  17.                     //i have no idea how this works anymore
  18.                     if !((idx - 1) % 8 == 0 && (possible_knight_move + 1) % 8 == 0)
  19.                         && !(idx % 8 == 0
  20.                             && ((possible_knight_move + 2) % 8 == 0
  21.                                 || (possible_knight_move + 1) % 8 == 0))
  22.                         && !((idx + 2) % 8 == 0 && possible_knight_move % 8 == 0)
  23.                         && !((idx + 1) % 8 == 0
  24.                             && ((possible_knight_move - 1) % 8 == 0
  25.                                 || possible_knight_move % 8 == 0))
  26.                     {
  27.                         *knight_move |= 1u64 << possible_knight_move as u64;
  28.                     }
  29.                 }
  30.             }
  31.         }
  32.  
  33.         let mut king_moves = [0; 64];
  34.         for (idx, king_move) in king_moves.iter_mut().enumerate() {
  35.             let idx = idx as i64;
  36.             let possible_king_moves = [
  37.                 idx + 8,
  38.                 idx + 9,
  39.                 idx + 1,
  40.                 idx - 7,
  41.                 idx - 8,
  42.                 idx - 9,
  43.                 idx - 1,
  44.                 idx + 7,
  45.             ];
  46.             for possible_king_move in &possible_king_moves {
  47.                 let possible_king_move = *possible_king_move;
  48.                 if possible_king_move >= 0 && possible_king_move < 64 {
  49.                     if !(idx % 8 == 0 && (possible_king_move + 1) % 8 == 0)
  50.                         && !((idx + 1) % 8 == 0 && possible_king_move % 8 == 0)
  51.                     {
  52.                         *king_move |= 1u64 << possible_king_move as u64;
  53.                     }
  54.                 }
  55.             }
  56.         }
  57.         fn gen_ray(position: u8, direction: u8) -> u64 {
  58.             let mut ray = 0;
  59.             match direction {
  60.                 0 => {
  61.                     // up
  62.                     for idx in (position..64).step_by(8) {
  63.                         ray |= 1u64 << idx;
  64.                     }
  65.                 }
  66.                 1 => {
  67.                     // up-right
  68.                     for idx in (position..64).step_by(9) {
  69.                         if idx % 8 == 0 && idx != position {
  70.                             break;
  71.                         }
  72.                         ray |= 1u64 << idx;
  73.                     }
  74.                 }
  75.                 2 => {
  76.                     // right
  77.                     for idx in position..64 {
  78.                         if idx % 8 == 0 && idx != position {
  79.                             break;
  80.                         }
  81.                         ray |= 1u64 << idx;
  82.                     }
  83.                 }
  84.                 3 => {
  85.                     // down-right
  86.                     for idx in ((position % 7)..(position + 1)).step_by(7).rev() {
  87.                         if idx % 8 == 0 && idx != position {
  88.                             break;
  89.                         }
  90.                         ray |= 1u64 << idx;
  91.                     }
  92.                 }
  93.                 4 => {
  94.                     //down
  95.                     for idx in ((position % 8)..(position + 1)).step_by(8).rev() {
  96.                         ray |= 1u64 << idx;
  97.                     }
  98.                 }
  99.                 5 => {
  100.                     // down-left
  101.                     for idx in ((position % 9)..(position + 1)).step_by(9).rev() {
  102.                         if (idx + 1) % 8 == 0 && idx != position {
  103.                             break;
  104.                         }
  105.                         ray |= 1u64 << idx;
  106.                     }
  107.                 }
  108.                 6 => {
  109.                     // left
  110.                     for idx in (0..(position + 1)).rev() {
  111.                         if (idx + 1) % 8 == 0 && idx != position {
  112.                             break;
  113.                         }
  114.                         ray |= 1u64 << idx;
  115.                     }
  116.                 }
  117.                 7 => {
  118.                     // up-left
  119.                     for idx in (position..64).step_by(7) {
  120.                         if (idx + 1) % 8 == 0 && idx != position {
  121.                             break;
  122.                         }
  123.                         ray |= 1u64 << idx;
  124.                     }
  125.                 }
  126.                 _ => (),
  127.             };
  128.             ray & !(1u64 << position)
  129.         }
  130.         let mut rays = [[0; 8]; 64];
  131.         for (position, square_rays) in rays.iter_mut().enumerate() {
  132.             for (direction, ray) in square_rays.iter_mut().enumerate() {
  133.                 *ray = gen_ray(position as u8, direction as u8);
  134.             }
  135.         }
  136.         fn blocker_from_idx(idx: u32, mut mask: u64) -> u64 {
  137.             let mut blockers = 0;
  138.             let mut bit_at = 0;
  139.             while mask != 0 {
  140.                 let lsb = mask.trailing_zeros();
  141.                 mask &= !(1u64 << lsb);
  142.                 if idx & (1u32 << bit_at) != 0 {
  143.                     blockers |= 1u64 << lsb;
  144.                 }
  145.                 bit_at += 1;
  146.             }
  147.             blockers
  148.         }
  149.         let edges = 18411139144890810879u64;
  150.         let mut bishop_table = vec![vec![0; 1024]; 64];
  151.         let mut rook_table = vec![vec![0; 4096]; 64];
  152.         for square in 0..64 {
  153.             let mut bishop_rays = 0;
  154.             for direction in 0..4 {
  155.                 bishop_rays |= rays[square][direction * 2 + 1];
  156.             }
  157.             bishop_rays &= !edges;
  158.             let mut rook_rays = 0;
  159.             for direction in 0..4 {
  160.                 rook_rays |= rays[square][direction * 2];
  161.             }
  162.             rook_rays &= !edges;
  163.  
  164.             for bishop_blockers_idx in 0..(1u64 << magics::BISHOP_INDICES[square]) {
  165.                 let blockers = blocker_from_idx(bishop_blockers_idx as u32, bishop_rays);
  166.                 let key = ((blockers.wrapping_mul(magics::BISHOP_MAGICS[square]))
  167.                     >> (64 - magics::BISHOP_INDICES[square])) as usize;
  168.                 bishop_table[square][key] =
  169.                     MoveGen::gen_bishop_classical(rays, square as u8, blockers);
  170.             }
  171.             for rook_blockers_idx in 0..(1u64 << magics::ROOK_INDICES[square]) {
  172.                 let blockers = blocker_from_idx(rook_blockers_idx as u32, rook_rays);
  173.                 let key = ((blockers.wrapping_mul(magics::ROOK_MAGICS[square]))
  174.                     >> (64 - magics::ROOK_INDICES[square])) as usize;
  175.                 rook_table[square][key] = MoveGen::gen_rook_classical(rays, square as u8, blockers);
  176.             }
  177.         }
  178.         MoveGen {
  179.             rays,
  180.             bishop_table,
  181.             rook_table,
  182.             knight_moves,
  183.             king_moves,
  184.         }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement