Merge branch 'main' into pretty-print-position

This commit is contained in:
Eryn Wells 2023-12-26 13:28:55 -07:00
commit a963cee1e7
8 changed files with 52 additions and 34 deletions

View file

@ -1,9 +1,7 @@
// Eryn Wells <eryn@erynwells.me>
mod bit_scanner;
use self::bit_scanner::BitScanner;
use crate::square::Square;
use super::BitScanner;
use crate::Square;
use std::fmt;
use std::ops::{BitAnd, BitOr, Not};
@ -154,7 +152,7 @@ impl BitOr<&BitBoard> for BitBoard {
#[cfg(test)]
mod tests {
use super::*;
use crate::square::Square;
use crate::Square;
#[test]
fn is_empty() {
@ -165,10 +163,10 @@ mod tests {
fn has_piece_at() {
let bb = BitBoard(0b1001100);
let c1 = Square::from_algebraic_string("c1").expect("Unable to get square for 'c1'");
let c1 = Square::from_algebraic_str("c1").expect("Unable to get square for 'c1'");
assert!(bb.has_piece_at(&c1));
let b1 = Square::from_algebraic_string("b1").expect("Unable to get square for 'b1'");
let b1 = Square::from_algebraic_str("b1").expect("Unable to get square for 'b1'");
assert!(!bb.has_piece_at(&b1));
}

View file

@ -0,0 +1,5 @@
mod bit_scanner;
mod bitboard;
pub(crate) use self::bit_scanner::BitScanner;
pub(crate) use self::bitboard::BitBoard;

View file

@ -3,3 +3,7 @@ mod neighbor;
mod piece;
mod position;
mod square;
pub use piece::{Color, Piece};
pub use position::Position;
pub use square::Square;

View file

@ -1,6 +1,6 @@
// Eryn Wells <eryn@erynwells.me>
use crate::square::Square;
use crate::Square;
#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)]
pub enum Color {

View file

@ -0,0 +1,5 @@
mod pieces;
mod position;
pub use pieces::Pieces;
pub use position::Position;

View file

@ -1,10 +1,9 @@
// Eryn Wells <eryn@erynwells.me>
use super::Position;
use crate::bitboard::BitBoard;
use crate::piece::{Color, Piece, PlacedPiece, Shape};
use crate::position::Position;
use crate::square::Square;
use std::collections::HashSet;
use crate::Square;
pub struct Pieces<'a> {
color: Color,
@ -89,11 +88,12 @@ impl<'a> Iterator for Pieces<'a> {
mod tests {
use super::*;
use crate::piece::{Color, Piece, Shape};
use crate::position::Position;
use crate::square::Square;
use crate::Position;
use crate::Square;
use std::collections::HashSet;
fn square_at(sq: &str) -> Square {
Square::from_algebraic_string(sq).expect(sq)
Square::from_algebraic_str(sq).expect(sq)
}
fn place_piece_in_position(pos: &mut Position, sq: &str, piece: Piece) {
@ -110,7 +110,7 @@ mod tests {
#[test]
fn one() {
let sq = Square::from_algebraic_string("e4").expect("e4");
let sq = Square::from_algebraic_str("e4").expect("e4");
let mut pos = Position::empty();
pos.place_piece(&Piece::new(Color::White, Shape::Queen), &sq)

View file

@ -1,16 +1,16 @@
// Eryn Wells <eryn@erynwells.me>
mod pieces;
use self::pieces::Pieces;
use super::Pieces;
use crate::bitboard::BitBoard;
use crate::piece::{Piece, PiecePlacementError};
use crate::square::Square;
use crate::piece::{Color, Piece, PiecePlacementError};
use crate::Square;
use std::fmt;
use std::fmt::Write;
#[derive(Clone, Eq, Hash, PartialEq)]
pub struct Position {
color_to_move: Color,
/// Composite bitboards for all the pieces of a particular color.
pieces_per_color: [BitBoard; 2],
@ -19,8 +19,9 @@ pub struct Position {
}
impl Position {
fn empty() -> Position {
pub fn empty() -> Position {
Position {
color_to_move: Color::White,
pieces_per_color: [BitBoard::empty(), BitBoard::empty()],
pieces_per_type: [
[
@ -64,6 +65,7 @@ impl Position {
];
Position {
color_to_move: Color::White,
pieces_per_color: [
white_pieces.iter().fold(BitBoard::empty(), |a, b| a | b),
black_pieces.iter().fold(BitBoard::empty(), |a, b| a | b),
@ -72,7 +74,11 @@ impl Position {
}
}
fn place_piece(&mut self, piece: &Piece, square: &Square) -> Result<(), PiecePlacementError> {
pub fn place_piece(
&mut self,
piece: &Piece,
square: &Square,
) -> Result<(), PiecePlacementError> {
let bitboard = self.bitboard_for_piece_mut(piece);
if bitboard.has_piece_at(&square) {
@ -92,7 +98,7 @@ impl Position {
&mut self.pieces_per_type[piece.color as usize][piece.shape as usize]
}
fn pieces(&self, color: Color) -> Pieces {
pub fn pieces(&self, color: Color) -> Pieces {
Pieces::new(&self, color)
}
}
@ -159,7 +165,7 @@ mod tests {
let mut position = Position::empty();
let piece = Piece::new(Color::White, Shape::Queen);
let square = Square::from_algebraic_string("e4").expect("Unable to get e4 square");
let square = Square::from_algebraic_str("e4").expect("Unable to get e4 square");
position
.place_piece(&piece, &square)

View file

@ -45,7 +45,7 @@ impl Square {
})
}
pub fn from_algebraic_string(s: &str) -> Result<Square, ParseSquareError> {
pub fn from_algebraic_str(s: &str) -> Result<Square, ParseSquareError> {
s.parse()
}
@ -142,27 +142,27 @@ mod tests {
#[test]
fn good_algebraic_input() {
let sq1 = Square::from_algebraic_string("a4").expect("Failed to parse 'a4' square");
let sq1 = Square::from_algebraic_str("a4").expect("Failed to parse 'a4' square");
assert_eq!(sq1.file, 0);
assert_eq!(sq1.rank, 3);
let sq2 = Square::from_algebraic_string("B8").expect("Failed to parse 'B8' square");
let sq2 = Square::from_algebraic_str("B8").expect("Failed to parse 'B8' square");
assert_eq!(sq2.file, 1);
assert_eq!(sq2.rank, 7);
let sq3 = Square::from_algebraic_string("e4").expect("Failed to parse 'B8' square");
let sq3 = Square::from_algebraic_str("e4").expect("Failed to parse 'B8' square");
assert_eq!(sq3.rank, 3, "Expected rank of e4 to be 3");
assert_eq!(sq3.file, 4, "Expected file of e4 to be 4");
}
#[test]
fn bad_algebraic_input() {
Square::from_algebraic_string("a0").expect_err("Got valid Square for 'a0'");
Square::from_algebraic_string("j3").expect_err("Got valid Square for 'j3'");
Square::from_algebraic_string("a11").expect_err("Got valid Square for 'a11'");
Square::from_algebraic_string("b-1").expect_err("Got valid Square for 'b-1'");
Square::from_algebraic_string("a 1").expect_err("Got valid Square for 'a 1'");
Square::from_algebraic_string("").expect_err("Got valid Square for ''");
Square::from_algebraic_str("a0").expect_err("Got valid Square for 'a0'");
Square::from_algebraic_str("j3").expect_err("Got valid Square for 'j3'");
Square::from_algebraic_str("a11").expect_err("Got valid Square for 'a11'");
Square::from_algebraic_str("b-1").expect_err("Got valid Square for 'b-1'");
Square::from_algebraic_str("a 1").expect_err("Got valid Square for 'a 1'");
Square::from_algebraic_str("").expect_err("Got valid Square for ''");
}
#[test]