Browse Source

some refactoring

Nicolas Winkler 3 years ago
parent
commit
8f72c29f3d
7 changed files with 36 additions and 44 deletions
  1. 17 15
      src/bitboard.rs
  2. 0 1
      src/game.rs
  3. 0 1
      src/hash.rs
  4. 0 2
      src/interface.rs
  5. 9 9
      src/main.rs
  6. 9 15
      src/movegen.rs
  7. 1 1
      src/zobrist.rs

+ 17 - 15
src/bitboard.rs

@@ -1,3 +1,5 @@
+
+
 /*!
  *
  * arranged like this:
@@ -6,18 +8,18 @@
  * 55 54 ...
  *
  */
+
 pub type Bitboard = u64;
 pub type Square = u8;
 
-
-pub const A_FILE: Bitboard = 0x_8080_8080_8080_8080;
-pub const B_FILE: Bitboard = A_FILE >> 1;
-pub const C_FILE: Bitboard = A_FILE >> 2;
-pub const D_FILE: Bitboard = A_FILE >> 3;
-pub const E_FILE: Bitboard = A_FILE >> 4;
-pub const F_FILE: Bitboard = A_FILE >> 5;
-pub const G_FILE: Bitboard = A_FILE >> 6;
-pub const H_FILE: Bitboard = A_FILE >> 7;
+pub const FILE_A: Bitboard = 0x_8080_8080_8080_8080;
+pub const FILE_B: Bitboard = FILE_A >> 1;
+pub const FILE_C: Bitboard = FILE_A >> 2;
+pub const FILE_D: Bitboard = FILE_A >> 3;
+pub const FILE_E: Bitboard = FILE_A >> 4;
+pub const FILE_F: Bitboard = FILE_A >> 5;
+pub const FILE_G: Bitboard = FILE_A >> 6;
+pub const FILE_H: Bitboard = FILE_A >> 7;
 
 pub const ROW_1: Bitboard = 0x_0000_0000_0000_00FF;
 pub const ROW_2: Bitboard = 0x_0000_0000_0000_FF00;
@@ -38,27 +40,27 @@ pub fn south_one(b: Bitboard) -> Bitboard {
 }
 
 pub fn west_one(b: Bitboard) -> Bitboard {
-    (b << 1) & !H_FILE
+    (b << 1) & !FILE_H
 }
 
 pub fn east_one(b: Bitboard) -> Bitboard {
-    (b >> 1) & !A_FILE
+    (b >> 1) & !FILE_A
 }
 
 pub fn northeast_one(b: Bitboard) -> Bitboard {
-    (b << 7) & !A_FILE
+    (b << 7) & !FILE_A
 }
 
 pub fn northwest_one(b: Bitboard) -> Bitboard {
-    (b << 9) & !H_FILE
+    (b << 9) & !FILE_H
 }
 
 pub fn southwest_one(b: Bitboard) -> Bitboard {
-    (b >> 7) & !H_FILE
+    (b >> 7) & !FILE_H
 }
 
 pub fn southeast_one(b: Bitboard) -> Bitboard {
-    (b >> 9) & !A_FILE
+    (b >> 9) & !FILE_A
 }
 
 pub fn bit_at_index(b: Bitboard, index: u32) -> bool {

+ 0 - 1
src/game.rs

@@ -4,7 +4,6 @@ use log::info;
 use std::sync::Arc;
 use zobrist::{ZobristTable};
 use zobrist;
-use hash::RepetitionTable;
 
 #[derive(Clone)]
 pub struct Game

+ 0 - 1
src/hash.rs

@@ -1,7 +1,6 @@
 use game::Game;
 use evaluate::PosValue;
 use std::collections::{HashMap};
-use std::hash::{BuildHasher, Hasher, Hash};
 use log::info;
 use zobrist;
 

+ 0 - 2
src/interface.rs

@@ -1,5 +1,3 @@
-
-use search::apply_move;
 use std::io::{self, BufRead};
 use std::collections::BTreeMap;
 use std::sync::mpsc::{Receiver, Sender};

+ 9 - 9
src/main.rs

@@ -1,12 +1,12 @@
-mod interface;
-mod bitboard;
-mod movegen;
-mod engine;
-mod game;
-mod evaluate;
-mod search;
-mod zobrist;
-mod hash;
+pub mod interface;
+pub mod bitboard;
+pub mod movegen;
+pub mod engine;
+pub mod game;
+pub mod evaluate;
+pub mod search;
+pub mod zobrist;
+pub mod hash;
 
 extern crate log;
 extern crate simplelog;

+ 9 - 15
src/movegen.rs

@@ -3,7 +3,6 @@ use bitboard::Square;
 use bitboard::*;
 use game::Game;
 use hash::Cache;
-use log::info;
 
 pub type Side = bool;
 
@@ -199,24 +198,19 @@ pub fn generate_attacking_moves(game: &Game, side: Side) -> Vec<Move> {
 }
 
 
-pub fn sort_moves(game: &Game, hash: &mut Cache, move_list: &mut Vec<Move>) {
-    let all_pieces = game.get_all_side(WHITE) | game.get_all_side(BLACK);
+pub fn sort_moves(game: &mut Game, hash: &mut Cache, move_list: &mut Vec<Move>) {
 
     move_list.sort_by_cached_key(|mov| {
-        let new_game = crate::search::apply_move(game, *mov);
-        if let Some(e) = hash.lookup(&new_game) {
+
+        let undo = game.apply(*mov);
+        if let Some(e) = hash.lookup(game) {
+            game.undo_move(undo);
             return e.value;
         }
         else {
-            return crate::evaluate::evaluate(&new_game);
-        }
-
-        match mov {
-            Move::Default { mov, piece_type: _, captured } => if let None = captured { 0 } else { -10 }, //if (from_square(mov.to) & all_pieces) != 0 { -10 } else { 0 },
-            Move::Castling { side: _, left: _ } => 0,
-            Move::Promotion { mov: _, promote_to: _, captured: _ } => -15,
-            Move::EnPassant { mov: _, beaten: _ } => -10,
-            Move::Nullmove => 0,
+            let eval = crate::evaluate::evaluate(game);
+            game.undo_move(undo);
+            return eval;
         }
     });
 }
@@ -328,7 +322,7 @@ fn generate_promotions(game: &Game, side: Side, move_list: &mut Vec<Move>) {
 
 fn generate_en_passant(game: &Game, side: Side, move_list: &mut Vec<Move>) {
     if let Some(ep) = game.en_passant {
-        let pawncolumn = A_FILE >> ep;
+        let pawncolumn = FILE_A >> ep;
 
         let pawnrow: Bitboard = match side {
             WHITE => ROW_5,

+ 1 - 1
src/zobrist.rs

@@ -1,6 +1,6 @@
 use rand::prelude::*;
 
-use movegen::{PieceType, Side, BLACK};
+use movegen::{PieceType};
 use bitboard::Square;
 
 pub type Hash = u64;