Skip to content

Commit

Permalink
Piece Square evaluation
Browse files Browse the repository at this point in the history
  • Loading branch information
Alex Johnson committed Dec 4, 2015
1 parent dba7ff8 commit b68747e
Showing 1 changed file with 71 additions and 1 deletion.
72 changes: 71 additions & 1 deletion src/evaluation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,70 @@ pub static SAFE_MASK: [u64; 2] = [
(FILE_C | FILE_D | FILE_E | FILE_F) & (ROW_2 | ROW_3 | ROW_4)
];

/// Piece Square boards are relative to black
fn flip_vertical(square: u32) -> u32 {
lsb((1u64 << square).swap_bytes())
}

fn rel_loc(square: u32, color: u8) -> usize {
if color == WHITE { flip_vertical(square) as usize } else { square as usize }
}

pub static PAWN_SQUARE: [i32; 64] = [
0, 0, 0, 0, 0, 0, 0, 0,
50, 50, 50, 50, 50, 50, 50, 50,
10, 10, 20, 30, 30, 20, 10, 10,
5, 5, 10, 25, 25, 10, 5, 5,
0, 0, 0, 20, 20, 0, 0, 0,
5, -5,-10, 0, 0,-10, -5, 5,
5, 10, 10,-20,-20, 10, 10, 5,
0, 0, 0, 0, 0, 0, 0, 0,
];

pub static KNIGHT_SQUARE: [i32; 64] = [
-50,-40,-30,-30,-30,-30,-40,-50,
-40,-20, 0, 0, 0, 0,-20,-40,
-30, 0, 10, 15, 15, 10, 0,-30,
-30, 5, 15, 20, 20, 15, 5,-30,
-30, 0, 15, 20, 20, 15, 0,-30,
-30, 5, 10, 15, 15, 10, 5,-30,
-40,-20, 0, 5, 5, 0,-20,-40,
-50,-40,-30,-30,-30,-30,-40,-50,
];

pub static BISHOP_SQUARE: [i32; 64] = [
-20,-10,-10,-10,-10,-10,-10,-20,
-10, 0, 0, 0, 0, 0, 0,-10,
-10, 0, 5, 10, 10, 5, 0,-10,
-10, 5, 5, 10, 10, 5, 5,-10,
-10, 0, 10, 10, 10, 10, 0,-10,
-10, 10, 10, 10, 10, 10, 10,-10,
-10, 5, 0, 0, 0, 0, 5,-10,
-20,-10,-10,-10,-10,-10,-10,-20,
];

pub static ROOK_SQUARE: [i32; 64] = [
0, 0, 0, 0, 0, 0, 0, 0,
5, 10, 10, 10, 10, 10, 10, 5,
-5, 0, 0, 0, 0, 0, 0, -5,
-5, 0, 0, 0, 0, 0, 0, -5,
-5, 0, 0, 0, 0, 0, 0, -5,
-5, 0, 0, 0, 0, 0, 0, -5,
-5, 0, 0, 0, 0, 0, 0, -5,
0, 0, 0, 5, 5, 0, 0, 0,
];

pub static QUEEN_SQUARE: [i32; 64] = [
-20,-10,-10, -5, -5,-10,-10,-20,
-10, 0, 0, 0, 0, 0, 0,-10,
-10, 0, 5, 5, 5, 5, 0,-10,
-5, 0, 5, 5, 5, 5, 0, -5,
0, 0, 5, 5, 5, 5, 0, -5,
-10, 5, 5, 5, 5, 5, 0,-10,
-10, 0, 5, 0, 0, 0, 0,-10,
-20,-10,-10, -5, -5,-10,-10,-20,
];

impl Board {
// Attack map by square
// Piece Values by Square
Expand All @@ -21,6 +85,7 @@ impl Board {
// Bishop pair
// Pawn on same color as bishop
// Trapped bishop
// Symmetric move generation and evaluation

pub fn eval_space(&self, us: u8, attacked_by: &mut BitBoard) -> u32 {
let opp = flip(us);
Expand Down Expand Up @@ -51,13 +116,15 @@ impl Board {
let occ = allies | enemies;

let mut eval = 0;
let mut piece_sq = 0;

for_all(bb[QUEEN | us], &mut |from| {
let att = queen_moves(from, occ);
eval += count(att & !occ) * 5 +
count(att & enemies) * 15 +
count(att & allies) * 8;
attacked_by[QUEEN | us] |= att;
piece_sq += QUEEN_SQUARE[rel_loc(from, us)];
});

for_all(bb[ROOK | us], &mut |from| {
Expand All @@ -66,6 +133,7 @@ impl Board {
count(att & enemies) * 20 +
count(att & allies) * 15;
attacked_by[ROOK | us] |= att;
piece_sq += ROOK_SQUARE[rel_loc(from, us)];
});

if count(bb[BISHOP | us]) == 2 { eval += 100 } // Ignore bishop promotions
Expand All @@ -76,6 +144,7 @@ impl Board {
count(att & enemies) * 30 +
count(att & allies) * 15;
attacked_by[BISHOP | us] |= att;
piece_sq += BISHOP_SQUARE[rel_loc(from, us)];
});

for_all(bb[KNIGHT | us], &mut |from| {
Expand All @@ -84,6 +153,7 @@ impl Board {
count(att & enemies) * 35 +
count(att & allies) * 15;
attacked_by[KNIGHT | us] |= att;
piece_sq += KNIGHT_SQUARE[rel_loc(from, us)];
});

for_all(bb[KING | us], &mut |from| {
Expand All @@ -100,7 +170,7 @@ impl Board {
count(bb[ROOK | us]) * p_val(ROOK) +
count(bb[QUEEN | us]) * p_val(QUEEN);

(eval + material) as i32
(material + eval) as i32 + piece_sq
}

/// Return a static evaluation relative to the player to move in milli-pawns
Expand Down

0 comments on commit b68747e

Please sign in to comment.