Skip to content
Merged
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: 2 additions & 2 deletions engine/src/bitboard.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ mod utils;
mod legality;
mod checkinfo;
mod attacks;
mod bitmove;
mod movebuffer;
mod movegen;

pub mod board;
pub mod board;
pub(in super) mod bitmove;
17 changes: 17 additions & 0 deletions engine/src/boardsquare.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,5 +27,22 @@ impl BoardSquare {
}
return Self { x: x, y: y };
}

pub(in super) fn from_index(idx: u8) -> Self {
let file = idx % 8;
let rank = idx / 8;

#[cfg(debug_assertions)]
{
if !(0..8).contains(&rank) {
println!("Warning: internal engine issue, given index is not on the board!");
}
}

return Self {x: file as usize, y: rank as usize};
}
pub(in super) fn to_index(&self) -> u8 {
return (8 * self.y + self.x) as u8;
}
}

181 changes: 151 additions & 30 deletions engine/src/chessmove.rs
Original file line number Diff line number Diff line change
@@ -1,19 +1,48 @@
use crate::piecetype;
use crate::{bitboard::{bitmove::{BitMove, BitMoveType}, board::Board}};

use super::boardsquare::BoardSquare;
use super::movetype::MoveType;
use super::piecetype::PieceType;
use serde::{Deserialize, Serialize};

#[derive(Serialize, Deserialize)]
pub struct ChessMove {
/*pub struct ChessMove {
pub move_type: MoveType,
pub piece_type: PieceType,
pub from_square: BoardSquare,
pub to_square: BoardSquare,
pub rook_from: BoardSquare,
pub rook_to: BoardSquare,
pub promotion_piece: Option<PieceType>,
}*/
pub enum ChessMove {
Quiet {
piece_type: PieceType,
from_square: BoardSquare,
to_square: BoardSquare,
promotion_piece: Option<PieceType>
},
Capture {
piece_type: PieceType,
from_square: BoardSquare,
to_square: BoardSquare,
captured_piece: PieceType,
promotion_piece: Option<PieceType>
},
Castle {
king_type: PieceType,
king_from: BoardSquare,
king_to: BoardSquare,
rook_type: PieceType,
rook_from: BoardSquare,
rook_to: BoardSquare
},
EnPassant {
pawn_type: PieceType,
from_square: BoardSquare,
to_square: BoardSquare,
captured_piece: PieceType,
captured_from: BoardSquare
}
}

impl ChessMove {
Expand All @@ -23,49 +52,141 @@ impl ChessMove {
to_square: BoardSquare,
promotion_piece: Option<PieceType>,
) -> Self {
return Self {
move_type: MoveType::Quiet,
piece_type: piece_type,
from_square: from_square,
to_square: to_square,
rook_from: BoardSquare::new(),
rook_to: BoardSquare::new(),
promotion_piece: promotion_piece,
return Self::Quiet {
piece_type,
from_square,
to_square,
promotion_piece
};
}

pub fn capture(
piece_type: PieceType,
from_square: BoardSquare,
to_square: BoardSquare,
captured_piece: PieceType,
promotion_piece: Option<PieceType>,
) -> Self {
return Self {
move_type: MoveType::Capture,
piece_type: piece_type,
from_square: from_square,
to_square: to_square,
rook_from: BoardSquare::new(),
rook_to: BoardSquare::new(),
promotion_piece: promotion_piece,
return Self::Capture {
piece_type,
from_square,
to_square,
captured_piece,
promotion_piece
};
}

pub fn castle(
piece_type: PieceType,
from_square: BoardSquare,
to_square: BoardSquare,
king_type: PieceType,
king_from: BoardSquare,
king_to: BoardSquare,
rook_type: PieceType,
rook_from: BoardSquare,
rook_to: BoardSquare,
) -> Self {
return Self {
move_type: MoveType::Quiet,
piece_type: piece_type,
from_square: from_square,
to_square: to_square,
rook_from: rook_from,
rook_to: rook_to,
promotion_piece: None,
return Self::Castle {
king_type,
king_from,
king_to,
rook_type,
rook_from,
rook_to
};
}

pub(in super) fn from_bitmove(bitmove: &BitMove, board: Board) -> Self {
match bitmove.move_type() {
BitMoveType::Quiet => {
let from_square_index = bitmove.from_square();
let piece_type = PieceType::from_index(board.piece_board(from_square_index));
let from_square = BoardSquare::from_index(from_square_index);
let to_square = BoardSquare::from_index(bitmove.to_square());
let promotion_piece = match bitmove.promotion_piece() {
Some(piece) => Some(PieceType::from_index(piece)),
None => None
};

return ChessMove::Quiet { piece_type, from_square, to_square, promotion_piece }
},
BitMoveType::Capture => {
let from_square_index = bitmove.from_square();
let to_square_index = bitmove.to_square();
let piece_type = PieceType::from_index(board.piece_board(from_square_index));
let from_square = BoardSquare::from_index(from_square_index);
let to_square = BoardSquare::from_index(to_square_index);
let captured_piece = PieceType::from_index(board.piece_board(to_square_index));
let promotion_piece = match bitmove.promotion_piece() {
Some(piece) => Some(PieceType::from_index(piece)),
None => None
};

return ChessMove::Capture { piece_type, from_square, to_square, captured_piece, promotion_piece }
},
BitMoveType::Castle => {
let from_square_index = bitmove.from_square();
let to_square_index = bitmove.to_square();
let king_type = PieceType::from_index(board.piece_board(from_square_index));
let king_from = BoardSquare::from_index(from_square_index);
let king_to = BoardSquare::from_index(to_square_index);
let promotion_piece = match bitmove.promotion_piece() {
Some(piece) => Some(PieceType::from_index(piece)),
None => None
};
let rook_type = if bitmove.from_square() < 32 { PieceType::WhiteRook } else { PieceType::BlackRook };
let rook_from_index = if bitmove.to_square() > bitmove.from_square() {
bitmove.from_square() + 3
} else {
bitmove.from_square() - 4
};
let rook_from = BoardSquare::from_index(rook_from_index);
let rook_to_index = if bitmove.to_square() > bitmove.from_square() {
bitmove.from_square() + 1
} else {
bitmove.from_square() - 1
};
let rook_to = BoardSquare::from_index(rook_to_index);

return ChessMove::Castle { king_type, king_from, king_to, rook_type, rook_from, rook_to }
},
BitMoveType::EnPassant => {
panic!("ChessMove::from_bitmove was left unimplemented");
}
}
}
pub(in super) fn to_bitmove(&self) -> BitMove {
let bitmove = match self {
ChessMove::Quiet { piece_type, from_square, to_square, promotion_piece } => {
let promotion_piece = match promotion_piece {
Some(piece) => Some(piece.to_index()),
None => None
};
return BitMove::quiet(
from_square.to_index(),
to_square.to_index(),
promotion_piece
);
},
ChessMove::Capture { piece_type, from_square, to_square, captured_piece, promotion_piece } => {
let promotion_piece = match promotion_piece {
Some(piece) => Some(piece.to_index()),
None => None
};
return BitMove::capture(
from_square.to_index(),
to_square.to_index(),
promotion_piece
);
},
ChessMove::Castle { king_type, king_from, king_to, rook_type, rook_from, rook_to } => {
return BitMove::castle(
king_from.to_index(),
king_to.to_index()
);
},
ChessMove::EnPassant { pawn_type, from_square, to_square, captured_piece, captured_from } => {
panic!("ChessMove::to_bitmove was left unimplemented");
}
};
return bitmove;
}
}
20 changes: 20 additions & 0 deletions engine/src/movetype.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use serde::Deserialize;
use serde::Serialize;
use super::bitboard::bitmove::BitMoveType;

#[derive(Serialize, Deserialize)]
pub enum MoveType {
Expand All @@ -9,3 +10,22 @@ pub enum MoveType {
EnPassant,
}

impl MoveType {
pub(in super) fn from_bitmovetype(move_type: BitMoveType) -> Self {
return match move_type {
BitMoveType::Quiet => Self::Quiet,
BitMoveType::Capture => Self::Capture,
BitMoveType::Castle => Self::Castle,
BitMoveType::EnPassant => Self::EnPassant,
_ => panic!("invalid move_type index! should NEVER appear")
}
}
pub(in super) fn to_bitmoveType(&self) -> BitMoveType {
return match self {
&MoveType::Quiet => BitMoveType::Quiet,
&MoveType::Capture => BitMoveType::Capture,
&MoveType::Castle => BitMoveType::Castle,
&MoveType::EnPassant => BitMoveType::EnPassant
};
}
}
36 changes: 36 additions & 0 deletions engine/src/piecetype.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,39 @@ pub enum PieceType {
BlackKing,
}

impl PieceType {

pub(in super) fn from_index(idx: u8) -> Self {
return match idx {
0 => PieceType::WhitePawn,
1 => PieceType::WhiteKnight,
2 => PieceType::WhiteBishop,
3 => PieceType::WhiteRook,
4 => PieceType::WhiteQueen,
5 => PieceType::WhiteKing,
6 => PieceType::BlackPawn,
7 => PieceType::BlackKnight,
8 => PieceType::BlackBishop,
9 => PieceType::BlackRook,
10 => PieceType::BlackQueen,
11 => PieceType::BlackKing,
_ => panic!("invalid piece index! should NEVER appear")
}
}
pub(in super) fn to_index(&self) -> u8 {
return match self {
&PieceType::WhitePawn => 0,
&PieceType::WhiteKnight => 1,
&PieceType::WhiteBishop => 2,
&PieceType::WhiteRook => 3,
&PieceType::WhiteQueen => 4,
&PieceType::WhiteKing => 5,
&PieceType::BlackPawn => 6,
&PieceType::BlackKnight => 7,
&PieceType::BlackBishop => 8,
&PieceType::BlackRook => 9,
&PieceType::BlackQueen => 10,
&PieceType::BlackKing => 11
}
}
}