fail miserably at parsing fen (i have failed in the same way like 7! times now)
This commit is contained in:
parent
530d316dc1
commit
73be31dd27
40
src/board.rs
40
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<u8, FENParseError> {
|
|||
'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 })
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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];
|
||||
|
|
Loading…
Reference in New Issue