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