Nicolas Winkler 2 лет назад
Родитель
Сommit
eb9a05f26d
2 измененных файлов с 20 добавлено и 20 удалено
  1. 18 1
      src/evaluate.rs
  2. 2 19
      src/search.rs

+ 18 - 1
src/evaluate.rs

@@ -119,6 +119,22 @@ fn material_value(game: &Game, side: Side) -> PosValue {
         + game.get_piece(QUEEN, side).count_ones() * 700) as PosValue
 }
 
+fn castling_rights(game: &Game, side: Side) -> PosValue {
+    let castling_value = 15;
+
+    (if side == WHITE {
+        0
+        + if game.castling_rights[0] { 1 } else { 0 }
+        + if game.castling_rights[1] { 1 } else { 0 }
+    }
+    else {
+        0
+        + if game.castling_rights[2] { 1 } else { 0 }
+        + if game.castling_rights[3] { 1 } else { 0 }
+    }
+    * castling_value) as PosValue
+}
+
 fn king_safety(game: &Game, side: Side) -> PosValue {
     let king = game.get_piece(KING, side);
     let area = north_one(king)
@@ -162,8 +178,9 @@ pub fn evaluate(game: &Game) -> 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);
+    let castling_rights = castling_rights(game, game.turn) - castling_rights(game, !game.turn);
 
-    let mge = sv + kv + king_safety + king_there;
+    let mge = sv + kv + king_safety + king_there + castling_rights;
     let lge = late_game_eval(game);
 
     let lateness = game_lateness(game);

+ 2 - 19
src/search.rs

@@ -74,7 +74,6 @@ pub fn search(game: &mut Game, sc: &mut SearchControl, hash: &mut Cache, mut alp
     }
 
     let mut moves = generate_legal_moves(game, game.turn);
-    let mut rng = rand::thread_rng();
     let ply_depth = (sc.initial_depth - depth) as usize;
     sort_moves(game, hash, &sc.killer_moves[ply_depth], &mut moves);
     
@@ -92,45 +91,29 @@ pub fn search(game: &mut Game, sc: &mut SearchControl, hash: &mut Cache, mut alp
 
     for mov in moves {
         let undo = game.apply(mov);
-        //assert_eq!(new_game, *game, );
 
-        //info!("searching {}", mov.to_string());
         let val = -negamax(game, sc, hash, decrease_mate_in(-beta), decrease_mate_in(-alpha), depth - 1);
+        
         game.undo_move(undo);
 
         if sc.stopping {
-            //return (Move::default(), 0);
             cancelled = true;
             break;
         }
 
-        if val >= mate_in_p1(1) {
-            // mate in 1 --- can't get better than that
-            info!("yay mate!");
-            //return SearchResult::Finished(mov, increase_mate_in(val));
-        }
-
-        //info!("searched {} -> {}", mov.to_string(), val);
-
         if increase_mate_in(val) > alpha {
             alpha = increase_mate_in(val) - ALPHA_OFFSET;
             valued_moves.push((mov, -alpha));
         }
     }
 
-
-
-    //info!("movvalues: {:?}", valued_moves.iter().map(|mv| mv.0.to_string() + " - " + &mv.1.to_string()).collect::<Vec<String>>());
-
     valued_moves.sort_by_key(|mv| mv.1);
 
-    //info!("best movvalues: {:?}", valued_moves.iter().map(|mv| mv.0.to_string() + " - " + &mv.1.to_string()).collect::<Vec<String>>());
-
     if valued_moves.len() > 0 {
         let min_val = valued_moves[0].1;
         let best_moves = valued_moves.iter().filter(|mv| mv.1 == min_val).collect::<Vec<&(Move, PosValue)>>();
 
-        //info!("bestmove value {}", -min_val);
+        let mut rng = rand::thread_rng();
         let chosen_mov = best_moves[(rng.next_u64() % best_moves.len() as u64) as usize];
         if cancelled {
             return SearchResult::Cancelled(Some((chosen_mov.0, -chosen_mov.1)));