[board] Remove the position reference from move generator structs
Simplifies the lifetime calculations, and makes it possible to cache a Moves object on Position.
This commit is contained in:
parent
d3ab477795
commit
bea6dd67c8
9 changed files with 25 additions and 28 deletions
|
@ -7,7 +7,7 @@ use chessfriend_core::{Color, Direction, Piece, PlacedPiece};
|
|||
|
||||
move_generator_declaration!(ClassicalMoveGenerator);
|
||||
|
||||
impl<'pos> MoveGeneratorInternal for ClassicalMoveGenerator<'pos> {
|
||||
impl MoveGeneratorInternal for ClassicalMoveGenerator {
|
||||
fn piece(color: Color) -> Piece {
|
||||
Piece::bishop(color)
|
||||
}
|
||||
|
@ -60,7 +60,7 @@ mod tests {
|
|||
use super::*;
|
||||
use crate::{position, position::DiagramFormatter};
|
||||
use chessfriend_bitboard::BitBoard;
|
||||
use chessfriend_core::{piece, Color};
|
||||
use chessfriend_core::Color;
|
||||
|
||||
#[test]
|
||||
fn classical_single_bishop_bitboard() {
|
||||
|
|
|
@ -4,7 +4,7 @@
|
|||
//! generating the possible moves for the king in the given position.
|
||||
|
||||
use super::{move_generator_declaration, MoveGeneratorInternal, MoveSet};
|
||||
use crate::{ r#move::Castle, Move, MoveBuilder, Position};
|
||||
use crate::{r#move::Castle, Move, MoveBuilder, Position};
|
||||
use chessfriend_bitboard::BitBoard;
|
||||
use chessfriend_core::{Color, Piece, PlacedPiece};
|
||||
|
||||
|
@ -12,7 +12,7 @@ move_generator_declaration!(KingMoveGenerator, struct);
|
|||
move_generator_declaration!(KingMoveGenerator, new);
|
||||
move_generator_declaration!(KingMoveGenerator, getters);
|
||||
|
||||
impl<'a> KingMoveGenerator<'a> {
|
||||
impl KingMoveGenerator {
|
||||
#[allow(unused_variables)]
|
||||
fn king_side_castle(position: &Position, color: Color) -> Option<Move> {
|
||||
if !position.player_has_right_to_castle(color, Castle::KingSide) {
|
||||
|
@ -34,7 +34,7 @@ impl<'a> KingMoveGenerator<'a> {
|
|||
}
|
||||
}
|
||||
|
||||
impl<'pos> MoveGeneratorInternal for KingMoveGenerator<'pos> {
|
||||
impl MoveGeneratorInternal for KingMoveGenerator {
|
||||
fn piece(color: Color) -> Piece {
|
||||
Piece::king(color)
|
||||
}
|
||||
|
|
|
@ -7,7 +7,7 @@ use chessfriend_core::{Color, Piece, PlacedPiece};
|
|||
|
||||
move_generator_declaration!(KnightMoveGenerator);
|
||||
|
||||
impl<'pos> MoveGeneratorInternal for KnightMoveGenerator<'pos> {
|
||||
impl MoveGeneratorInternal for KnightMoveGenerator {
|
||||
fn piece(color: Color) -> Piece {
|
||||
Piece::knight(color)
|
||||
}
|
||||
|
|
|
@ -29,9 +29,8 @@ macro_rules! move_generator_declaration {
|
|||
move_generator_declaration!($name, getters);
|
||||
};
|
||||
($name:ident, struct) => {
|
||||
#[derive(Clone, Debug)]
|
||||
pub(super) struct $name<'pos> {
|
||||
position: &'pos $crate::Position,
|
||||
#[derive(Clone, Debug, Eq, PartialEq)]
|
||||
pub(super) struct $name {
|
||||
color: chessfriend_core::Color,
|
||||
move_sets: std::collections::BTreeMap<
|
||||
chessfriend_core::Square,
|
||||
|
@ -40,13 +39,12 @@ macro_rules! move_generator_declaration {
|
|||
}
|
||||
};
|
||||
($name:ident, new) => {
|
||||
impl<'pos> $name<'pos> {
|
||||
impl $name {
|
||||
pub(super) fn new(
|
||||
position: &$crate::Position,
|
||||
color: chessfriend_core::Color,
|
||||
) -> $name {
|
||||
$name {
|
||||
position,
|
||||
color,
|
||||
move_sets: Self::move_sets(position, color),
|
||||
}
|
||||
|
@ -54,7 +52,7 @@ macro_rules! move_generator_declaration {
|
|||
}
|
||||
};
|
||||
($name:ident, getters) => {
|
||||
impl<'pos> $name<'pos> {
|
||||
impl $name {
|
||||
pub(super) fn iter(&self) -> impl Iterator<Item = &$crate::Move> + '_ {
|
||||
self.move_sets.values().map(|set| set.moves()).flatten()
|
||||
}
|
||||
|
|
|
@ -9,17 +9,17 @@ use super::{
|
|||
use crate::{Move, Position};
|
||||
use chessfriend_core::{Color, PlacedPiece, Shape};
|
||||
|
||||
#[derive(Clone, Debug)]
|
||||
pub struct Moves<'pos> {
|
||||
pawn_moves: PawnMoveGenerator<'pos>,
|
||||
knight_moves: KnightMoveGenerator<'pos>,
|
||||
bishop_moves: BishopMoveGenerator<'pos>,
|
||||
rook_moves: RookMoveGenerator<'pos>,
|
||||
queen_moves: QueenMoveGenerator<'pos>,
|
||||
king_moves: KingMoveGenerator<'pos>,
|
||||
#[derive(Clone, Debug, Eq, PartialEq)]
|
||||
pub struct Moves {
|
||||
pawn_moves: PawnMoveGenerator,
|
||||
knight_moves: KnightMoveGenerator,
|
||||
bishop_moves: BishopMoveGenerator,
|
||||
rook_moves: RookMoveGenerator,
|
||||
queen_moves: QueenMoveGenerator,
|
||||
king_moves: KingMoveGenerator,
|
||||
}
|
||||
|
||||
impl<'a> Moves<'a> {
|
||||
impl Moves {
|
||||
pub fn new(position: &Position, color: Color) -> Moves {
|
||||
Moves {
|
||||
pawn_moves: PawnMoveGenerator::new(position, color),
|
||||
|
|
|
@ -24,7 +24,7 @@ struct MoveGenerationParameters {
|
|||
|
||||
move_generator_declaration!(PawnMoveGenerator);
|
||||
|
||||
impl<'pos> MoveGeneratorInternal for PawnMoveGenerator<'pos> {
|
||||
impl MoveGeneratorInternal for PawnMoveGenerator {
|
||||
fn piece(color: Color) -> Piece {
|
||||
Piece::pawn(color)
|
||||
}
|
||||
|
@ -54,7 +54,7 @@ impl<'pos> MoveGeneratorInternal for PawnMoveGenerator<'pos> {
|
|||
}
|
||||
}
|
||||
|
||||
impl<'pos> PawnMoveGenerator<'pos> {
|
||||
impl PawnMoveGenerator {
|
||||
fn move_generation_parameters(color: Color) -> MoveGenerationParameters {
|
||||
match color {
|
||||
Color::White => MoveGenerationParameters {
|
||||
|
|
|
@ -7,7 +7,7 @@ use chessfriend_core::{Color, Direction, Piece, PlacedPiece};
|
|||
|
||||
move_generator_declaration!(ClassicalMoveGenerator);
|
||||
|
||||
impl<'pos> MoveGeneratorInternal for ClassicalMoveGenerator<'pos> {
|
||||
impl MoveGeneratorInternal for ClassicalMoveGenerator {
|
||||
fn piece(color: Color) -> Piece {
|
||||
Piece::queen(color)
|
||||
}
|
||||
|
@ -66,7 +66,7 @@ mod tests {
|
|||
use super::*;
|
||||
use crate::{position, position::DiagramFormatter};
|
||||
use chessfriend_bitboard::{bitboard, BitBoard};
|
||||
use chessfriend_core::{piece, Color};
|
||||
use chessfriend_core::Color;
|
||||
|
||||
#[test]
|
||||
fn classical_single_queen_bitboard() {
|
||||
|
|
|
@ -1,13 +1,13 @@
|
|||
// Eryn Wells <eryn@erynwells.me>
|
||||
|
||||
use super::{move_generator_declaration, MoveGeneratorInternal, MoveSet};
|
||||
use crate::{ MoveBuilder, Position};
|
||||
use crate::{MoveBuilder, Position};
|
||||
use chessfriend_bitboard::BitBoard;
|
||||
use chessfriend_core::{Color, Direction, Piece, PlacedPiece};
|
||||
|
||||
move_generator_declaration!(ClassicalMoveGenerator);
|
||||
|
||||
impl<'pos> MoveGeneratorInternal for ClassicalMoveGenerator<'pos> {
|
||||
impl MoveGeneratorInternal for ClassicalMoveGenerator {
|
||||
fn piece(color: Color) -> Piece {
|
||||
Piece::rook(color)
|
||||
}
|
||||
|
|
|
@ -20,7 +20,6 @@ pub struct Position {
|
|||
en_passant_square: Option<Square>,
|
||||
sight: [OnceCell<BitBoard>; 2],
|
||||
moves: OnceCell<Moves>,
|
||||
|
||||
half_move_counter: u16,
|
||||
full_move_number: u16,
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue