diff --git a/src/board.rs b/src/board.rs index 1811844..c947204 100644 --- a/src/board.rs +++ b/src/board.rs @@ -90,7 +90,41 @@ impl Board { let mut piecelist = Vec::new(); let mut bitfield = Boardfield::new(); + let mut rank = 0; + let mut file = 0; + // parse component 1: piece data + for char in components[0].chars() { + match char { + 'r' | 'n' | 'b' | 'q' | 'k' | 'p' | 'R' | 'N' | 'B' | 'Q' | 'K' | 'P' => { + piecelist.push(create_fen_piece(char, 8 - rank, file)?); + bitfield.set_pos(((8 - rank) + file * 8) - 1, create_bitfield_piece(char)?); + file += 1; + }, + _ if char.is_numeric() => { + let num = char.to_digit(10).unwrap() as usize; + + if !(1..=8).contains(&num) { + return Err(FENParseError::CannotSkipToOutsideOfBoard {got: num, which_is: num}) + } + + if file + num > 8 { + return Err(FENParseError::CannotSkipToOutsideOfBoard {got: num, which_is: file + num}) + } + + file += num; + }, + '/' => { + if rank == 8 { + return Err(FENParseError::CannotSkipToOutsideOfBoard { got: 1, which_is: file + 1 }) + } + + rank += 1; + file = 0; + } + _ => return Err(FENParseError::InvalidPieceCharacter {got: char}) + } + } Ok(Self { bitfield, @@ -133,14 +167,16 @@ fn create_bitfield_piece(c: char) -> Result { 'n' => Ok(PieceColor::Black as u8 | PieceType::Knight as u8), 'b' => Ok(PieceColor::Black as u8 | PieceType::Bishop as u8), 'q' => Ok(PieceColor::Black as u8 | PieceType::Queen as u8), - 'k' => Ok(PieceColor::Black as u8 | PieceType::Knight as u8), + 'k' => Ok(PieceColor::Black as u8 | PieceType::King as u8), 'p' => Ok(PieceColor::Black as u8 | PieceType::Pawn as u8), + 'R' => Ok(PieceColor::White as u8 | PieceType::Rook as u8), 'N' => Ok(PieceColor::White as u8 | PieceType::Knight as u8), 'B' => Ok(PieceColor::White as u8 | PieceType::Bishop as u8), 'Q' => Ok(PieceColor::White as u8 | PieceType::Queen as u8), - 'K' => Ok(PieceColor::White as u8 | PieceType::Knight as u8), + 'K' => Ok(PieceColor::White as u8 | PieceType::King as u8), 'P' => Ok(PieceColor::White as u8 | PieceType::Pawn as u8), + _ => Err(FENParseError::InvalidPieceCharacter { got: c }) } } diff --git a/src/boardfield.rs b/src/boardfield.rs index b1ca6d8..c26b0b0 100644 --- a/src/boardfield.rs +++ b/src/boardfield.rs @@ -101,7 +101,7 @@ impl BoardfieldOps for Boardfield { fn set_pos(&mut self, boardloc: usize, piece: u8) { if boardloc > 63 { - panic!("boardloc out of range"); + panic!("boardloc out of range {}", boardloc); } let field = self[boardloc / 2];