Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions examples/binpack_writer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use sfbinpack::{
chess::{
coords::Square,
piece::Piece,
piecetype::PieceType,
position::Position,
r#move::{Move, MoveType},
},
Expand Down Expand Up @@ -32,6 +33,7 @@ fn main() {
Square::new(26),
MoveType::Normal,
Piece::none(),
PieceType::Pawn,
),
score: -201,
ply: 68,
Expand All @@ -45,6 +47,7 @@ fn main() {
Square::new(19),
MoveType::Normal,
Piece::none(),
PieceType::Pawn,
),
score: 254,
ply: 69,
Expand All @@ -58,6 +61,7 @@ fn main() {
Square::new(49),
MoveType::Normal,
Piece::none(),
PieceType::Bishop,
),
score: -220,
ply: 70,
Expand Down
12 changes: 8 additions & 4 deletions src/chess/attacks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -88,15 +88,19 @@ fn generate_pawn_pushes(
if (promotion_start..promotion_end).contains(&one_step) {
add_promotions(from_sq, to_sq, side, moves);
} else {
moves.push(Move::normal(from_sq, to_sq));
moves.push(Move::normal(from_sq, to_sq, PieceType::Pawn));

// Double push
if from_sq.index() / 8 == start_rank {
let two_step = one_step + direction;
if (0..64).contains(&two_step)
&& pos.piece_at(Square::new(two_step as u32)) == Piece::none()
{
moves.push(Move::normal(from_sq, Square::new(two_step as u32)));
moves.push(Move::normal(
from_sq,
Square::new(two_step as u32),
PieceType::Pawn,
));
}
}
}
Expand Down Expand Up @@ -126,7 +130,7 @@ fn generate_pawn_captures(
if (promotion_start..promotion_end).contains(&(to_sq.index() as i32)) {
add_promotions(from_sq, to_sq, side, moves);
} else {
moves.push(Move::normal(from_sq, to_sq));
moves.push(Move::normal(from_sq, to_sq, PieceType::Pawn));
}
}
}
Expand Down Expand Up @@ -204,7 +208,7 @@ fn generate_piece_moves<P: PieceMovement>(
let target = pos.piece_at(to_sq);

if target == Piece::none() || target.color() != side {
moves.push(Move::normal(from_sq, to_sq));
moves.push(Move::normal(from_sq, to_sq, P::piece_type()));
}
}
}
Expand Down
17 changes: 15 additions & 2 deletions src/chess/move.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,17 @@ pub struct Move {
to: Square,
move_type: MoveType,
promoted_piece: Piece,
pub piece_type: PieceType,
}

impl Move {
pub fn new(from: Square, to: Square, move_type: MoveType, promoted_piece: Piece) -> Self {
pub fn new(
from: Square,
to: Square,
move_type: MoveType,
promoted_piece: Piece,
piece_type: PieceType,
) -> Self {
debug_assert!(from.index() < 64);
debug_assert!(to.index() < 64);

Expand All @@ -57,6 +64,7 @@ impl Move {
to,
move_type,
promoted_piece,
piece_type,
}
}

Expand All @@ -66,6 +74,7 @@ impl Move {
to: Square::NONE,
move_type: MoveType::Normal,
promoted_piece: Piece::none(),
piece_type: PieceType::None,
}
}

Expand All @@ -87,12 +96,13 @@ impl Move {
self.to
}

pub const fn normal(from: Square, to: Square) -> Self {
pub const fn normal(from: Square, to: Square, piece_type: PieceType) -> Self {
Self {
from,
to,
move_type: MoveType::Normal,
promoted_piece: Piece::none(),
piece_type,
}
}

Expand All @@ -102,6 +112,7 @@ impl Move {
to,
move_type: MoveType::EnPassant,
promoted_piece: Piece::none(),
piece_type: PieceType::Pawn,
}
}

Expand All @@ -111,6 +122,7 @@ impl Move {
to,
move_type: MoveType::Promotion,
promoted_piece: piece,
piece_type: PieceType::Pawn,
}
}

Expand All @@ -120,6 +132,7 @@ impl Move {
to,
move_type: MoveType::Castle,
promoted_piece: Piece::none(),
piece_type: PieceType::King,
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/chess/piecetype.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#[derive(Clone, Copy, PartialEq, Eq)]
#[derive(Clone, Copy, PartialEq, Eq, Debug)]
pub enum PieceType {
Pawn,
Knight,
Expand Down
26 changes: 13 additions & 13 deletions src/chess/position.rs
Original file line number Diff line number Diff line change
Expand Up @@ -181,15 +181,15 @@ impl Position {

if mv.mtype() == MoveType::Promotion {
let promotion = mv.promoted_piece();
self.place_piece(self.stm, promotion, to);
self.place_piece(self.stm, promotion, promotion.piece_type(), to);
} else if mv.mtype() == MoveType::EnPassant {
debug_assert!(piece.piece_type() == PieceType::Pawn);

let captured_sq = Square::new(to.index() ^ 8);
self.remove_piecetype(!self.stm, PieceType::Pawn, captured_sq);
self.place_piece(self.stm, piece, to);
self.place_piece(self.stm, piece, PieceType::Pawn, to);
} else if mv.mtype() == MoveType::Normal {
self.place_piece(self.stm, piece, to);
self.place_piece(self.stm, piece, pt, to);
} else if mv.mtype() == MoveType::Castle {
if mv.castle_type() == CastleType::Short {
let rook_to = if self.stm == Color::White {
Expand All @@ -207,8 +207,8 @@ impl Position {
let rook = self.piece_at(to);

self.remove_piecetype(self.stm, PieceType::Rook, to);
self.place_piece(self.stm, rook, rook_to);
self.place_piece(self.stm, piece, king_to);
self.place_piece(self.stm, rook, PieceType::Rook, rook_to);
self.place_piece(self.stm, piece, PieceType::King, king_to);
} else {
let rook_to = if self.stm == Color::White {
Square::D1
Expand All @@ -225,8 +225,8 @@ impl Position {
let rook = self.piece_at(to);

self.remove_piecetype(self.stm, PieceType::Rook, to);
self.place_piece(self.stm, rook, rook_to);
self.place_piece(self.stm, piece, king_to);
self.place_piece(self.stm, rook, PieceType::Rook, rook_to);
self.place_piece(self.stm, piece, PieceType::King, king_to);
}
}

Expand Down Expand Up @@ -271,7 +271,7 @@ impl Position {
// move the enemy pawn
let enemy_pawn = self.piece_at(enemy_sq);
self.remove_piecetype(!self.stm, PieceType::Pawn, enemy_sq);
self.place_piece(!self.stm, enemy_pawn, ep);
self.place_piece(!self.stm, enemy_pawn, PieceType::Pawn, ep);

// remove our pawn
self.remove_piecetype(self.stm, PieceType::Pawn, to);
Expand All @@ -282,11 +282,11 @@ impl Position {
// undo the move

// move the enemy pawn
self.place_piece(!self.stm, enemy_pawn, enemy_sq);
self.place_piece(!self.stm, enemy_pawn, PieceType::Pawn, enemy_sq);
self.remove_piecetype(!self.stm, PieceType::Pawn, ep);

// place our pawn
self.place_piece(self.stm, piece, to);
self.place_piece(self.stm, piece, PieceType::Pawn, to);

if !is_checked {
self.enpassant = ep;
Expand Down Expand Up @@ -341,19 +341,19 @@ impl Position {
debug_assert!(pc != Piece::none());
debug_assert!(sq != Square::NONE);

self.place_piece(pc.color(), pc, sq);
self.place_piece(pc.color(), pc, pc.piece_type(), sq);
}

/// Places a piece on the board
#[inline(always)]
fn place_piece(&mut self, side: Color, pc: Piece, sq: Square) {
fn place_piece(&mut self, side: Color, pc: Piece, pt: PieceType, sq: Square) {
debug_assert!(pc != Piece::none());
debug_assert!(sq != Square::NONE);
debug_assert!(side == pc.color());

let mask = 1u64 << (sq.index());
self.bb_color[side as usize] |= mask;
self.bb[pc.piece_type().ordinal() as usize] |= mask;
self.bb[pt.ordinal() as usize] |= mask;
self.pieces[sq.index() as usize] = pc;
}

Expand Down
11 changes: 10 additions & 1 deletion src/common/compressed_move.rs
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,13 @@ impl CompressedMove {
debug_assert!(from != Square::NONE);
debug_assert!(to != Square::NONE);

Move::new(from, to, move_type, promoted_piece)
Move::new(
from,
to,
move_type,
promoted_piece,
/* will be adjusted later */ PieceType::None,
)
}
}

Expand Down Expand Up @@ -146,6 +152,7 @@ mod tests {
Square::new(58),
MoveType::Normal,
Piece::none(),
PieceType::None,
);

assert_eq!(expected, compressed.decompress());
Expand All @@ -158,6 +165,7 @@ mod tests {
Square::new(56),
MoveType::Promotion,
Piece::new(PieceType::Queen, Color::White),
PieceType::None,
);

let compressed = CompressedMove::from_move(expected);
Expand All @@ -172,6 +180,7 @@ mod tests {
Square::new(56),
MoveType::Promotion,
Piece::new(PieceType::Queen, Color::White),
PieceType::None,
);

let compressed = CompressedMove::from_move(expected);
Expand Down
10 changes: 7 additions & 3 deletions src/common/entry.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use std::fmt;

use crate::chess::{position::Position, r#move::Move};
use crate::chess::{piecetype::PieceType, position::Position, r#move::Move};

use super::{
arithmetic::{signed_to_unsigned, unsigned_to_signed},
Expand Down Expand Up @@ -75,9 +75,12 @@ impl PackedTrainingDataEntry {
// Read and decompress move
// EBNF: Move
let compressed_move = CompressedMove::read_from_big_endian(&self.data[offset..]);
let mv = compressed_move.decompress();
let mut mv = compressed_move.decompress();
offset += CompressedMove::byte_size();

mv.piece_type = pos.piece_at(mv.from()).piece_type();
debug_assert!(mv.piece_type != PieceType::None);

// Read score
// EBNF: Score
let score = unsigned_to_signed(self.read_u16_be(offset));
Expand Down Expand Up @@ -148,7 +151,7 @@ impl PackedTrainingDataEntry {

#[cfg(test)]
mod test {
use crate::chess::{coords::Square, piece::Piece, r#move::MoveType};
use crate::chess::{coords::Square, piece::Piece, piecetype::PieceType, r#move::MoveType};

use super::*;

Expand All @@ -173,6 +176,7 @@ mod test {
Square::new(58),
MoveType::Normal,
Piece::none(),
PieceType::Rook,
),
score: -127,
ply: 39,
Expand Down
2 changes: 1 addition & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use sfbinpack::CompressedTrainingDataEntryReader;

fn main() {
let file =
File::open("..\\..\\stockfish-data\\test80-2024-06-jun-2tb7p.min-v2.v6.binpack").unwrap();
File::open("/mnt/g/stockfish-data/test80-2024-06-jun-2tb7p.min-v2.v6.binpack").unwrap();

let filesize = file.metadata().unwrap().len();

Expand Down
4 changes: 4 additions & 0 deletions src/reader/compressed_reader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -321,6 +321,7 @@ mod tests {
use crate::chess::{
coords::Square,
piece::Piece,
piecetype::PieceType,
position::Position,
r#move::{Move, MoveType},
};
Expand Down Expand Up @@ -355,6 +356,7 @@ mod tests {
Square::new(26),
MoveType::Normal,
Piece::none(),
PieceType::Pawn,
),
score: -201,
ply: 68,
Expand All @@ -368,6 +370,7 @@ mod tests {
Square::new(19),
MoveType::Normal,
Piece::none(),
PieceType::Pawn,
),
score: 254,
ply: 69,
Expand All @@ -383,6 +386,7 @@ mod tests {
Square::new(49),
MoveType::Normal,
Piece::none(),
PieceType::Bishop,
),
score: -220,
ply: 70,
Expand Down
8 changes: 5 additions & 3 deletions src/reader/move_score_list_reader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,8 @@ impl PackedMoveScoreListReader {
// Extract the move
let move_ = self.decode_move(movetext, piece_id, occupied);

debug_assert!(move_.piece_type != PieceType::None);

// Extract the score
let score = self.decode_score(movetext);

Expand Down Expand Up @@ -157,7 +159,7 @@ impl PackedMoveScoreListReader {
if to == ep_square {
Move::en_passant(from, to)
} else {
Move::normal(from, to)
Move::normal(from, to, PieceType::Pawn)
}
}
}
Expand Down Expand Up @@ -195,7 +197,7 @@ impl PackedMoveScoreListReader {
Move::from_castle(castle_type, side_to_move)
} else {
let to = Square::new(nth_set_bit_index(attacks.bits(), move_id as u64));
Move::normal(from, to)
Move::normal(from, to, PieceType::King)
}
}

Expand All @@ -207,7 +209,7 @@ impl PackedMoveScoreListReader {
.extract_bits_le8(movetext, used_bits_safe(attacks.count() as u64));
let idx = nth_set_bit_index(attacks.bits(), move_id as u64);
let to = Square::new(idx);
Move::normal(from, to)
Move::normal(from, to, piece_type)
}
}
}
Expand Down
Loading
Loading