Spaces:
Runtime error
Runtime error
File size: 12,003 Bytes
3014f14 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 | use crate::types::*;
use crate::bitboard::{Bitboard, EMPTY, get_bit, pop_lsb};
#[derive(Clone)]
pub struct Position {
pub pieces: [Bitboard; 7],
pub colors: [Bitboard; 2],
pub side_to_move: Color,
pub castling_rights: u8,
pub ep_square: Square,
pub halfmove_clock: u32,
pub fullmove_number: u32,
pub history: Vec<UndoState>,
}
#[derive(Clone)]
pub struct UndoState {
pub castling_rights: u8,
pub ep_square: Square,
pub halfmove_clock: u32,
pub captured: PieceType,
}
impl Position {
pub fn new() -> Self {
let mut pos = Position {
pieces: [EMPTY; 7],
colors: [EMPTY; 2],
side_to_move: Color::White,
castling_rights: 0,
ep_square: Square::None,
halfmove_clock: 0,
fullmove_number: 1,
history: Vec::new(),
};
pos.set_fen("rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1");
pos
}
pub fn get_piece_at(&self, sq: Square) -> PieceType {
for p in 1..=6 {
if get_bit(self.pieces[p], sq) {
return unsafe { std::mem::transmute(p as u8) };
}
}
PieceType::None
}
pub fn set_fen(&mut self, fen: &str) {
let tokens: Vec<&str> = fen.split_whitespace().collect();
if tokens.is_empty() { return; }
for p in &mut self.pieces { *p = EMPTY; }
for c in &mut self.colors { *c = EMPTY; }
let mut rank = 7;
let mut file = 0;
for c in tokens[0].chars() {
if c == '/' {
rank -= 1;
file = 0;
} else if let Some(digit) = c.to_digit(10) {
file += digit as u8;
} else {
let sq = Square::from_u8(rank * 8 + file);
let color = if c.is_uppercase() { Color::White } else { Color::Black };
let p_type = match c.to_ascii_lowercase() {
'p' => PieceType::Pawn,
'n' => PieceType::Knight,
'b' => PieceType::Bishop,
'r' => PieceType::Rook,
'q' => PieceType::Queen,
'k' => PieceType::King,
_ => PieceType::None,
};
self.pieces[p_type as usize] |= 1 << (sq as u8);
self.colors[color as usize] |= 1 << (sq as u8);
file += 1;
}
}
self.side_to_move = if tokens.get(1).copied().unwrap_or("w") == "w" { Color::White } else { Color::Black };
self.castling_rights = 0;
if let Some(&c_str) = tokens.get(2) {
if c_str != "-" {
for c in c_str.chars() {
match c {
'K' => self.castling_rights |= CastlingRights::WHITE_OO,
'Q' => self.castling_rights |= CastlingRights::WHITE_OOO,
'k' => self.castling_rights |= CastlingRights::BLACK_OO,
'q' => self.castling_rights |= CastlingRights::BLACK_OOO,
_ => {}
}
}
}
}
self.ep_square = Square::None;
if let Some(&ep_str) = tokens.get(3) {
if ep_str != "-" && ep_str.len() == 2 {
let chars: Vec<char> = ep_str.chars().collect();
let f = (chars[0] as u8) - b'a';
let r = (chars[1] as u8) - b'1';
self.ep_square = Square::from_u8(r * 8 + f);
}
}
self.halfmove_clock = tokens.get(4).and_then(|s| s.parse().ok()).unwrap_or(0);
self.fullmove_number = tokens.get(5).and_then(|s| s.parse().ok()).unwrap_or(1);
}
pub fn do_move(&mut self, m: Move) -> bool {
let from = m.from();
let to = m.to();
let promo = m.promotion();
let us = self.side_to_move;
let them = us.flip();
let p_type = self.get_piece_at(from);
let captured = self.get_piece_at(to);
self.history.push(UndoState {
castling_rights: self.castling_rights,
ep_square: self.ep_square,
halfmove_clock: self.halfmove_clock,
captured,
});
let from_bb = 1 << (from as u8);
let to_bb = 1 << (to as u8);
self.pieces[p_type as usize] ^= from_bb;
self.colors[us as usize] ^= from_bb;
if captured != PieceType::None {
self.pieces[captured as usize] ^= to_bb;
self.colors[them as usize] ^= to_bb;
self.halfmove_clock = 0;
} else {
self.halfmove_clock += 1;
}
if p_type == PieceType::Pawn {
self.halfmove_clock = 0;
if promo != PieceType::None {
self.pieces[promo as usize] |= to_bb;
} else {
self.pieces[p_type as usize] |= to_bb;
}
if to == self.ep_square {
let ep_cap_sq = Square::from_u8(if us == Color::White { (to as u8) - 8 } else { (to as u8) + 8 });
self.pieces[PieceType::Pawn as usize] ^= 1 << (ep_cap_sq as u8);
self.colors[them as usize] ^= 1 << (ep_cap_sq as u8);
}
if (from as i8 - to as i8).abs() == 16 {
self.ep_square = Square::from_u8(if us == Color::White { (from as u8) + 8 } else { (from as u8) - 8 });
} else {
self.ep_square = Square::None;
}
} else {
self.pieces[p_type as usize] |= to_bb;
self.ep_square = Square::None;
}
self.colors[us as usize] |= to_bb;
if p_type == PieceType::King {
if us == Color::White {
if from == Square::E1 && to == Square::G1 {
self.pieces[PieceType::Rook as usize] ^= (1 << (Square::H1 as u8)) | (1 << (Square::F1 as u8));
self.colors[Color::White as usize] ^= (1 << (Square::H1 as u8)) | (1 << (Square::F1 as u8));
} else if from == Square::E1 && to == Square::C1 {
self.pieces[PieceType::Rook as usize] ^= (1 << (Square::A1 as u8)) | (1 << (Square::D1 as u8));
self.colors[Color::White as usize] ^= (1 << (Square::A1 as u8)) | (1 << (Square::D1 as u8));
}
self.castling_rights &= !CastlingRights::ANY_WHITE;
} else {
if from == Square::E8 && to == Square::G8 {
self.pieces[PieceType::Rook as usize] ^= (1 << (Square::H8 as u8)) | (1 << (Square::F8 as u8));
self.colors[Color::Black as usize] ^= (1 << (Square::H8 as u8)) | (1 << (Square::F8 as u8));
} else if from == Square::E8 && to == Square::C8 {
self.pieces[PieceType::Rook as usize] ^= (1 << (Square::A8 as u8)) | (1 << (Square::D8 as u8));
self.colors[Color::Black as usize] ^= (1 << (Square::A8 as u8)) | (1 << (Square::D8 as u8));
}
self.castling_rights &= !CastlingRights::ANY_BLACK;
}
}
if from == Square::A1 || to == Square::A1 { self.castling_rights &= !CastlingRights::WHITE_OOO; }
if from == Square::H1 || to == Square::H1 { self.castling_rights &= !CastlingRights::WHITE_OO; }
if from == Square::A8 || to == Square::A8 { self.castling_rights &= !CastlingRights::BLACK_OOO; }
if from == Square::H8 || to == Square::H8 { self.castling_rights &= !CastlingRights::BLACK_OO; }
if us == Color::Black {
self.fullmove_number += 1;
}
self.side_to_move = them;
let king_sq = pop_lsb(&mut (self.pieces[PieceType::King as usize] & self.colors[us as usize]));
if self.is_square_attacked(Square::from_u8(king_sq), us) {
self.undo_move(m);
return false;
}
true
}
pub fn undo_move(&mut self, m: Move) {
let state = self.history.pop().unwrap();
self.side_to_move = self.side_to_move.flip();
let us = self.side_to_move;
let them = us.flip();
let from = m.from();
let to = m.to();
let promo = m.promotion();
let p_type = if promo != PieceType::None { PieceType::Pawn } else { self.get_piece_at(to) };
let from_bb = 1 << (from as u8);
let to_bb = 1 << (to as u8);
self.pieces[p_type as usize] |= from_bb;
self.colors[us as usize] |= from_bb;
if promo != PieceType::None {
self.pieces[promo as usize] ^= to_bb;
} else {
self.pieces[p_type as usize] ^= to_bb;
}
self.colors[us as usize] ^= to_bb;
if state.captured != PieceType::None {
self.pieces[state.captured as usize] |= to_bb;
self.colors[them as usize] |= to_bb;
}
if p_type == PieceType::Pawn && to == state.ep_square {
let ep_cap_sq = Square::from_u8(if us == Color::White { (to as u8) - 8 } else { (to as u8) + 8 });
self.pieces[PieceType::Pawn as usize] |= 1 << (ep_cap_sq as u8);
self.colors[them as usize] |= 1 << (ep_cap_sq as u8);
}
if p_type == PieceType::King {
if us == Color::White {
if from == Square::E1 && to == Square::G1 {
self.pieces[PieceType::Rook as usize] ^= (1 << (Square::H1 as u8)) | (1 << (Square::F1 as u8));
self.colors[Color::White as usize] ^= (1 << (Square::H1 as u8)) | (1 << (Square::F1 as u8));
} else if from == Square::E1 && to == Square::C1 {
self.pieces[PieceType::Rook as usize] ^= (1 << (Square::A1 as u8)) | (1 << (Square::D1 as u8));
self.colors[Color::White as usize] ^= (1 << (Square::A1 as u8)) | (1 << (Square::D1 as u8));
}
} else {
if from == Square::E8 && to == Square::G8 {
self.pieces[PieceType::Rook as usize] ^= (1 << (Square::H8 as u8)) | (1 << (Square::F8 as u8));
self.colors[Color::Black as usize] ^= (1 << (Square::H8 as u8)) | (1 << (Square::F8 as u8));
} else if from == Square::E8 && to == Square::C8 {
self.pieces[PieceType::Rook as usize] ^= (1 << (Square::A8 as u8)) | (1 << (Square::D8 as u8));
self.colors[Color::Black as usize] ^= (1 << (Square::A8 as u8)) | (1 << (Square::D8 as u8));
}
}
}
self.castling_rights = state.castling_rights;
self.ep_square = state.ep_square;
self.halfmove_clock = state.halfmove_clock;
if us == Color::Black {
self.fullmove_number -= 1;
}
}
pub fn is_square_attacked(&self, sq: Square, side_by: Color) -> bool {
let occ = self.colors[0] | self.colors[1];
let knight_attacks = crate::movegen::knight_mask(sq);
if (knight_attacks & self.pieces[PieceType::Knight as usize] & self.colors[side_by as usize]) != 0 { return true; }
let king_attacks = crate::movegen::king_mask(sq);
if (king_attacks & self.pieces[PieceType::King as usize] & self.colors[side_by as usize]) != 0 { return true; }
let pawn_attacks = crate::movegen::pawn_attack_mask(sq, side_by.flip());
if (pawn_attacks & self.pieces[PieceType::Pawn as usize] & self.colors[side_by as usize]) != 0 { return true; }
if (crate::movegen::sliding_attacks(sq, occ, true) & (self.pieces[PieceType::Bishop as usize] | self.pieces[PieceType::Queen as usize]) & self.colors[side_by as usize]) != 0 { return true; }
if (crate::movegen::sliding_attacks(sq, occ, false) & (self.pieces[PieceType::Rook as usize] | self.pieces[PieceType::Queen as usize]) & self.colors[side_by as usize]) != 0 { return true; }
false
}
}
|