Nicolas Winkler hace 6 años
padre
commit
d066bb606b
Se han modificado 5 ficheros con 92 adiciones y 23 borrados
  1. 16 1
      src/bitboard.rs
  2. 6 1
      src/engine.rs
  3. 1 1
      src/interface.rs
  4. 59 17
      src/movegen.rs
  5. 10 3
      src/search.rs

+ 16 - 1
src/bitboard.rs

@@ -11,7 +11,22 @@ pub type Bitboard = u64;
 
 
 pub const A_FILE: Bitboard = 0x_8080_8080_8080_8080;
-pub const H_FILE: Bitboard = 0x_0101_0101_0101_0101;
+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 ROW_1: Bitboard = 0x_0000_0000_0000_00FF;
+pub const ROW_2: Bitboard = 0x_0000_0000_0000_FF00;
+pub const ROW_3: Bitboard = 0x_0000_0000_00FF_0000;
+pub const ROW_4: Bitboard = 0x_0000_0000_FF00_0000;
+pub const ROW_5: Bitboard = 0x_0000_00FF_0000_0000;
+pub const ROW_6: Bitboard = 0x_0000_FF00_0000_0000;
+pub const ROW_7: Bitboard = 0x_00FF_0000_0000_0000;
+pub const ROW_8: Bitboard = 0x_FF00_0000_0000_0000;
 
 
 pub fn north_one(b: Bitboard) -> Bitboard {

+ 6 - 1
src/engine.rs

@@ -1,6 +1,8 @@
 use bitboard::Bitboard;
+use search::Game;
+
 use std::sync::mpsc::{Receiver, Sender};
-use movegen::Move;
+use movegen::*;
 
 pub enum EngineMsg {
     SetBoard,
@@ -26,7 +28,10 @@ pub enum InterfaceMsg {
 
 
 pub fn run_engine(r: Receiver<EngineMsg>, s: Sender<InterfaceMsg>) {
+    let mut game = Game::default();
     for msg in r {
+        game.pieces[0] = 0xFF00;
+        generate_pawn_moves(&game, WHITE);
         println!("engine received message");
     }
 }

+ 1 - 1
src/interface.rs

@@ -29,7 +29,7 @@ fn run_command(mut cmd: Vec<&str>, r: &Receiver<InterfaceMsg>, s: &Sender<Engine
 
 fn cmd_uci(_args: Vec<&str>) {
     println!("id name bishop 1.0");
-    println!("id author bishop");
+    println!("id author Geile Siech");
     println!("uciok");
 }
 

+ 59 - 17
src/movegen.rs

@@ -1,4 +1,5 @@
 use bitboard::Bitboard;
+use bitboard::*;
 use search::Game;
 
 pub type Square = u8;
@@ -9,12 +10,12 @@ pub const BLACK: Side = true;
 
 pub type PieceType = u8;
 
-pub const PAWN:     PieceType = 0;
-pub const KNIGHT:   PieceType = 1;
-pub const BISHOP:   PieceType = 2;
-pub const ROOK:     PieceType = 3;
-pub const QUEEN:    PieceType = 4;
-pub const KING:     PieceType = 5;
+pub const PAWN: PieceType = 0;
+pub const KNIGHT: PieceType = 1;
+pub const BISHOP: PieceType = 2;
+pub const ROOK: PieceType = 3;
+pub const QUEEN: PieceType = 4;
+pub const KING: PieceType = 5;
 
 
 pub struct SimpleMove {
@@ -23,13 +24,17 @@ pub struct SimpleMove {
 }
 
 pub enum Move {
-    Default     { mov: SimpleMove, capture: PieceType },
-    Castling    { side: Side, left: bool },
-    EnPassant   { side: Side, column: u8},
-    Promotion   { side: Side, column: u8, promote_to: PieceType },
+    Default { mov: SimpleMove, piece_type: PieceType, capture: PieceType },
+    Castling { side: Side, left: bool },
+    EnPassant { side: Side, column: u8 },
+    Promotion { side: Side, column: u8, promote_to: PieceType, capture: PieceType },
 }
 
-
+/**
+ * \brief Iterator to serialize bitboard bit for bit
+ *
+ * Extracts each bit out of the bitboard and returns each one as a separate bitboard.
+ */
 pub struct BitboardIterator { pub board: Bitboard }
 
 impl Iterator for BitboardIterator {
@@ -41,28 +46,65 @@ impl Iterator for BitboardIterator {
             self.board &= !lsb;
             //Some(lsb.trailing_zeros())
             Some(lsb)
+        } else {
+            None
         }
-            else {
-                None
-            }
     }
 }
 
 
-fn generate_pawn_moves(game: &Game, side: Side) 
-{
+pub fn generate_pawn_moves(game: &Game, side: Side) -> Bitboard {
+    let pushed = generate_pawn_pushes(game, side);
+    let iter = BitboardIterator { board: pushed };
+    for p in iter {
+        println!("{}", p.trailing_zeros());
+    }
+
+    return 0;
+}
+
+fn generate_pawn_pushes(game: &Game, side: Side) -> Bitboard {
     let pawns = game.pawns(side);
+    let others = game.get_all_side(!side);
+    let moved_pawns =
+        match side {
+            WHITE => north_one(pawns),
+            BLACK => south_one(pawns)
+        }
+            & !(ROW_1 | ROW_8);
+
+    let possible_pushes = moved_pawns & !others;
+    return possible_pushes;
 }
 
+fn generate_pawn_captures(game: &Game, side: Side, move_list: &mut Vec<Move>) {
+    let pawns = game.pawns(side);
+    let others = game.get_all_side(!side);
+    let moved_pawns_left =
+        match side {
+            WHITE => northeast_one(pawns),
+            BLACK => southeast_one(pawns)
+        };
+    let moved_pawns_right =
+        match side {
+            WHITE => northwest_one(pawns),
+            BLACK => southwest_one(pawns)
+        };
+
+    let left_captures = moved_pawns_left & others;
+    let right_captures = moved_pawns_right & others;
+
+}
 
 
 #[cfg(test)]
 mod tests {
     use search::Game;
     use movegen::*;
+
     #[test]
     fn pawn_pushes() {
-        let game: Game = Game::default();
+        let mut game: Game = Game::default();
         let pawn_moves = generate_pawn_moves(&game, WHITE);
         assert_eq!(pawn_moves, ());
     }

+ 10 - 3
src/search.rs

@@ -1,12 +1,10 @@
-use std::sync::mpsc::Receiver;
-
 use bitboard::Bitboard;
 use movegen::*;
 
 
 pub struct Game
 {
-    pieces: [Bitboard; 12],
+    pub pieces: [Bitboard; 12],
     en_passant: u8,
 }
 
@@ -68,6 +66,15 @@ impl Game {
             BLACK => self.pieces[11],
         }
     }
+
+    pub fn get_all_side(&self, side: Side) -> Bitboard {
+        match side {
+            WHITE => self.pieces[0] | self.pieces[1] | self.pieces[2] | self.pieces[3] |
+                self.pieces[4] | self.pieces[5],
+            BLACK => self.pieces[6] | self.pieces[7] | self.pieces[8] | self.pieces[9] |
+                self.pieces[10] | self.pieces[11]
+        }
+    }
 }
 
 pub fn search() {