Compare commits

..

13 Commits

Author SHA1 Message Date
Varga Dávid Lajos
ebcc447092 removed unused imports from chessmove.rs 2025-11-21 14:19:33 +01:00
Varga Dávid Lajos
a4982c0fd3 implemented conversion method to_bitmove for enum ChessMove 2025-11-21 14:18:23 +01:00
Varga Dávid Lajos
c414485891 implemented conversion method to_index for struct BoardSquare 2025-11-21 14:09:02 +01:00
Varga Dávid Lajos
f29298731b implemented conversion method to_index for enum PieceType 2025-11-21 14:07:27 +01:00
Varga Dávid Lajos
e1d51fe291 implemented conversion method to_bitmovetype for enum MoveType 2025-11-21 14:04:37 +01:00
Varga Dávid Lajos
f841fba080 implemented constructor from_bitmove for enum ChessMove 2025-11-21 13:55:07 +01:00
Varga Dávid Lajos
821746d7cf changed implementation of ChessMove from struct to enum 2025-11-21 13:29:01 +01:00
Varga Dávid Lajos
5d108602ea implemented constructor by square_index for struct BoardSquare 2025-11-21 13:08:53 +01:00
Varga Dávid Lajos
b137e6ceab implemented constructor by piece_index for enum PieceType 2025-11-21 13:01:38 +01:00
Varga Dávid Lajos
d649afdcfd limited visibility of method from_bitmovetype from enum MoveType to pub(in super) 2025-11-21 12:57:38 +01:00
Varga Dávid Lajos
5e65bcf5ef changed method from_index for enum MoveType to from_bitmovetype 2025-11-21 12:55:18 +01:00
Varga Dávid Lajos
82a6bd8449 implemented constructor for enum MoveType from in-engine move_type index 2025-11-21 12:40:24 +01:00
Hatvani Tamás
23c890f329 Merge pull request #20 from htamas1210/Engine/move-gen 2025-11-20 21:26:58 +01:00
5 changed files with 226 additions and 32 deletions

View File

@@ -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;

View File

@@ -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;
}
}

View File

@@ -1,12 +1,11 @@
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,
@@ -14,6 +13,36 @@ pub struct ChessMove {
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 {
@@ -23,14 +52,11 @@ 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
};
}
@@ -38,34 +64,129 @@ impl ChessMove {
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;
}
}

View File

@@ -1,5 +1,6 @@
use serde::Deserialize;
use serde::Serialize;
use super::bitboard::bitmove::BitMoveType;
#[derive(Serialize, Deserialize)]
pub enum MoveType {
@@ -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
};
}
}

View File

@@ -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
}
}
}