[board] Create const arrays for Shape that return &'static to its enum cases
This commit is contained in:
parent
878d771d86
commit
750b16970f
3 changed files with 17 additions and 8 deletions
|
@ -2,6 +2,7 @@
|
||||||
|
|
||||||
use crate::Square;
|
use crate::Square;
|
||||||
use std::fmt;
|
use std::fmt;
|
||||||
|
use std::slice::Iter;
|
||||||
|
|
||||||
#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)]
|
#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)]
|
||||||
pub enum Color {
|
pub enum Color {
|
||||||
|
@ -33,16 +34,24 @@ pub enum Shape {
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Shape {
|
impl Shape {
|
||||||
pub fn iter() -> impl Iterator<Item = Shape> {
|
pub fn iter() -> Iter<'static, Shape> {
|
||||||
[
|
const ALL_SHAPES: [Shape; 6] = [
|
||||||
Shape::Pawn,
|
Shape::Pawn,
|
||||||
Shape::Knight,
|
Shape::Knight,
|
||||||
Shape::Bishop,
|
Shape::Bishop,
|
||||||
Shape::Rook,
|
Shape::Rook,
|
||||||
Shape::Queen,
|
Shape::Queen,
|
||||||
Shape::King,
|
Shape::King,
|
||||||
]
|
];
|
||||||
.into_iter()
|
|
||||||
|
ALL_SHAPES.iter()
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn promotable() -> Iter<'static, Shape> {
|
||||||
|
const PROMOTABLE_SHAPES: [Shape; 4] =
|
||||||
|
[Shape::Queen, Shape::Rook, Shape::Bishop, Shape::Knight];
|
||||||
|
|
||||||
|
PROMOTABLE_SHAPES.iter()
|
||||||
}
|
}
|
||||||
|
|
||||||
fn _ascii_representation(&self) -> char {
|
fn _ascii_representation(&self) -> char {
|
||||||
|
|
|
@ -11,7 +11,7 @@ pub struct Pieces<'a> {
|
||||||
|
|
||||||
current_shape: Option<Shape>,
|
current_shape: Option<Shape>,
|
||||||
|
|
||||||
shape_iterator: Box<dyn Iterator<Item = Shape>>,
|
shape_iterator: Box<dyn Iterator<Item = &'static Shape>>,
|
||||||
square_iterator: Option<Box<dyn Iterator<Item = Square>>>,
|
square_iterator: Option<Box<dyn Iterator<Item = Square>>>,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -41,7 +41,7 @@ impl<'a> Iterator for Pieces<'a> {
|
||||||
let mut next_nonempty_bitboard: Option<&BitBoard> = None;
|
let mut next_nonempty_bitboard: Option<&BitBoard> = None;
|
||||||
|
|
||||||
while let Some(shape) = self.shape_iterator.next() {
|
while let Some(shape) = self.shape_iterator.next() {
|
||||||
let piece = Piece::new(self.color, shape);
|
let piece = Piece::new(self.color, *shape);
|
||||||
|
|
||||||
let bitboard = self.position.bitboard_for_piece(piece);
|
let bitboard = self.position.bitboard_for_piece(piece);
|
||||||
if bitboard.is_empty() {
|
if bitboard.is_empty() {
|
||||||
|
@ -49,7 +49,7 @@ impl<'a> Iterator for Pieces<'a> {
|
||||||
}
|
}
|
||||||
|
|
||||||
next_nonempty_bitboard = Some(bitboard);
|
next_nonempty_bitboard = Some(bitboard);
|
||||||
current_shape = Some(shape);
|
current_shape = Some(*shape);
|
||||||
|
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
|
@ -129,7 +129,7 @@ impl Position {
|
||||||
pub fn piece_on_square(&self, sq: Square) -> Option<PlacedPiece> {
|
pub fn piece_on_square(&self, sq: Square) -> Option<PlacedPiece> {
|
||||||
for color in Color::iter() {
|
for color in Color::iter() {
|
||||||
for shape in Shape::iter() {
|
for shape in Shape::iter() {
|
||||||
let piece = Piece::new(color, shape);
|
let piece = Piece::new(color, *shape);
|
||||||
let bb = self.bitboard_for_piece(piece);
|
let bb = self.bitboard_for_piece(piece);
|
||||||
if bb.has_piece_at(&sq) {
|
if bb.has_piece_at(&sq) {
|
||||||
return Some(PlacedPiece::new(piece, sq));
|
return Some(PlacedPiece::new(piece, sq));
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue