board: use bitboards struct

This commit is contained in:
dece 2020-06-18 23:27:58 +02:00
parent a2ae6d6e73
commit 8485714a24

View file

@ -101,30 +101,48 @@ pub fn pos_string(p: &Pos) -> String {
String::from_utf8_lossy(&bytes).to_string() String::from_utf8_lossy(&bytes).to_string()
} }
/// Bitboard representation of a chess board. /// Bitboard for color or piece bits.
/// pub type Bitboard = u64;
/// 64 squares, from A1, A2 to H7, H8. A square is an u8, with bits
/// defining the state of the square. const BB_WH: u8 = 0;
pub type Board = [u8; 64]; const BB_BL: u8 = 1;
const BB_P: u8 = 0;
const BB_B: u8 = 1;
const BB_N: u8 = 2;
const BB_R: u8 = 3;
const BB_Q: u8 = 4;
const BB_K: u8 = 5;
/// Board representation with color/piece bitboards.
pub struct Board {
pub color: [Bitboard; 2],
pub piece: [Bitboard; 6],
}
/// Generate the board of a new game. /// Generate the board of a new game.
pub const fn new() -> Board { pub const fn new() -> Board {
[ Board {
/* 1 2 3 4 5 6 7 8 */ color: [
/* A */ SQ_WH_R, SQ_WH_P, SQ_E, SQ_E, SQ_E, SQ_E, SQ_BL_P, SQ_BL_R, 0b11000000_11000000_11000000_11000000_11000000_11000000_11000000_11000000,
/* B */ SQ_WH_N, SQ_WH_P, SQ_E, SQ_E, SQ_E, SQ_E, SQ_BL_P, SQ_BL_N, 0b00000011_00000011_00000011_00000011_00000011_00000011_00000011_00000011,
/* C */ SQ_WH_B, SQ_WH_P, SQ_E, SQ_E, SQ_E, SQ_E, SQ_BL_P, SQ_BL_B, ],
/* D */ SQ_WH_Q, SQ_WH_P, SQ_E, SQ_E, SQ_E, SQ_E, SQ_BL_P, SQ_BL_Q, piece: [
/* E */ SQ_WH_K, SQ_WH_P, SQ_E, SQ_E, SQ_E, SQ_E, SQ_BL_P, SQ_BL_K, 0b01000010_01000010_01000010_01000010_01000010_01000010_01000010_01000010,
/* F */ SQ_WH_B, SQ_WH_P, SQ_E, SQ_E, SQ_E, SQ_E, SQ_BL_P, SQ_BL_B, 0b00000000_00000000_10000001_00000000_00000000_10000001_00000000_00000000,
/* G */ SQ_WH_N, SQ_WH_P, SQ_E, SQ_E, SQ_E, SQ_E, SQ_BL_P, SQ_BL_N, 0b00000000_10000001_00000000_00000000_00000000_00000000_10000001_00000000,
/* H */ SQ_WH_R, SQ_WH_P, SQ_E, SQ_E, SQ_E, SQ_E, SQ_BL_P, SQ_BL_R, 0b10000001_00000000_00000000_00000000_00000000_00000000_00000000_10000001,
] 0b00000000_00000000_00000000_10000001_00000000_00000000_00000000_00000000,
0b00000000_00000000_00000000_00000000_10000001_00000000_00000000_00000000,
]
}
} }
/// Generate an empty board. /// Generate an empty board.
pub const fn new_empty() -> Board { pub const fn new_empty() -> Board {
[SQ_E; 64] Board {
color: [0; 2],
piece: [0; 6],
}
} }
/// Generate a board from a FEN placement string. /// Generate a board from a FEN placement string.
@ -154,11 +172,6 @@ pub fn new_from_fen(fen: &str) -> Board {
board board
} }
/// Return true of both boards are equal.
pub fn eq(b1: &Board, b2: &Board) -> bool {
b1.iter().zip(b2.iter()).all(|(a, b)| a == b)
}
/// Get value of the square at this position. /// Get value of the square at this position.
#[inline] #[inline]
pub const fn get_square(board: &Board, coords: &Pos) -> u8 { pub const fn get_square(board: &Board, coords: &Pos) -> u8 {
@ -280,17 +293,6 @@ mod tests {
assert!(eq(&b1, &b2)); assert!(eq(&b1, &b2));
} }
#[test]
fn test_eq() {
let mut b1 = new();
let b2 = new();
assert!(eq(&b1, &b2));
set_square(&mut b1, &pos("a1"), SQ_E);
assert!(!eq(&b1, &b2));
set_square(&mut b1, &pos("a1"), SQ_WH_R);
assert!(eq(&b1, &b2));
}
#[test] #[test]
fn test_get_square() { fn test_get_square() {
let b = new(); let b = new();