finally make an actually good fen parser (fun fact: yeah i was being an idiot)

This commit is contained in:
c0repwn3r 2023-02-13 08:45:09 -05:00
parent 73be31dd27
commit 0483f4ce86
Signed by: core
GPG Key ID: FDBF740DADDCEECF
5 changed files with 119 additions and 99 deletions

View File

@ -142,6 +142,7 @@ impl Board {
} }
fn create_fen_piece(c: char, rank: usize, file: usize) -> Result<PieceOnBoard, FENParseError> { fn create_fen_piece(c: char, rank: usize, file: usize) -> Result<PieceOnBoard, FENParseError> {
println!("playing {} at {} ({}, {})", c, rank + file * 8, file, rank);
Ok(PieceOnBoard { Ok(PieceOnBoard {
loc: rank + file * 8, loc: rank + file * 8,
value: match c { value: match c {
@ -185,6 +186,7 @@ fn create_bitfield_piece(c: char) -> Result<u8, FENParseError> {
mod tests { mod tests {
use crate::board::Board; use crate::board::Board;
use crate::boardfield::BoardfieldOps; use crate::boardfield::BoardfieldOps;
use crate::boardloc;
use crate::piece::{PieceColor, PieceType}; use crate::piece::{PieceColor, PieceType};
#[test] #[test]
@ -192,40 +194,40 @@ mod tests {
let board = Board::from_fen("rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1").unwrap(); let board = Board::from_fen("rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1").unwrap();
let field = board.bitfield; let field = board.bitfield;
assert_eq!(field.get_pos(0), PieceType::Rook as u8 | PieceColor::White as u8); assert_eq!(field.get_pos(boardloc!("a1")), PieceType::Rook as u8 | PieceColor::White as u8);
assert_eq!(field.get_pos(1), PieceType::Knight as u8 | PieceColor::White as u8); assert_eq!(field.get_pos(boardloc!("b1")), PieceType::Knight as u8 | PieceColor::White as u8);
assert_eq!(field.get_pos(2), PieceType::Bishop as u8 | PieceColor::White as u8); assert_eq!(field.get_pos(boardloc!("c1")), PieceType::Bishop as u8 | PieceColor::White as u8);
assert_eq!(field.get_pos(3), PieceType::Queen as u8 | PieceColor::White as u8); assert_eq!(field.get_pos(boardloc!("d1")), PieceType::Queen as u8 | PieceColor::White as u8);
assert_eq!(field.get_pos(4), PieceType::King as u8 | PieceColor::White as u8); assert_eq!(field.get_pos(boardloc!("e1")), PieceType::King as u8 | PieceColor::White as u8);
assert_eq!(field.get_pos(5), PieceType::Bishop as u8 | PieceColor::White as u8); assert_eq!(field.get_pos(boardloc!("f1")), PieceType::Bishop as u8 | PieceColor::White as u8);
assert_eq!(field.get_pos(6), PieceType::Knight as u8 | PieceColor::White as u8); assert_eq!(field.get_pos(boardloc!("g1")), PieceType::Knight as u8 | PieceColor::White as u8);
assert_eq!(field.get_pos(7), PieceType::Rook as u8 | PieceColor::White as u8); assert_eq!(field.get_pos(boardloc!("h1")), PieceType::Rook as u8 | PieceColor::White as u8);
assert_eq!(field.get_pos(8), PieceType::Pawn as u8 | PieceColor::White as u8); assert_eq!(field.get_pos(boardloc!("a2")), PieceType::Pawn as u8 | PieceColor::White as u8);
assert_eq!(field.get_pos(9), PieceType::Pawn as u8 | PieceColor::White as u8); assert_eq!(field.get_pos(boardloc!("b2")), PieceType::Pawn as u8 | PieceColor::White as u8);
assert_eq!(field.get_pos(10), PieceType::Pawn as u8 | PieceColor::White as u8); assert_eq!(field.get_pos(boardloc!("c2")), PieceType::Pawn as u8 | PieceColor::White as u8);
assert_eq!(field.get_pos(11), PieceType::Pawn as u8 | PieceColor::White as u8); assert_eq!(field.get_pos(boardloc!("d2")), PieceType::Pawn as u8 | PieceColor::White as u8);
assert_eq!(field.get_pos(12), PieceType::Pawn as u8 | PieceColor::White as u8); assert_eq!(field.get_pos(boardloc!("e2")), PieceType::Pawn as u8 | PieceColor::White as u8);
assert_eq!(field.get_pos(13), PieceType::Pawn as u8 | PieceColor::White as u8); assert_eq!(field.get_pos(boardloc!("f2")), PieceType::Pawn as u8 | PieceColor::White as u8);
assert_eq!(field.get_pos(14), PieceType::Pawn as u8 | PieceColor::White as u8); assert_eq!(field.get_pos(boardloc!("g2")), PieceType::Pawn as u8 | PieceColor::White as u8);
assert_eq!(field.get_pos(15), PieceType::Pawn as u8 | PieceColor::White as u8); assert_eq!(field.get_pos(boardloc!("h2")), PieceType::Pawn as u8 | PieceColor::White as u8);
assert_eq!(field.get_pos(48), PieceType::Pawn as u8 | PieceColor::Black as u8); assert_eq!(field.get_pos(boardloc!("a7")), PieceType::Pawn as u8 | PieceColor::Black as u8);
assert_eq!(field.get_pos(49), PieceType::Pawn as u8 | PieceColor::Black as u8); assert_eq!(field.get_pos(boardloc!("b7")), PieceType::Pawn as u8 | PieceColor::Black as u8);
assert_eq!(field.get_pos(50), PieceType::Pawn as u8 | PieceColor::Black as u8); assert_eq!(field.get_pos(boardloc!("c7")), PieceType::Pawn as u8 | PieceColor::Black as u8);
assert_eq!(field.get_pos(51), PieceType::Pawn as u8 | PieceColor::Black as u8); assert_eq!(field.get_pos(boardloc!("d7")), PieceType::Pawn as u8 | PieceColor::Black as u8);
assert_eq!(field.get_pos(52), PieceType::Pawn as u8 | PieceColor::Black as u8); assert_eq!(field.get_pos(boardloc!("e7")), PieceType::Pawn as u8 | PieceColor::Black as u8);
assert_eq!(field.get_pos(53), PieceType::Pawn as u8 | PieceColor::Black as u8); assert_eq!(field.get_pos(boardloc!("f7")), PieceType::Pawn as u8 | PieceColor::Black as u8);
assert_eq!(field.get_pos(54), PieceType::Pawn as u8 | PieceColor::Black as u8); assert_eq!(field.get_pos(boardloc!("g7")), PieceType::Pawn as u8 | PieceColor::Black as u8);
assert_eq!(field.get_pos(55), PieceType::Pawn as u8 | PieceColor::Black as u8); assert_eq!(field.get_pos(boardloc!("h7")), PieceType::Pawn as u8 | PieceColor::Black as u8);
assert_eq!(field.get_pos(56), PieceType::Rook as u8 | PieceColor::Black as u8); assert_eq!(field.get_pos(boardloc!("a8")), PieceType::Rook as u8 | PieceColor::Black as u8);
assert_eq!(field.get_pos(57), PieceType::Knight as u8 | PieceColor::Black as u8); assert_eq!(field.get_pos(boardloc!("b8")), PieceType::Knight as u8 | PieceColor::Black as u8);
assert_eq!(field.get_pos(58), PieceType::Bishop as u8 | PieceColor::Black as u8); assert_eq!(field.get_pos(boardloc!("c8")), PieceType::Bishop as u8 | PieceColor::Black as u8);
assert_eq!(field.get_pos(59), PieceType::Queen as u8 | PieceColor::Black as u8); assert_eq!(field.get_pos(boardloc!("d8")), PieceType::Queen as u8 | PieceColor::Black as u8);
assert_eq!(field.get_pos(60), PieceType::King as u8 | PieceColor::Black as u8); assert_eq!(field.get_pos(boardloc!("e8")), PieceType::King as u8 | PieceColor::Black as u8);
assert_eq!(field.get_pos(61), PieceType::Bishop as u8 | PieceColor::Black as u8); assert_eq!(field.get_pos(boardloc!("f8")), PieceType::Bishop as u8 | PieceColor::Black as u8);
assert_eq!(field.get_pos(62), PieceType::Knight as u8 | PieceColor::Black as u8); assert_eq!(field.get_pos(boardloc!("g8")), PieceType::Knight as u8 | PieceColor::Black as u8);
assert_eq!(field.get_pos(63), PieceType::Rook as u8 | PieceColor::Black as u8); assert_eq!(field.get_pos(boardloc!("h8")), PieceType::Rook as u8 | PieceColor::Black as u8);
} }
} }

View File

@ -24,6 +24,7 @@
a b c d e f g h a b c d e f g h
*/ */
use crate::boardloc;
use crate::piece::{PieceColor, PieceType}; use crate::piece::{PieceColor, PieceType};
pub type Boardfield = [u8; 32]; pub type Boardfield = [u8; 32];
@ -42,44 +43,44 @@ impl BoardfieldOps for Boardfield {
let mut field = Self::new(); let mut field = Self::new();
// a1-h1 (RNBQKBNR) // a1-h1 (RNBQKBNR)
field.set_pos(0, PieceColor::White as u8 | PieceType::Rook as u8); field.set_pos(boardloc!("a1"), PieceColor::White as u8 | PieceType::Rook as u8);
field.set_pos(1, PieceColor::White as u8 | PieceType::Knight as u8); field.set_pos(boardloc!("b1"), PieceColor::White as u8 | PieceType::Knight as u8);
field.set_pos(2, PieceColor::White as u8 | PieceType::Bishop as u8); field.set_pos(boardloc!("c1"), PieceColor::White as u8 | PieceType::Bishop as u8);
field.set_pos(3, PieceColor::White as u8 | PieceType::Queen as u8); field.set_pos(boardloc!("d1"), PieceColor::White as u8 | PieceType::Queen as u8);
field.set_pos(4, PieceColor::White as u8 | PieceType::King as u8); field.set_pos(boardloc!("e1"), PieceColor::White as u8 | PieceType::King as u8);
field.set_pos(5, PieceColor::White as u8 | PieceType::Bishop as u8); field.set_pos(boardloc!("f1"), PieceColor::White as u8 | PieceType::Bishop as u8);
field.set_pos(6, PieceColor::White as u8 | PieceType::Knight as u8); field.set_pos(boardloc!("g1"), PieceColor::White as u8 | PieceType::Knight as u8);
field.set_pos(7, PieceColor::White as u8 | PieceType::Rook as u8); field.set_pos(boardloc!("h1"), PieceColor::White as u8 | PieceType::Rook as u8);
// a2-h2 (PPPPPPPP) // a2-h2 (PPPPPPPP)
field.set_pos(8, PieceColor::White as u8 | PieceType::Pawn as u8); field.set_pos(boardloc!("a2"), PieceColor::White as u8 | PieceType::Pawn as u8);
field.set_pos(9, PieceColor::White as u8 | PieceType::Pawn as u8); field.set_pos(boardloc!("b2"), PieceColor::White as u8 | PieceType::Pawn as u8);
field.set_pos(10, PieceColor::White as u8 | PieceType::Pawn as u8); field.set_pos(boardloc!("c2"), PieceColor::White as u8 | PieceType::Pawn as u8);
field.set_pos(11, PieceColor::White as u8 | PieceType::Pawn as u8); field.set_pos(boardloc!("d2"), PieceColor::White as u8 | PieceType::Pawn as u8);
field.set_pos(12, PieceColor::White as u8 | PieceType::Pawn as u8); field.set_pos(boardloc!("e2"), PieceColor::White as u8 | PieceType::Pawn as u8);
field.set_pos(13, PieceColor::White as u8 | PieceType::Pawn as u8); field.set_pos(boardloc!("f2"), PieceColor::White as u8 | PieceType::Pawn as u8);
field.set_pos(14, PieceColor::White as u8 | PieceType::Pawn as u8); field.set_pos(boardloc!("g2"), PieceColor::White as u8 | PieceType::Pawn as u8);
field.set_pos(15, PieceColor::White as u8 | PieceType::Pawn as u8); field.set_pos(boardloc!("h2"), PieceColor::White as u8 | PieceType::Pawn as u8);
// a7-h7 (pppppppp) // a7-h7 (pppppppp)
field.set_pos(48, PieceColor::Black as u8 | PieceType::Pawn as u8); field.set_pos(boardloc!("a7"), PieceColor::Black as u8 | PieceType::Pawn as u8);
field.set_pos(49, PieceColor::Black as u8 | PieceType::Pawn as u8); field.set_pos(boardloc!("b7"), PieceColor::Black as u8 | PieceType::Pawn as u8);
field.set_pos(50, PieceColor::Black as u8 | PieceType::Pawn as u8); field.set_pos(boardloc!("c7"), PieceColor::Black as u8 | PieceType::Pawn as u8);
field.set_pos(51, PieceColor::Black as u8 | PieceType::Pawn as u8); field.set_pos(boardloc!("d7"), PieceColor::Black as u8 | PieceType::Pawn as u8);
field.set_pos(52, PieceColor::Black as u8 | PieceType::Pawn as u8); field.set_pos(boardloc!("e7"), PieceColor::Black as u8 | PieceType::Pawn as u8);
field.set_pos(53, PieceColor::Black as u8 | PieceType::Pawn as u8); field.set_pos(boardloc!("f7"), PieceColor::Black as u8 | PieceType::Pawn as u8);
field.set_pos(54, PieceColor::Black as u8 | PieceType::Pawn as u8); field.set_pos(boardloc!("g7"), PieceColor::Black as u8 | PieceType::Pawn as u8);
field.set_pos(55, PieceColor::Black as u8 | PieceType::Pawn as u8); field.set_pos(boardloc!("h7"), PieceColor::Black as u8 | PieceType::Pawn as u8);
// a8-h8 (RNBQKBNR) // a8-h8 (RNBQKBNR)
field.set_pos(56, PieceColor::Black as u8 | PieceType::Rook as u8); field.set_pos(boardloc!("a8"), PieceColor::Black as u8 | PieceType::Rook as u8);
field.set_pos(57, PieceColor::Black as u8 | PieceType::Knight as u8); field.set_pos(boardloc!("b8"), PieceColor::Black as u8 | PieceType::Knight as u8);
field.set_pos(58, PieceColor::Black as u8 | PieceType::Bishop as u8); field.set_pos(boardloc!("c8"), PieceColor::Black as u8 | PieceType::Bishop as u8);
field.set_pos(59, PieceColor::Black as u8 | PieceType::Queen as u8); field.set_pos(boardloc!("d8"), PieceColor::Black as u8 | PieceType::Queen as u8);
field.set_pos(60, PieceColor::Black as u8 | PieceType::King as u8); field.set_pos(boardloc!("e8"), PieceColor::Black as u8 | PieceType::King as u8);
field.set_pos(61, PieceColor::Black as u8 | PieceType::Bishop as u8); field.set_pos(boardloc!("f8"), PieceColor::Black as u8 | PieceType::Bishop as u8);
field.set_pos(62, PieceColor::Black as u8 | PieceType::Knight as u8); field.set_pos(boardloc!("g8"), PieceColor::Black as u8 | PieceType::Knight as u8);
field.set_pos(63, PieceColor::Black as u8 | PieceType::Rook as u8); field.set_pos(boardloc!("h8"), PieceColor::Black as u8 | PieceType::Rook as u8);
field field
@ -121,6 +122,7 @@ impl BoardfieldOps for Boardfield {
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use crate::boardfield::{Boardfield, BoardfieldOps}; use crate::boardfield::{Boardfield, BoardfieldOps};
use crate::boardloc;
use crate::piece::{PieceColor, PieceType}; use crate::piece::{PieceColor, PieceType};
#[test] #[test]
@ -141,40 +143,40 @@ mod tests {
fn bitfield_board() { fn bitfield_board() {
let field = Boardfield::startpos(); let field = Boardfield::startpos();
assert_eq!(field.get_pos(0), PieceType::Rook as u8 | PieceColor::White as u8); assert_eq!(field.get_pos(boardloc!("a1")), PieceType::Rook as u8 | PieceColor::White as u8);
assert_eq!(field.get_pos(1), PieceType::Knight as u8 | PieceColor::White as u8); assert_eq!(field.get_pos(boardloc!("b1")), PieceType::Knight as u8 | PieceColor::White as u8);
assert_eq!(field.get_pos(2), PieceType::Bishop as u8 | PieceColor::White as u8); assert_eq!(field.get_pos(boardloc!("c1")), PieceType::Bishop as u8 | PieceColor::White as u8);
assert_eq!(field.get_pos(3), PieceType::Queen as u8 | PieceColor::White as u8); assert_eq!(field.get_pos(boardloc!("d1")), PieceType::Queen as u8 | PieceColor::White as u8);
assert_eq!(field.get_pos(4), PieceType::King as u8 | PieceColor::White as u8); assert_eq!(field.get_pos(boardloc!("e1")), PieceType::King as u8 | PieceColor::White as u8);
assert_eq!(field.get_pos(5), PieceType::Bishop as u8 | PieceColor::White as u8); assert_eq!(field.get_pos(boardloc!("f1")), PieceType::Bishop as u8 | PieceColor::White as u8);
assert_eq!(field.get_pos(6), PieceType::Knight as u8 | PieceColor::White as u8); assert_eq!(field.get_pos(boardloc!("g1")), PieceType::Knight as u8 | PieceColor::White as u8);
assert_eq!(field.get_pos(7), PieceType::Rook as u8 | PieceColor::White as u8); assert_eq!(field.get_pos(boardloc!("h1")), PieceType::Rook as u8 | PieceColor::White as u8);
assert_eq!(field.get_pos(8), PieceType::Pawn as u8 | PieceColor::White as u8); assert_eq!(field.get_pos(boardloc!("a2")), PieceType::Pawn as u8 | PieceColor::White as u8);
assert_eq!(field.get_pos(9), PieceType::Pawn as u8 | PieceColor::White as u8); assert_eq!(field.get_pos(boardloc!("b2")), PieceType::Pawn as u8 | PieceColor::White as u8);
assert_eq!(field.get_pos(10), PieceType::Pawn as u8 | PieceColor::White as u8); assert_eq!(field.get_pos(boardloc!("c2")), PieceType::Pawn as u8 | PieceColor::White as u8);
assert_eq!(field.get_pos(11), PieceType::Pawn as u8 | PieceColor::White as u8); assert_eq!(field.get_pos(boardloc!("d2")), PieceType::Pawn as u8 | PieceColor::White as u8);
assert_eq!(field.get_pos(12), PieceType::Pawn as u8 | PieceColor::White as u8); assert_eq!(field.get_pos(boardloc!("e2")), PieceType::Pawn as u8 | PieceColor::White as u8);
assert_eq!(field.get_pos(13), PieceType::Pawn as u8 | PieceColor::White as u8); assert_eq!(field.get_pos(boardloc!("f2")), PieceType::Pawn as u8 | PieceColor::White as u8);
assert_eq!(field.get_pos(14), PieceType::Pawn as u8 | PieceColor::White as u8); assert_eq!(field.get_pos(boardloc!("g2")), PieceType::Pawn as u8 | PieceColor::White as u8);
assert_eq!(field.get_pos(15), PieceType::Pawn as u8 | PieceColor::White as u8); assert_eq!(field.get_pos(boardloc!("h2")), PieceType::Pawn as u8 | PieceColor::White as u8);
assert_eq!(field.get_pos(48), PieceType::Pawn as u8 | PieceColor::Black as u8); assert_eq!(field.get_pos(boardloc!("a7")), PieceType::Pawn as u8 | PieceColor::Black as u8);
assert_eq!(field.get_pos(49), PieceType::Pawn as u8 | PieceColor::Black as u8); assert_eq!(field.get_pos(boardloc!("b7")), PieceType::Pawn as u8 | PieceColor::Black as u8);
assert_eq!(field.get_pos(50), PieceType::Pawn as u8 | PieceColor::Black as u8); assert_eq!(field.get_pos(boardloc!("c7")), PieceType::Pawn as u8 | PieceColor::Black as u8);
assert_eq!(field.get_pos(51), PieceType::Pawn as u8 | PieceColor::Black as u8); assert_eq!(field.get_pos(boardloc!("d7")), PieceType::Pawn as u8 | PieceColor::Black as u8);
assert_eq!(field.get_pos(52), PieceType::Pawn as u8 | PieceColor::Black as u8); assert_eq!(field.get_pos(boardloc!("e7")), PieceType::Pawn as u8 | PieceColor::Black as u8);
assert_eq!(field.get_pos(53), PieceType::Pawn as u8 | PieceColor::Black as u8); assert_eq!(field.get_pos(boardloc!("f7")), PieceType::Pawn as u8 | PieceColor::Black as u8);
assert_eq!(field.get_pos(54), PieceType::Pawn as u8 | PieceColor::Black as u8); assert_eq!(field.get_pos(boardloc!("g7")), PieceType::Pawn as u8 | PieceColor::Black as u8);
assert_eq!(field.get_pos(55), PieceType::Pawn as u8 | PieceColor::Black as u8); assert_eq!(field.get_pos(boardloc!("h7")), PieceType::Pawn as u8 | PieceColor::Black as u8);
assert_eq!(field.get_pos(56), PieceType::Rook as u8 | PieceColor::Black as u8); assert_eq!(field.get_pos(boardloc!("a8")), PieceType::Rook as u8 | PieceColor::Black as u8);
assert_eq!(field.get_pos(57), PieceType::Knight as u8 | PieceColor::Black as u8); assert_eq!(field.get_pos(boardloc!("b8")), PieceType::Knight as u8 | PieceColor::Black as u8);
assert_eq!(field.get_pos(58), PieceType::Bishop as u8 | PieceColor::Black as u8); assert_eq!(field.get_pos(boardloc!("c8")), PieceType::Bishop as u8 | PieceColor::Black as u8);
assert_eq!(field.get_pos(59), PieceType::Queen as u8 | PieceColor::Black as u8); assert_eq!(field.get_pos(boardloc!("d8")), PieceType::Queen as u8 | PieceColor::Black as u8);
assert_eq!(field.get_pos(60), PieceType::King as u8 | PieceColor::Black as u8); assert_eq!(field.get_pos(boardloc!("e8")), PieceType::King as u8 | PieceColor::Black as u8);
assert_eq!(field.get_pos(61), PieceType::Bishop as u8 | PieceColor::Black as u8); assert_eq!(field.get_pos(boardloc!("f8")), PieceType::Bishop as u8 | PieceColor::Black as u8);
assert_eq!(field.get_pos(62), PieceType::Knight as u8 | PieceColor::Black as u8); assert_eq!(field.get_pos(boardloc!("g8")), PieceType::Knight as u8 | PieceColor::Black as u8);
assert_eq!(field.get_pos(63), PieceType::Rook as u8 | PieceColor::Black as u8); assert_eq!(field.get_pos(boardloc!("h8")), PieceType::Rook as u8 | PieceColor::Black as u8);
} }
} }

View File

@ -10,6 +10,7 @@ pub mod uci;
pub mod boardfield; pub mod boardfield;
pub mod piece; pub mod piece;
pub mod board; pub mod board;
#[macro_use]
pub mod utils; pub mod utils;
pub const ENGINE_NAME: &str = "Bamboo"; pub const ENGINE_NAME: &str = "Bamboo";

View File

@ -68,6 +68,7 @@ mod piece_tests {
assert_eq!(PieceType::Rook as u8 | PieceColor::White as u8, 0b1100); assert_eq!(PieceType::Rook as u8 | PieceColor::White as u8, 0b1100);
assert_eq!(PieceType::Rook as u8 | PieceColor::Black as u8, 0b0100); assert_eq!(PieceType::Rook as u8 | PieceColor::Black as u8, 0b0100);
assert_eq!(PieceType::Queen as u8 | PieceColor::White as u8, 0b1101); assert_eq!(PieceType::Queen as u8 | PieceColor::White as u8, 0b1101);
assert_eq!(PieceType::Queen as u8 | PieceColor::Black as u8, 0b0101); assert_eq!(PieceType::Queen as u8 | PieceColor::Black as u8, 0b0101);

View File

@ -47,22 +47,36 @@ pub fn algebraic_to_boardloc(algebraic: &str) -> Result<usize, AlgebraicNotation
if (file_char as u8) < b'a' || (file_char as u8) > b'h' { if (file_char as u8) < b'a' || (file_char as u8) > b'h' {
return Err(AlgebraicNotationError::InvalidFile { got: file_char.to_string() }) return Err(AlgebraicNotationError::InvalidFile { got: file_char.to_string() })
} }
if (rank_char as u8) < b'0' || (rank_char as u8) > b'8' { if (rank_char as u8) < b'1' || (rank_char as u8) > b'8' {
return Err(AlgebraicNotationError::InvalidRank { got: rank_char.to_string() }) return Err(AlgebraicNotationError::InvalidRank { got: rank_char.to_string() })
} }
let file = (file_char as u8 - 97) as usize; let file = (file_char as u8 - 97) as usize;
let rank = (rank_char as u8 - 48) as usize; let rank = (rank_char as u8 - 49) as usize;
Ok(rank + file * 8) Ok(rank + file * 8)
} }
const BOARDLOC_TO_ALG: [&str; 64] = ["a0", "a1", "a2", "a3", "a4", "a5", "a6", "a7", "b0", "b1", "b2", "b3", "b4", "b5", "b6", "b7", "c0", "c1", "c2", "c3", "c4", "c5", "c6", "c7", "d0", "d1", "d2", "d3", "d4", "d5", "d6", "d7", "e0", "e1", "e2", "e3", "e4", "e5", "e6", "e7", "f0", "f1", "f2", "f3", "f4", "f5", "f6", "f7", "g0", "g1", "g2", "g3", "g4", "g5", "g6", "g7", "h0", "h1", "h2", "h3", "h4", "h5", "h6", "h7"]; const BOARDLOC_TO_ALG: [&str; 64] = ["a1", "a2", "a3", "a4", "a5", "a6", "a7", "a8", "b1", "b2", "b3", "b4", "b5", "b6", "b7", "b8", "c1", "c2", "c3", "c4", "c5", "c6", "c7", "c8", "d1", "d2", "d3", "d4", "d5", "d6", "d7", "d8", "e1", "e2", "e3", "e4", "e5", "e6", "e7", "e8", "f1", "f2", "f3", "f4", "f5", "f6", "f7", "f8", "g1", "g2", "g3", "g4", "g5", "g6", "g7", "g8", "h1", "h2", "h3", "h4", "h5", "h6", "h7", "h8"];
pub fn boardloc_to_algebraic<'a>(boardloc: usize) -> &'a str { pub fn boardloc_to_algebraic<'a>(boardloc: usize) -> &'a str {
BOARDLOC_TO_ALG[boardloc] BOARDLOC_TO_ALG[boardloc]
} }
#[macro_export]
macro_rules! boardloc {
($alg:expr) => {
$crate::utils::algebraic_to_boardloc($alg).unwrap()
};
}
#[macro_export]
macro_rules! algebraic {
($boardloc:expr) => {
$crate::utils::BOARDLOC_TO_ALG[$boardloc]
}
}
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use crate::utils::{algebraic_to_boardloc, boardloc_to_algebraic}; use crate::utils::{algebraic_to_boardloc, boardloc_to_algebraic};