Kaynağa Gözat

workong on ot

Nicolas Winkler 3 yıl önce
ebeveyn
işleme
3083c30e31
3 değiştirilmiş dosya ile 94 ekleme ve 42 silme
  1. 71 39
      src/game.rs
  2. 19 1
      src/interface.rs
  3. 4 2
      src/search.rs

+ 71 - 39
src/game.rs

@@ -6,7 +6,15 @@ pub struct Game
 {
     pub pieces: [Bitboard; 12],
     pub turn: Side,
-    pub en_passant: u8,
+    pub en_passant: Option<u8>,
+
+    ///
+    /// castling rights in the following order
+    /// white kingside, white queenside, black kingside, black queenside
+    /// 
+    pub castling_rights: [bool; 4],
+    pub halfmoves_since_last_event: i32,
+    pub turn_number: i32,
 }
 
 impl Default for Game {
@@ -27,61 +35,85 @@ impl Default for Game {
                 0x08 << 56,
             ],
             turn: WHITE,
-            en_passant: 0,
+            en_passant: None,
+            castling_rights: [true, true, true, true],
+            halfmoves_since_last_event: 0,
+            turn_number: 0
         }
     }
 }
 
 impl Game {
 
-    pub fn from_fen(fen: &str) -> Option<Game> {
+    pub fn from_fen(fen: &[&str]) -> Option<Game> {
         let mut game: Game = Game::default();
         let example = "rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1";
-        let mut fen_parts = fen.split_whitespace();
 
-        match fen_parts.next() {
-            Some(position) => {
-                let rows = position.split('/');
-                let mut row_index = 0;
-                for row in rows {
-                    if row_index >= 8 {
+        let position = fen[0];
+        let turn = fen[1];
+        let castling_rights = fen[2];
+        let en_passant = fen[3];
+        let halfmoves_since_last_event = fen[4];
+        let turn_number = fen[5];
+
+        {
+            let rows = position.split('/');
+            let mut row_index = 0;
+            for row in rows {
+                if row_index >= 8 {
+                    return None;
+                }
+                let mut col_index = 0;
+                for c in row.chars() {
+
+                    if col_index >= 8 {
                         return None;
                     }
-                    let mut col_index = 0;
-                    for c in row.chars() {
 
-                        if col_index >= 8 {
-                            return None;
+                    let bit_to_set = from_indices(col_index, row_index);
+                    match c {
+                        'p' => { *game.pawns_mut(BLACK) |= bit_to_set },
+                        'n' => { *game.knights_mut(BLACK) |= bit_to_set },
+                        'b' => { *game.bishops_mut(BLACK) |= bit_to_set },
+                        'r' => { *game.rooks_mut(BLACK) |= bit_to_set },
+                        'q' => { *game.queens_mut(BLACK) |= bit_to_set },
+                        'k' => { *game.kings_mut(BLACK) |= bit_to_set },
+                        'P' => { *game.pawns_mut(WHITE) |= bit_to_set },
+                        'N' => { *game.knights_mut(WHITE) |= bit_to_set },
+                        'B' => { *game.bishops_mut(WHITE) |= bit_to_set },
+                        'R' => { *game.rooks_mut(WHITE) |= bit_to_set },
+                        'Q' => { *game.queens_mut(WHITE) |= bit_to_set },
+                        'K' => { *game.kings_mut(WHITE) |= bit_to_set },
+                        num => {
+                            col_index += match num.to_digit(10) { Some(x) => x, None => {
+                                return None;
+                            }} as u8 - 1;
                         }
-
-                        let bit_to_set = from_indices(col_index, row_index);
-                        match c {
-                            'p' => { *game.pawns_mut(BLACK) |= bit_to_set },
-                            'n' => { *game.knights_mut(BLACK) |= bit_to_set },
-                            'b' => { *game.bishops_mut(BLACK) |= bit_to_set },
-                            'r' => { *game.rooks_mut(BLACK) |= bit_to_set },
-                            'q' => { *game.queens_mut(BLACK) |= bit_to_set },
-                            'k' => { *game.kings_mut(BLACK) |= bit_to_set },
-                            'P' => { *game.pawns_mut(WHITE) |= bit_to_set },
-                            'N' => { *game.knights_mut(WHITE) |= bit_to_set },
-                            'B' => { *game.bishops_mut(WHITE) |= bit_to_set },
-                            'R' => { *game.rooks_mut(WHITE) |= bit_to_set },
-                            'Q' => { *game.queens_mut(WHITE) |= bit_to_set },
-                            'K' => { *game.kings_mut(WHITE) |= bit_to_set },
-                            num => {
-                                col_index += match num.to_digit(10) { Some(x) => x, None => {
-                                    return None;
-                                }} as u8 - 1;
-                            }
-                        }
-                        col_index += 1;
                     }
+                    col_index += 1;
                 }
-            },
-            None => return None
+            }
         }
 
-        None
+        // parse castling rights
+        game.castling_rights[0] = castling_rights.contains('K');
+        game.castling_rights[1] = castling_rights.contains('Q');
+        game.castling_rights[2] = castling_rights.contains('k');
+        game.castling_rights[3] = castling_rights.contains('q');
+
+        // parse castling rights
+        game.en_passant = en_passant.parse::<u8>().ok();
+
+        game.halfmoves_since_last_event = halfmoves_since_last_event.parse::<i32>().unwrap_or(0);
+        game.turn_number = turn_number.parse::<i32>().unwrap_or(0);
+        
+        Some(game)
+    }
+
+    pub fn from_fen_str(fen: &str) -> Option<Game> {
+        let mut fen_parts = fen.split_whitespace();
+
+        Self::from_fen(fen_parts.collect::<Vec<&str>>().as_slice())
     }
 
     pub fn parse_move(&self, mov: &str) -> Result<Move, ()> {

+ 19 - 1
src/interface.rs

@@ -6,11 +6,25 @@ use std::process::exit;
 use engine::{EngineMsg, InterfaceMsg};
 use game::{Game};
 
+use std::fs::File;
+use std::io::Write;
+use std::cell::RefCell;
+
+
+thread_local!(static debug_log: RefCell<File> = RefCell::new(File::create("debug.log").unwrap()));
+
+
 
 pub fn run(r: Receiver<InterfaceMsg>, s: Sender<EngineMsg>) {
+    //unsafe { debug_log = Box::new(File::open("debug.log").unwrap()); }
     let stdin = io::stdin();
     for line_m in stdin.lock().lines() {
         let line = line_m.unwrap();
+
+        debug_log.with(|fc| 
+            writeln!(fc.borrow_mut(), "received line: {}", line)
+        );
+
         let split = line.split_whitespace().collect::<Vec<&str>>();
         run_command(split, &r, &s);
     }
@@ -51,7 +65,11 @@ fn cmd_position(mut args: Vec<&str>, r: &Receiver<InterfaceMsg>, s: &Sender<Engi
 
     }
     else if position == "fen" {
-        let fen_parts: Vec<String> = Vec::from(&args[1..6]).into_iter().map(|x| x.to_owned()).collect::<Vec<String>>();
+        let fen_parts: Vec<String> = Vec::from(&args[0..6]).into_iter().map(|x| x.to_owned() + " ").collect::<Vec<String>>();
+
+        debug_log.with(|fc| 
+            writeln!(fc.borrow_mut(), "fen string is: {:?}", fen_parts)
+        );
         
         // "3N4/5P2/2K1P1pP/p2PNn2/1p3r2/2P4P/4k2B/8 w - - 0 1"
     }

+ 4 - 2
src/search.rs

@@ -99,9 +99,11 @@ pub fn apply_move(game: &Game, mov: Move) -> Game {
         Move::EnPassant { side: side, column: col } => {
             
             let pawns = game.pawns(side);
-            if pawns & A_FILE >> game.en_passant & (ROW_4 | ROW_5) != 0 {
-                if pawns & A_FILE >> game.en_passant & (ROW_4 | ROW_5) != 0 {
+            if let Some(ep) = game.en_passant {
+                if pawns & A_FILE >> ep & (ROW_4 | ROW_5) != 0 {
+                    if pawns & A_FILE >> ep & (ROW_4 | ROW_5) != 0 {
 
+                    }
                 }
             }
             panic!("oh no");