Skip to content

Commit

Permalink
chore: reintroduce cartesiean parsing functions
Browse files Browse the repository at this point in the history
  • Loading branch information
raklaptudirm committed Jan 22, 2025
1 parent 2addea1 commit 555037a
Show file tree
Hide file tree
Showing 6 changed files with 145 additions and 71 deletions.
30 changes: 14 additions & 16 deletions games/src/games/ataxx.rs
Original file line number Diff line number Diff line change
Expand Up @@ -361,7 +361,7 @@ impl FromStr for Position {
half_move_clock: 0,
};

// TODO: parse::piece_placement(&mut position, pos)?;
parse::piece_placement(&mut position, pos)?;

position.side_to_move = Color::from_str(stm)?;
position.half_move_clock = hmc.parse::<u8>()?;
Expand All @@ -383,21 +383,19 @@ impl fmt::Display for Position {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
let board = self;
let mut string_rep = String::from(" ");
/* TODO
for rank in Rank::iter().rev() {
for file in File::iter() {
let square = Square::new(file, rank);
let square_str = match board.at(square) {
Some(piece) => format!("{} ", piece),
None => ". ".to_string(),
};
string_rep += &square_str;
}
// Append the rank marker.
string_rep += &format!(" {} \n ", rank);
}
*/
for rank in Rank::iter().rev() {
for file in File::iter() {
let square = Square::new(file, rank);
let square_str = match board.at(square) {
Some(piece) => format!("{} ", piece),
None => ". ".to_string(),
};
string_rep += &square_str;
}

// Append the rank marker.
string_rep += &format!(" {} \n ", rank);
}
// Append the file markers.
string_rep += "a b c d e f g\n";

Expand Down
12 changes: 6 additions & 6 deletions games/src/games/chess/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,19 +69,19 @@ impl BitBoard {
}

static DIAGONAL: LazyLock<[BitBoard; 15]> = LazyLock::new(|| {
let MAIN_DIAGONAL = BitBoard::from(0x8040201008040201u64);
let main_diagonal = BitBoard::from(0x8040201008040201u64);

let mut diagonals = [BitBoard::EMPTY; 15];

// Initialize diagonals to the east of the main.
let mut upper_triangle = MAIN_DIAGONAL;
let mut upper_triangle = main_diagonal;
for diagonal in diagonals.iter_mut().rev().skip(7) {
*diagonal = upper_triangle;
upper_triangle = upper_triangle.east();
}

// Initialize diagonals to the west of the main.
let mut upper_triangle = MAIN_DIAGONAL;
let mut upper_triangle = main_diagonal;
for diagonal in diagonals.iter_mut().skip(7) {
*diagonal = upper_triangle;
upper_triangle = upper_triangle.west();
Expand All @@ -91,19 +91,19 @@ static DIAGONAL: LazyLock<[BitBoard; 15]> = LazyLock::new(|| {
});

static ANTI_DIAGONAL: LazyLock<[BitBoard; 15]> = LazyLock::new(|| {
let MAIN_ANTI_DIAGONAL = BitBoard::from(0x0102040810204080u64);
let main_anti_diagonal = BitBoard::from(0x0102040810204080u64);

let mut anti_diagonals = [BitBoard::EMPTY; 15];

// Initialize anti-diagonals to the east of the main.
let mut upper_triangle = MAIN_ANTI_DIAGONAL;
let mut upper_triangle = main_anti_diagonal;
for anti_diagonal in anti_diagonals.iter_mut().skip(7) {
*anti_diagonal = upper_triangle;
upper_triangle = upper_triangle.east();
}

// Initialize anti-diagonals to the west of the main.
let mut upper_triangle = MAIN_ANTI_DIAGONAL;
let mut upper_triangle = main_anti_diagonal;
for anti_diagonal in anti_diagonals.iter_mut().rev().skip(7) {
*anti_diagonal = upper_triangle;
upper_triangle = upper_triangle.west();
Expand Down
33 changes: 16 additions & 17 deletions games/src/games/chess/position.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ use std::{fmt, num::ParseIntError, str::FromStr};

use super::{
castling::{self, CastlingRightsParseError, Dimension, Side},
movegen, BitBoard, Color, ColoredPiece, Move, MoveFlag, Piece, Square,
movegen, BitBoard, Color, ColoredPiece, File, Move, MoveFlag, Piece, Rank,
Square,
};
use crate::interface::{
self, parse::PiecePlacementParseError, ColoredPieceType, Hash, MoveStore,
Expand Down Expand Up @@ -257,7 +258,7 @@ impl FromStr for Position {
castling: Default::default(),
};

// TODO: interface::parse::piece_placement(&mut position, pos)?;
interface::parse::piece_placement(&mut position, pos)?;

let kings = position.piece_bb(Piece::King);
let white_king = (kings & position.color_bb(Color::White)).next();
Expand Down Expand Up @@ -290,21 +291,19 @@ impl fmt::Display for Position {
let board = self;
let mut string_rep = String::from(" ");

/*
for rank in Rank::iter().rev() {
for file in File::iter() {
let square = Square::new(file, rank);
let square_str = match board.at(square) {
Some(piece) => format!("{} ", piece),
None => ". ".to_string(),
};
string_rep += &square_str;
}
// Append the rank marker.
string_rep += &format!(" {} \n ", rank);
}
*/
for rank in Rank::iter().rev() {
for file in File::iter() {
let square = Square::new(file, rank);
let square_str = match board.at(square) {
Some(piece) => format!("{} ", piece),
None => ". ".to_string(),
};
string_rep += &square_str;
}

// Append the rank marker.
string_rep += &format!(" {} \n ", rank);
}

// Append the file markers.
string_rep += "a b c d e f g\n";
Expand Down
30 changes: 14 additions & 16 deletions games/src/games/isolation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -280,7 +280,7 @@ impl FromStr for Position {
ply_count: 0,
};

// TODO: parse::piece_placement(&mut position, pos)?;
parse::piece_placement(&mut position, pos)?;

position.side_to_move = Color::from_str(stm)?;
position.ply_count = parse::ply_count(fmc, position.side_to_move)?;
Expand All @@ -302,21 +302,19 @@ impl fmt::Display for Position {
let board = self;
let mut string_rep = String::from(" ");

/*
for rank in Rank::iter().rev() {
for file in File::iter() {
let square = Square::new(file, rank);
let square_str = match board.at(square) {
Some(piece) => format!("{} ", piece),
None => ". ".to_string(),
};
string_rep += &square_str;
}
// Append the rank marker.
string_rep += &format!(" {} \n ", rank);
}
*/
for rank in Rank::iter().rev() {
for file in File::iter() {
let square = Square::new(file, rank);
let square_str = match board.at(square) {
Some(piece) => format!("{} ", piece),
None => ". ".to_string(),
};
string_rep += &square_str;
}

// Append the rank marker.
string_rep += &format!(" {} \n ", rank);
}

// Append the file markers.
string_rep += "a b c d e f g h\n";
Expand Down
37 changes: 21 additions & 16 deletions games/src/interface/parse.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,17 @@
// See the License for the specific language governing permissions and
// limitations under the License.

use std::num::ParseIntError;
use std::{num::ParseIntError, str::FromStr};

use crate::interface::ColorType;
use crate::interface::{
CartesianFile, CartesianRank, CartesianSquare, ColorType, ColoredPiece,
PositionType,
};

use thiserror::Error;

use strum::IntoEnumIterator;

/// PositionParseErr represents an error encountered while parsing
/// the given FEN position field into a valid Position.
#[derive(Error, Debug)]
Expand All @@ -32,10 +37,8 @@ pub enum PiecePlacementParseError {
TooManyRanks(usize),
}

/*
pub(crate) fn piece_placement<
T: PositionType<Square = CartesianSquare<B, F, R>>,
B: PrimInt,
T: PositionType<Square = CartesianSquare<F, R>>,
const F: u8,
const R: u8,
>(
Expand All @@ -49,17 +52,17 @@ pub(crate) fn piece_placement<
// Spilt the position spec by the Ranks which are separated by '/'.
let ranks: Vec<&str> = fen_fragment.split('/').collect();

let first_file = File::<T>::iter().next().unwrap();
let first_file = CartesianFile::<F>::iter().next().unwrap();

let mut file = Ok(first_file);
let mut rank = Ok(Rank::<T>::iter().last().unwrap());
let mut rank = Ok(CartesianRank::<R>::iter().last().unwrap());

// Iterate over the Ranks in the string spec.
for rank_data in ranks {
// Rank pointer ran out, but data carried on.
if rank.is_err() {
return Err(PiecePlacementParseError::TooManyRanks(
Rank::<T>::iter().len(),
CartesianRank::<R>::iter().len(),
));
}

Expand All @@ -70,14 +73,14 @@ pub(crate) fn piece_placement<
return Err(PiecePlacementParseError::JumpTooLong);
}

let file_value = *file.as_ref().unwrap();
let rank_value = *rank.as_ref().unwrap();
let square = Square::<T>::new(file_value, rank_value);
let file_value = file.unwrap();
let rank_value = rank.unwrap();
let square = CartesianSquare::<F, R>::new(file_value, rank_value);
match data {
// Numbers represent jump specs to jump over empty squares.
'1'..='8' => {
file = File::<T>::try_from(
file_value.into() + data as u8 - b'1',
file = CartesianFile::<F>::try_from(
u8::from(file_value) + data as u8 - b'1',
);
if file.is_err() {
return Err(PiecePlacementParseError::JumpTooLong);
Expand All @@ -95,7 +98,7 @@ pub(crate) fn piece_placement<
}

// On to the next Square spec in the Rank spec.
file = <File<T>>::try_from(file.unwrap().into() + 1);
file = CartesianFile::<F>::try_from(u8::from(file.unwrap()) + 1);
}

// After rank data runs out, file pointer should be
Expand All @@ -107,13 +110,15 @@ pub(crate) fn piece_placement<
}

// Switch rank pointer and reset file pointer.
rank = Rank::<T>::try_from((rank.unwrap().into()).wrapping_sub(1));
rank = CartesianRank::<R>::try_from(
(u8::from(rank.unwrap())).wrapping_sub(1),
);
file = Ok(first_file);
}

Ok(())
}
*/

pub(crate) fn ply_count<C: ColorType>(
fmc: &str,
stm: C,
Expand Down
74 changes: 74 additions & 0 deletions games/src/interface/square.rs
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,43 @@ impl<A, const C: u8, const N: usize> IndexMut<CartesianFile<C>> for [A; N] {
}
}

impl<const C: u8> IntoEnumIterator for CartesianFile<C> {
type Iterator = Self;

fn iter() -> Self::Iterator {
Self(0)
}
}

impl<const C: u8> Iterator for CartesianFile<C> {
type Item = Self;

fn next(&mut self) -> Option<Self::Item> {
let idx = (*self).into();
if idx < C {
self.0 = idx + 1;
Some(Self(idx))
} else {
None
}
}
}

impl<const C: u8> DoubleEndedIterator for CartesianFile<C> {
fn next_back(&mut self) -> Option<Self::Item> {
let idx = (*self).into();
self.0 = idx - 1;
if idx < C {
Some(Self(idx))
} else {
None
}
}
}

impl<const C: u8> FusedIterator for CartesianFile<C> {}
impl<const C: u8> ExactSizeIterator for CartesianFile<C> {}

#[derive(Clone, Copy, PartialEq, Eq, derive_more::Into, derive_more::From)]
pub struct CartesianRank<const N: u8>(u8);

Expand Down Expand Up @@ -284,3 +321,40 @@ impl<A, const C: u8, const N: usize> IndexMut<CartesianRank<C>> for [A; N] {
&mut self[u8::from(index) as usize]
}
}

impl<const C: u8> IntoEnumIterator for CartesianRank<C> {
type Iterator = Self;

fn iter() -> Self::Iterator {
Self(0)
}
}

impl<const C: u8> Iterator for CartesianRank<C> {
type Item = Self;

fn next(&mut self) -> Option<Self::Item> {
let idx = (*self).into();
if idx < C {
self.0 = idx + 1;
Some(Self(idx))
} else {
None
}
}
}

impl<const C: u8> DoubleEndedIterator for CartesianRank<C> {
fn next_back(&mut self) -> Option<Self::Item> {
let idx = (*self).into();
self.0 = idx - 1;
if idx < C {
Some(Self(idx))
} else {
None
}
}
}

impl<const C: u8> FusedIterator for CartesianRank<C> {}
impl<const C: u8> ExactSizeIterator for CartesianRank<C> {}

0 comments on commit 555037a

Please sign in to comment.