Skip to content

Commit

Permalink
revert
Browse files Browse the repository at this point in the history
  • Loading branch information
Heiaha committed Nov 17, 2024
1 parent ce0caf7 commit 6264882
Show file tree
Hide file tree
Showing 13 changed files with 278 additions and 308 deletions.
2 changes: 1 addition & 1 deletion src/bitboard.rs
Original file line number Diff line number Diff line change
Expand Up @@ -271,7 +271,7 @@ impl fmt::Debug for Bitboard {
let mut result = String::new();
for i in (0..=56).rev().step_by(8) {
for j in 0..8 {
result.push_str(format!("{} ", self.0 >> (i + j) & 1).as_str());
result.push_str(format!("{} ", self.0 >> (i + j) & 1).as_ref());
}
result.push('\n');
}
Expand Down
276 changes: 135 additions & 141 deletions src/board.rs

Large diffs are not rendered by default.

38 changes: 21 additions & 17 deletions src/moov.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,6 @@ impl Move {
MoveFlags::from(((self.0 >> 12) & 0xf) as u8)
}

pub fn move_int(&self) -> MoveInt {
self.0
}

pub fn is_quiet(&self) -> bool {
(self.0 >> 12) & 0b1100 == 0
}
Expand All @@ -40,19 +36,23 @@ impl Move {
self.flags() == MoveFlags::EnPassant
}

pub fn promotion(&self) -> Option<PieceType> {
pub fn promotion(&self) -> PieceType {
match self.flags() {
MoveFlags::PrKnight | MoveFlags::PcKnight => Some(PieceType::Knight),
MoveFlags::PrBishop | MoveFlags::PcBishop => Some(PieceType::Bishop),
MoveFlags::PrRook | MoveFlags::PcRook => Some(PieceType::Rook),
MoveFlags::PrQueen | MoveFlags::PcQueen => Some(PieceType::Queen),
_ => None,
MoveFlags::PrKnight | MoveFlags::PcKnight => PieceType::Knight,
MoveFlags::PrBishop | MoveFlags::PcBishop => PieceType::Bishop,
MoveFlags::PrRook | MoveFlags::PcRook => PieceType::Rook,
MoveFlags::PrQueen | MoveFlags::PcQueen => PieceType::Queen,
_ => PieceType::None,
}
}

pub fn is_castling(&self) -> bool {
matches!(self.flags(), MoveFlags::OO | MoveFlags::OOO)
}

pub fn is_null(&self) -> bool {
*self == Move::NULL
}
}

impl From<MoveInt> for Move {
Expand All @@ -63,16 +63,14 @@ impl From<MoveInt> for Move {

impl fmt::Display for Move {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "{}{}", self.from_sq(), self.to_sq())?;

if let Some(promotion_pt) = self.promotion() {
write!(f, "{}", promotion_pt)?;
}

Ok(())
write!(f, "{}{}{}", self.from_sq(), self.to_sq(), self.promotion())
}
}

impl Move {
pub const NULL: Self = Self(0);
}

#[derive(Debug, PartialEq, Eq)]
pub enum MoveFlags {
Quiet = 0b0000,
Expand All @@ -91,6 +89,12 @@ pub enum MoveFlags {
PcQueen = 0b1111,
}

impl Default for MoveFlags {
fn default() -> Self {
MoveFlags::Quiet
}
}

impl From<u8> for MoveFlags {
fn from(n: u8) -> Self {
unsafe { std::mem::transmute::<u8, Self>(n) }
Expand Down
27 changes: 12 additions & 15 deletions src/move_list.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ const MAX_MOVES: usize = 254;
const MAX_MOVES: usize = 255;

pub struct MoveList {
moves: [Option<Move>; MAX_MOVES],
moves: [Move; MAX_MOVES],
pub scores: [Value; MAX_MOVES],
idx: usize,
len: usize,
Expand All @@ -30,7 +30,7 @@ pub struct MoveList {
impl MoveList {
pub fn new() -> Self {
Self {
moves: [None; MAX_MOVES],
moves: [Move::NULL; MAX_MOVES],
scores: [0; MAX_MOVES],
idx: 0,
len: 0,
Expand All @@ -54,31 +54,31 @@ impl MoveList {
}

pub fn contains(&self, m: Move) -> bool {
self.moves[..self.len].contains(&Some(m))
self.moves[..self.len].contains(&m)
}

pub fn push(&mut self, m: Move) {
self.moves[self.len] = Some(m);
self.moves[self.len] = m;
self.len += 1;
}

pub fn make_q(&mut self, from_sq: SQ, to: Bitboard) {
for to_sq in to {
self.moves[self.len] = Some(Move::new(from_sq, to_sq, MoveFlags::Quiet));
self.moves[self.len] = Move::new(from_sq, to_sq, MoveFlags::Quiet);
self.len += 1;
}
}

pub fn make_c(&mut self, from_sq: SQ, to: Bitboard) {
for to_sq in to {
self.moves[self.len] = Some(Move::new(from_sq, to_sq, MoveFlags::Capture));
self.moves[self.len] = Move::new(from_sq, to_sq, MoveFlags::Capture);
self.len += 1;
}
}

pub fn make_dp(&mut self, from_sq: SQ, to: Bitboard) {
for to_sq in to {
self.moves[self.len] = Some(Move::new(from_sq, to_sq, MoveFlags::DoublePush));
self.moves[self.len] = Move::new(from_sq, to_sq, MoveFlags::DoublePush);
self.len += 1;
}
}
Expand All @@ -91,7 +91,7 @@ impl MoveList {
MoveFlags::PcRook,
MoveFlags::PcBishop,
] {
self.moves[self.len] = Some(Move::new(from_sq, to_sq, flag));
self.moves[self.len] = Move::new(from_sq, to_sq, flag);
self.len += 1;
}
}
Expand All @@ -117,7 +117,7 @@ impl MoveList {

let idx = self.idx;
self.idx += 1;
self.moves[idx]
Some(self.moves[idx])
}
}

Expand All @@ -132,26 +132,23 @@ impl Iterator for MoveList {

let idx = self.idx;
self.idx += 1;
self.moves[idx]
Some(self.moves[idx])
}
}

impl Index<usize> for MoveList {
type Output = Move;

fn index(&self, i: usize) -> &Self::Output {
self.moves[i]
.as_ref()
.expect("Tried to access an empty slot in MoveList")
&self.moves[i]
}
}

impl fmt::Debug for MoveList {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
let mut result = String::from('[');
for i in 0..self.len {
let m = self.moves[i].expect("None found in MoveList");
result.push_str(format!("{}, ", m).as_str());
result.push_str(format!("{}, ", self.moves[i]).as_ref());
}
result.push(']');
write!(f, "{}", result)
Expand Down
65 changes: 20 additions & 45 deletions src/move_sorter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,32 +9,26 @@ use super::square::*;
use super::types::*;

pub struct MoveSorter {
killer_moves: [[[Option<Move>; Self::N_KILLERS]; MAX_MOVES]; Color::N_COLORS],
killer_moves: [[[Move; Self::N_KILLERS]; MAX_MOVES]; Color::N_COLORS],
history_scores: [[Value; SQ::N_SQUARES]; SQ::N_SQUARES],
}

impl MoveSorter {
pub fn new() -> Self {
Self {
killer_moves: [[[None; Self::N_KILLERS]; MAX_MOVES]; Color::N_COLORS],
killer_moves: [[[Move::NULL; Self::N_KILLERS]; MAX_MOVES]; Color::N_COLORS],
history_scores: [[0; SQ::N_SQUARES]; SQ::N_SQUARES],
}
}

pub fn score_moves(
&self,
moves: &mut MoveList,
board: &Board,
ply: Ply,
hash_move: Option<Move>,
) {
pub fn score_moves(&self, moves: &mut MoveList, board: &Board, ply: Ply, hash_move: Move) {
for idx in 0..moves.len() {
moves.scores[idx] = self.score_move(moves[idx], board, ply, hash_move);
}
}

fn score_move(&self, m: Move, board: &Board, ply: Ply, hash_move: Option<Move>) -> Value {
if Some(m) == hash_move {
fn score_move(&self, m: Move, board: &Board, ply: Ply, hash_move: Move) -> Value {
if m == hash_move {
return Self::HASH_MOVE_SCORE;
}

Expand Down Expand Up @@ -65,34 +59,27 @@ impl MoveSorter {
}

score += match m.promotion() {
Some(PieceType::Knight) => Self::KNIGHT_PROMOTION_SCORE,
Some(PieceType::Bishop) => Self::BISHOP_PROMOTION_SCORE,
Some(PieceType::Rook) => Self::ROOK_PROMOTION_SCORE,
Some(PieceType::Queen) => Self::QUEEN_PROMOTION_SCORE,
None => 0,
PieceType::Knight => Self::KNIGHT_PROMOTION_SCORE,
PieceType::Bishop => Self::BISHOP_PROMOTION_SCORE,
PieceType::Rook => Self::ROOK_PROMOTION_SCORE,
PieceType::Queen => Self::QUEEN_PROMOTION_SCORE,
PieceType::None => 0,
_ => unreachable!(),
};
score
}

fn mvv_lva_score(board: &Board, m: Move) -> Value {
Self::MVV_LVA_SCORES[board
.piece_type_at(m.to_sq())
.expect("No captured in MVVLVA.")
.index()
* PieceType::N_PIECE_TYPES
+ board
.piece_type_at(m.from_sq())
.expect("No attacker in MVVLVA.")
.index()]
Self::MVV_LVA_SCORES[board.piece_type_at(m.to_sq()).index() * PieceType::N_PIECE_TYPES
+ board.piece_type_at(m.from_sq()).index()]
}

pub fn add_killer(&mut self, board: &Board, m: Move, ply: Ply) {
let color = board.ctm().index();
let killer_moves = &mut self.killer_moves[color][ply];

killer_moves.rotate_right(1);
killer_moves[0] = Some(m);
killer_moves[0] = m;
}

pub fn add_history(&mut self, m: Move, depth: Depth) {
Expand All @@ -110,36 +97,27 @@ impl MoveSorter {
}

fn is_killer(&self, board: &Board, m: Move, ply: usize) -> bool {
self.killer_moves[board.ctm().index()][ply].contains(&Some(m))
self.killer_moves[board.ctm().index()][ply].contains(&m)
}

fn history_score(&self, m: Move) -> Value {
self.history_scores[m.from_sq().index()][m.to_sq().index()]
}

pub fn see(board: &Board, m: Move) -> bool {
if m.promotion().is_some() {
if m.promotion() != PieceType::None {
return true;
}

let from_sq = m.from_sq();
let to_sq = m.to_sq();

let Some(captured_pt) = board.piece_type_at(to_sq) else {
return false;
};

let mut value = Self::SEE_PIECE_TYPE[captured_pt.index()];
let mut value = Self::SEE_PIECE_TYPE[board.piece_type_at(to_sq).index()];

if value < 0 {
return false;
}

let Some(mut attacking_pt) = board.piece_type_at(from_sq) else {
return false;
};

value -= Self::SEE_PIECE_TYPE[attacking_pt.index()];
value -= Self::SEE_PIECE_TYPE[board.piece_type_at(from_sq).index()];

if value >= 0 {
return true;
Expand All @@ -161,9 +139,9 @@ impl MoveSorter {
}

// We know at this point that there must be a piece, so find the least valuable attacker.
attacking_pt = PieceType::iter(PieceType::Pawn, PieceType::King)
let attacking_pt = PieceType::iter(PieceType::Pawn, PieceType::King)
.find(|&pt| stm_attackers & board.bitboard_of_pt(pt) != Bitboard::ZERO)
.expect("No attacking pt found.");
.unwrap();

ctm = !ctm;

Expand Down Expand Up @@ -194,10 +172,7 @@ impl MoveSorter {
}
}

ctm != board
.piece_at(from_sq)
.expect("No piece at original attacking square.")
.color_of()
ctm != board.piece_at(from_sq).color_of()
}
}

Expand Down
6 changes: 0 additions & 6 deletions src/perft.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,6 @@ pub fn print_perft(board: &mut Board, depth: Depth) -> u128 {

let moves: MoveList = MoveList::from(board);
let mut nodes = 0;
let hash = board.hash();
let material_hash = board.material_hash();
for m in moves {
print!("{}: ", m);
let move_nodes;
Expand All @@ -41,10 +39,6 @@ pub fn print_perft(board: &mut Board, depth: Depth) -> u128 {
nodes += move_nodes;
println!("{}", move_nodes);
}

assert_eq!(board.hash(), hash);
assert_eq!(board.material_hash(), material_hash);

let elapsed = now.elapsed().as_secs_f32();
println!();
println!("{:?}", board);
Expand Down
Loading

0 comments on commit 6264882

Please sign in to comment.