[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:
parent
e4859105b7
commit
a2f88f0fde
2 changed files with 18 additions and 60 deletions
|
@ -1,19 +1,19 @@
|
|||
// Eryn Wells <eryn@erynwells.me>
|
||||
|
||||
#[derive(Debug, Eq, PartialEq)]
|
||||
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
|
||||
pub enum Color {
|
||||
White,
|
||||
Black,
|
||||
White = 0,
|
||||
Black = 1,
|
||||
}
|
||||
|
||||
#[derive(Debug, Eq, PartialEq)]
|
||||
pub enum PieceShape {
|
||||
Pawn,
|
||||
Knight,
|
||||
Bishop,
|
||||
Rook,
|
||||
Queen,
|
||||
King,
|
||||
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
|
||||
pub enum Shape {
|
||||
Pawn = 0,
|
||||
Knight = 1,
|
||||
Bishop = 2,
|
||||
Rook = 3,
|
||||
Queen = 4,
|
||||
King = 5,
|
||||
}
|
||||
|
||||
#[derive(Debug, Eq, PartialEq)]
|
||||
|
@ -24,11 +24,11 @@ pub enum PiecePlacementError {
|
|||
#[derive(Debug, Eq, PartialEq)]
|
||||
pub struct Piece {
|
||||
pub color: Color,
|
||||
pub piece: PieceShape,
|
||||
pub shape: Shape,
|
||||
}
|
||||
|
||||
impl Piece {
|
||||
pub fn new(color: Color, piece: PieceShape) -> Piece {
|
||||
Piece { color, piece }
|
||||
pub fn new(color: Color, shape: Shape) -> Piece {
|
||||
Piece { color, shape }
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,24 +1,10 @@
|
|||
// Eryn Wells <eryn@erynwells.me>
|
||||
|
||||
use crate::bitboard::BitBoard;
|
||||
use crate::piece::{Color, Piece, PiecePlacementError, PieceShape};
|
||||
use crate::piece::{Piece, PiecePlacementError};
|
||||
use crate::square::Square;
|
||||
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)]
|
||||
pub struct Position {
|
||||
/// 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> {
|
||||
let mut bitboard = self.bitboard_for_piece_mut(piece);
|
||||
let bitboard = self.bitboard_for_piece_mut(piece);
|
||||
|
||||
if bitboard.has_piece_at(&square) {
|
||||
return Err(PiecePlacementError::PieceExistsOnSquare);
|
||||
|
@ -95,39 +81,11 @@ impl Position {
|
|||
}
|
||||
|
||||
fn bitboard_for_piece(&self, piece: &Piece) -> &BitBoard {
|
||||
let color_index: usize = match piece.color {
|
||||
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]
|
||||
&self.pieces_per_type[piece.color as usize][piece.shape as usize]
|
||||
}
|
||||
|
||||
fn bitboard_for_piece_mut(&mut self, piece: &Piece) -> &mut BitBoard {
|
||||
let color_index: usize = match piece.color {
|
||||
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]
|
||||
&mut self.pieces_per_type[piece.color as usize][piece.shape as usize]
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue