Explorar o código

Merge branch 'master' of https://git.winfor.ch/nicolas/bishop

Nicolas Winkler %!s(int64=3) %!d(string=hai) anos
pai
achega
3fb9915980
Modificáronse 4 ficheiros con 50 adicións e 6 borrados
  1. 3 1
      src/evaluate.rs
  2. 1 1
      src/hash.rs
  3. 42 0
      src/movegen.rs
  4. 4 4
      src/search.rs

+ 3 - 1
src/evaluate.rs

@@ -150,7 +150,9 @@ pub fn evaluate(game: &Game) -> PosValue {
     let sv = side_value(game, game.turn) as PosValue - side_value(game, !game.turn) as PosValue;
     let material_value_us = material_value(game, game.turn);
     let material_value_them = material_value(game, !game.turn);
-    let mat_val = ((material_value_us as f32).powf(0.995f32) - (material_value_them as f32).powf(0.995f32)) as PosValue;
+
+    let mat_val = (material_value_us) - (material_value_them) as PosValue;
+
     let kv = knight_value(game, game.turn) - knight_value(game, !game.turn);
     let king_safety = king_safety(game, game.turn) - king_safety(game, !game.turn);
     let king_there = king_there(game, game.turn) - king_there(game, !game.turn);

+ 1 - 1
src/hash.rs

@@ -63,7 +63,7 @@ impl Cache {
     }
 
     pub fn lookup(&self, game_pos: &Game) -> Option<CacheEntry> {
-        self.hashmap.get(&game_pos.zobrist.clone().unwrap().1).map(|x| x.clone())
+        self.hashmap.get(&game_pos.zobrist.as_ref().unwrap().1).map(|x| x.clone())
     }
     
     pub fn cache(&mut self, game_pos: &Game, ce: CacheEntry) {

+ 42 - 0
src/movegen.rs

@@ -202,6 +202,48 @@ pub fn generate_legal_moves(game: &mut Game, side: Side) -> Vec<Move> {
 }
 
 
+pub fn generate_legal_sorted_moves(game: &mut Game, hash: &mut Cache, killers: &[Move], side: Side) -> Vec<Move> {
+    let moves = generate_moves(game, side);
+
+    let illegal_value = crate::evaluate::MAX_VALUE;
+
+    let mut mov_val_and_legality = |mov: &Move| {
+        let undo = game.apply(*mov);
+        let illegal = is_check(game, side);
+
+        if illegal {
+            game.undo_move(undo);
+            return illegal_value;
+        }
+
+        if let Some(e) = hash.lookup(game) {
+            game.undo_move(undo);
+            if let EntryType::Value = e.entry_type {
+                return e.value;
+            }
+            else if let EntryType::LowerBound = e.entry_type {
+                return e.value - 1000;
+            }
+            else if let EntryType::UpperBound = e.entry_type {
+                return e.value + 1000;
+            }
+            else {
+                return crate::evaluate::evaluate(game) - if killers.contains(mov) { 200 } else { 0 };
+            }
+        }
+        else {
+            let eval = crate::evaluate::evaluate(game) - if killers.contains(mov) { 200 } else { 0 };
+            game.undo_move(undo);
+            return eval;
+        }
+    };
+    let mut amovs: Vec<_> = moves.iter().map(|m| (*m, mov_val_and_legality(m))).collect();
+    amovs.sort_unstable_by_key(|x| x.1);
+
+    amovs.into_iter().filter(|x| x.1 != illegal_value).map(|(m, _v)| m).collect::<Vec<Move>>()
+}
+
+
 pub fn generate_attacking_moves(game: &Game, side: Side) -> Vec<Move> {
     let mut moves: Vec<Move> = Vec::with_capacity(32);
     generate_pawn_captures(game, side, &mut moves);

+ 4 - 4
src/search.rs

@@ -179,8 +179,8 @@ pub fn negamax(game: &mut Game, sc: &mut SearchControl, hash: &mut Cache, mut al
         }
     }
 
-
-    let mut moves = generate_legal_moves(game, game.turn);
+    let ply_depth = (sc.initial_depth - depth) as usize;
+    let moves = generate_legal_sorted_moves(game, hash, &sc.killer_moves[ply_depth], game.turn);
     //info!("nega moves: {:?}", moves.iter().map(|mv| mv.to_string()).collect::<Vec<String>>());
 
     let check = is_check(game, game.turn);
@@ -214,8 +214,8 @@ pub fn negamax(game: &mut Game, sc: &mut SearchControl, hash: &mut Cache, mut al
         }
     }
 
-    let ply_depth = (sc.initial_depth - depth) as usize;
-    sort_moves(game, hash, &sc.killer_moves[ply_depth], &mut moves);
+
+    //sort_moves(game, hash, &sc.killer_moves, &mut moves);
     //moves.sort_by_cached_key(|m| if sc.is_killer(*m) { -1 } else { 0 });
 
     let mut alpha_is_exact = false;