Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- let mut knight_moves = [0; 64];
- for (idx, knight_move) in knight_moves.iter_mut().enumerate() {
- let idx = idx as i64;
- let possible_knight_moves = [
- idx + 15,
- idx + 17,
- idx + 10,
- idx - 6,
- idx - 15,
- idx - 17,
- idx - 10,
- idx + 6,
- ];
- for possible_knight_move in &possible_knight_moves {
- let possible_knight_move = *possible_knight_move;
- if possible_knight_move >= 0 && possible_knight_move < 64 {
- //i have no idea how this works anymore
- if !((idx - 1) % 8 == 0 && (possible_knight_move + 1) % 8 == 0)
- && !(idx % 8 == 0
- && ((possible_knight_move + 2) % 8 == 0
- || (possible_knight_move + 1) % 8 == 0))
- && !((idx + 2) % 8 == 0 && possible_knight_move % 8 == 0)
- && !((idx + 1) % 8 == 0
- && ((possible_knight_move - 1) % 8 == 0
- || possible_knight_move % 8 == 0))
- {
- *knight_move |= 1u64 << possible_knight_move as u64;
- }
- }
- }
- }
- let mut king_moves = [0; 64];
- for (idx, king_move) in king_moves.iter_mut().enumerate() {
- let idx = idx as i64;
- let possible_king_moves = [
- idx + 8,
- idx + 9,
- idx + 1,
- idx - 7,
- idx - 8,
- idx - 9,
- idx - 1,
- idx + 7,
- ];
- for possible_king_move in &possible_king_moves {
- let possible_king_move = *possible_king_move;
- if possible_king_move >= 0 && possible_king_move < 64 {
- if !(idx % 8 == 0 && (possible_king_move + 1) % 8 == 0)
- && !((idx + 1) % 8 == 0 && possible_king_move % 8 == 0)
- {
- *king_move |= 1u64 << possible_king_move as u64;
- }
- }
- }
- }
- fn gen_ray(position: u8, direction: u8) -> u64 {
- let mut ray = 0;
- match direction {
- 0 => {
- // up
- for idx in (position..64).step_by(8) {
- ray |= 1u64 << idx;
- }
- }
- 1 => {
- // up-right
- for idx in (position..64).step_by(9) {
- if idx % 8 == 0 && idx != position {
- break;
- }
- ray |= 1u64 << idx;
- }
- }
- 2 => {
- // right
- for idx in position..64 {
- if idx % 8 == 0 && idx != position {
- break;
- }
- ray |= 1u64 << idx;
- }
- }
- 3 => {
- // down-right
- for idx in ((position % 7)..(position + 1)).step_by(7).rev() {
- if idx % 8 == 0 && idx != position {
- break;
- }
- ray |= 1u64 << idx;
- }
- }
- 4 => {
- //down
- for idx in ((position % 8)..(position + 1)).step_by(8).rev() {
- ray |= 1u64 << idx;
- }
- }
- 5 => {
- // down-left
- for idx in ((position % 9)..(position + 1)).step_by(9).rev() {
- if (idx + 1) % 8 == 0 && idx != position {
- break;
- }
- ray |= 1u64 << idx;
- }
- }
- 6 => {
- // left
- for idx in (0..(position + 1)).rev() {
- if (idx + 1) % 8 == 0 && idx != position {
- break;
- }
- ray |= 1u64 << idx;
- }
- }
- 7 => {
- // up-left
- for idx in (position..64).step_by(7) {
- if (idx + 1) % 8 == 0 && idx != position {
- break;
- }
- ray |= 1u64 << idx;
- }
- }
- _ => (),
- };
- ray & !(1u64 << position)
- }
- let mut rays = [[0; 8]; 64];
- for (position, square_rays) in rays.iter_mut().enumerate() {
- for (direction, ray) in square_rays.iter_mut().enumerate() {
- *ray = gen_ray(position as u8, direction as u8);
- }
- }
- fn blocker_from_idx(idx: u32, mut mask: u64) -> u64 {
- let mut blockers = 0;
- let mut bit_at = 0;
- while mask != 0 {
- let lsb = mask.trailing_zeros();
- mask &= !(1u64 << lsb);
- if idx & (1u32 << bit_at) != 0 {
- blockers |= 1u64 << lsb;
- }
- bit_at += 1;
- }
- blockers
- }
- let edges = 18411139144890810879u64;
- let mut bishop_table = vec![vec![0; 1024]; 64];
- let mut rook_table = vec![vec![0; 4096]; 64];
- for square in 0..64 {
- let mut bishop_rays = 0;
- for direction in 0..4 {
- bishop_rays |= rays[square][direction * 2 + 1];
- }
- bishop_rays &= !edges;
- let mut rook_rays = 0;
- for direction in 0..4 {
- rook_rays |= rays[square][direction * 2];
- }
- rook_rays &= !edges;
- for bishop_blockers_idx in 0..(1u64 << magics::BISHOP_INDICES[square]) {
- let blockers = blocker_from_idx(bishop_blockers_idx as u32, bishop_rays);
- let key = ((blockers.wrapping_mul(magics::BISHOP_MAGICS[square]))
- >> (64 - magics::BISHOP_INDICES[square])) as usize;
- bishop_table[square][key] =
- MoveGen::gen_bishop_classical(rays, square as u8, blockers);
- }
- for rook_blockers_idx in 0..(1u64 << magics::ROOK_INDICES[square]) {
- let blockers = blocker_from_idx(rook_blockers_idx as u32, rook_rays);
- let key = ((blockers.wrapping_mul(magics::ROOK_MAGICS[square]))
- >> (64 - magics::ROOK_INDICES[square])) as usize;
- rook_table[square][key] = MoveGen::gen_rook_classical(rays, square as u8, blockers);
- }
- }
- MoveGen {
- rays,
- bishop_table,
- rook_table,
- knight_moves,
- king_moves,
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement