[board] Give piece Color and shape enums integer values

Rename PieceShape → Shape
Remove color and piece modules from position.rs
This commit is contained in:
Eryn Wells 2023-12-23 20:14:24 -07:00
parent e4859105b7
commit a2f88f0fde
2 changed files with 18 additions and 60 deletions

View file

@ -1,19 +1,19 @@
// Eryn Wells <eryn@erynwells.me> // Eryn Wells <eryn@erynwells.me>
#[derive(Debug, Eq, PartialEq)] #[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub enum Color { pub enum Color {
White, White = 0,
Black, Black = 1,
} }
#[derive(Debug, Eq, PartialEq)] #[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub enum PieceShape { pub enum Shape {
Pawn, Pawn = 0,
Knight, Knight = 1,
Bishop, Bishop = 2,
Rook, Rook = 3,
Queen, Queen = 4,
King, King = 5,
} }
#[derive(Debug, Eq, PartialEq)] #[derive(Debug, Eq, PartialEq)]
@ -24,11 +24,11 @@ pub enum PiecePlacementError {
#[derive(Debug, Eq, PartialEq)] #[derive(Debug, Eq, PartialEq)]
pub struct Piece { pub struct Piece {
pub color: Color, pub color: Color,
pub piece: PieceShape, pub shape: Shape,
} }
impl Piece { impl Piece {
pub fn new(color: Color, piece: PieceShape) -> Piece { pub fn new(color: Color, shape: Shape) -> Piece {
Piece { color, piece } Piece { color, shape }
} }
} }

View file

@ -1,24 +1,10 @@
// Eryn Wells <eryn@erynwells.me> // Eryn Wells <eryn@erynwells.me>
use crate::bitboard::BitBoard; use crate::bitboard::BitBoard;
use crate::piece::{Color, Piece, PiecePlacementError, PieceShape}; use crate::piece::{Piece, PiecePlacementError};
use crate::square::Square; use crate::square::Square;
use std::fmt; use std::fmt;
mod color {
const WHITE: u8 = 0;
const BLACK: u8 = 1;
}
mod piece {
const PAWN: u8 = 0;
const KNIGHT: u8 = 1;
const BISHOP: u8 = 2;
const ROOK: u8 = 3;
const QUEEN: u8 = 4;
const KING: u8 = 5;
}
#[derive(Clone, Debug, Eq, Hash, PartialEq)] #[derive(Clone, Debug, Eq, Hash, PartialEq)]
pub struct Position { pub struct Position {
/// Composite bitboards for all the pieces of a particular color. /// Composite bitboards for all the pieces of a particular color.
@ -83,7 +69,7 @@ impl Position {
} }
fn place_piece(&mut self, piece: &Piece, square: &Square) -> Result<(), PiecePlacementError> { fn place_piece(&mut self, piece: &Piece, square: &Square) -> Result<(), PiecePlacementError> {
let mut bitboard = self.bitboard_for_piece_mut(piece); let bitboard = self.bitboard_for_piece_mut(piece);
if bitboard.has_piece_at(&square) { if bitboard.has_piece_at(&square) {
return Err(PiecePlacementError::PieceExistsOnSquare); return Err(PiecePlacementError::PieceExistsOnSquare);
@ -95,39 +81,11 @@ impl Position {
} }
fn bitboard_for_piece(&self, piece: &Piece) -> &BitBoard { fn bitboard_for_piece(&self, piece: &Piece) -> &BitBoard {
let color_index: usize = match piece.color { &self.pieces_per_type[piece.color as usize][piece.shape as usize]
Color::White => 0,
Color::Black => 1,
};
let piece_index: usize = match piece.piece {
PieceShape::Pawn => 0,
PieceShape::Knight => 1,
PieceShape::Bishop => 2,
PieceShape::Rook => 3,
PieceShape::Queen => 4,
PieceShape::King => 5,
};
&self.pieces_per_type[color_index][piece_index]
} }
fn bitboard_for_piece_mut(&mut self, piece: &Piece) -> &mut BitBoard { fn bitboard_for_piece_mut(&mut self, piece: &Piece) -> &mut BitBoard {
let color_index: usize = match piece.color { &mut self.pieces_per_type[piece.color as usize][piece.shape as usize]
Color::White => 0,
Color::Black => 1,
};
let piece_index: usize = match piece.piece {
PieceShape::Pawn => 0,
PieceShape::Knight => 1,
PieceShape::Bishop => 2,
PieceShape::Rook => 3,
PieceShape::Queen => 4,
PieceShape::King => 5,
};
&mut self.pieces_per_type[color_index][piece_index]
} }
} }