Преглед на файлове

adding magic bitboard move generation

Nicolas Winkler преди 2 години
родител
ревизия
6e3fdae19e
променени са 4 файла, в които са добавени 82 реда и са изтрити 6 реда
  1. 5 1
      Cargo.toml
  2. 67 1
      src/main.rs
  3. 8 2
      src/movegen.rs
  4. 2 2
      src/settings.rs

+ 5 - 1
Cargo.toml

@@ -9,6 +9,10 @@ log = "*"
 simplelog = "*"
 rand = "*"
 
+[build-dependencies]
+rand = "*"
+
 [profile.release]
 opt-level = 3
-lto = true
+lto = true
+

+ 67 - 1
src/main.rs

@@ -8,17 +8,83 @@ pub mod search;
 pub mod zobrist;
 pub mod hash;
 pub mod settings;
+pub mod magic;
 
 extern crate log;
 extern crate simplelog;
 extern crate rand;
 
-use std::sync::mpsc;
+use std::sync::{mpsc};
+use bitboard::{Bitboard};
+use rand::RngCore;
+use crate::bitboard::{Square, print_board, ROW_4};
 use std::{thread, fs::File};
+use bitboard::{from_square};
 use log::*;
+use movegen::generate_sliding_destinations;
 use simplelog::*;
 use engine::Engine;
 
+use crate::bitboard::{FILE_A, FILE_H, ROW_1, ROW_8, ROW_3};
+
+
+fn extract(mut mask: Bitboard, index: u32) -> Bitboard {
+    let required_bits = mask.count_ones();
+    let mut result: Bitboard = 0;
+    for bit in 0..required_bits {
+        let idx = mask.trailing_zeros();
+        if (1 << bit) & index != 0 {
+            result |= 1 << idx;
+        }
+        mask = mask & !(1 << idx);
+    }
+    return result;
+}
+
+pub fn low_one_random() -> u64 {
+    let mut rng = rand::thread_rng();
+    rng.next_u64() & rng.next_u64() & rng.next_u64()
+}
+
+fn create_magic_bishop(s: Square) -> Option<(u64, Vec<Bitboard>)> {
+    let border_mask = FILE_A | FILE_H | ROW_1 | ROW_8;
+    let bitboard = from_square(s);
+    let patt = generate_sliding_destinations(border_mask, 0, bitboard, false, true);
+
+    let required_bits = patt.count_ones();
+    
+    for _ in 0..1000000 {
+        let mut table: [Bitboard; 128] = [0; 128];
+        let magic = low_one_random();
+        let mut failed = false;
+        for i in 0..(1 << required_bits) {
+            let b = extract(patt, i);
+            let correct_result = generate_sliding_destinations(0, b, bitboard, false, true);
+
+            let index = magic.wrapping_mul(b).wrapping_shr(64 - required_bits) as usize;
+            if table[index] == 0 {
+                table[index] = correct_result;
+            }
+            else if table[index] != correct_result {
+                failed = true;
+                break;
+            }
+
+            //println!("{}", print_board(b));
+        }
+
+        if !failed {
+            println!("OMG: {:16x}", magic);
+            return Some((magic, table.to_vec()));
+        }
+
+    }
+
+    println!("att\n{}", print_board(patt));
+    return None;
+}
+
+
 fn main() {
     /*let mut builder = Builder::from_default_env();
     builder

+ 8 - 2
src/movegen.rs

@@ -729,7 +729,13 @@ fn generate_sliding_moves(game: &Game, friends: Bitboard, others: Bitboard, piec
     let mask = if captures_only { others } else { !(0 as Bitboard) };
 
     for piece in pieces_iter {
-        let destinations = generate_sliding_destinations(friends, others, piece, straight, diagonal);
+        let mut destinations: Bitboard = 0;
+        if diagonal {
+            destinations |= crate::magic::magic_bishop(square(piece), friends | others) & !friends;
+        }
+        if straight {
+            destinations |= crate::magic::magic_rook(square(piece), friends | others) & !friends;
+        }
         let dest_iter = BitboardIterator(destinations & mask);
         for dest in dest_iter {
             let smove = SimpleMove{ from: square(piece), to: square(dest) };
@@ -743,7 +749,7 @@ fn generate_sliding_moves(game: &Game, friends: Bitboard, others: Bitboard, piec
     }
 }
 
-fn generate_sliding_destinations(friends: Bitboard, others: Bitboard,
+pub fn generate_sliding_destinations(friends: Bitboard, others: Bitboard,
                                piece: Bitboard, straight: bool,
                                diagonal: bool) -> Bitboard {
     let occupied = friends | others;

+ 2 - 2
src/settings.rs

@@ -22,7 +22,7 @@ pub struct Setting {
 
 
 pub struct Configuration {
-    settings: BTreeMap<String, (Setting, SettingValue)>,
+    pub settings: BTreeMap<String, (Setting, SettingValue)>,
 }
 
 impl Configuration {
@@ -33,7 +33,7 @@ impl Configuration {
     }
 
 
-    pub fn get_setting(name: &str) {
+    pub fn get_setting(_name: &str) {
         
     }
 }