Refactor Piece
This commit is contained in:
@@ -1,9 +1,29 @@
|
||||
use std::marker::PhantomData;
|
||||
enum Color {
|
||||
Black,
|
||||
White,
|
||||
}
|
||||
|
||||
impl Color {
|
||||
fn get_home_row(&self) -> Row {
|
||||
match self {
|
||||
Color::Black => Row::_8,
|
||||
Color::White => Row::_1,
|
||||
}
|
||||
}
|
||||
fn get_pawn_row(&self) -> Row {
|
||||
match self {
|
||||
Color::Black => Row::_7,
|
||||
Color::White => Row::_2,
|
||||
}
|
||||
}
|
||||
fn get_direction(&self) -> i8 {
|
||||
match self {
|
||||
Color::Black => -1,
|
||||
Color::White => 1,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
struct Position {
|
||||
row: Row,
|
||||
column: Column,
|
||||
@@ -108,10 +128,14 @@ impl GridAxis for Column {
|
||||
}
|
||||
}
|
||||
|
||||
struct Piece<T> {
|
||||
enum PieceType {
|
||||
Pawn,
|
||||
}
|
||||
|
||||
struct Piece {
|
||||
position: Position,
|
||||
color: Color,
|
||||
piece_type: PhantomData<T>,
|
||||
piece_type: PieceType,
|
||||
}
|
||||
|
||||
trait Movement {
|
||||
@@ -119,9 +143,19 @@ trait Movement {
|
||||
fn get_captures(&self) -> Vec<Position>;
|
||||
}
|
||||
|
||||
struct Pawn {}
|
||||
|
||||
impl<T> Piece<T> {
|
||||
impl Piece {
|
||||
fn get_moves(&self) -> Vec<Position> {
|
||||
let deltas = match self.piece_type {
|
||||
PieceType::Pawn => self._pawn_get_move_deltas(),
|
||||
};
|
||||
self.deltas_to_valid_positions(&deltas)
|
||||
}
|
||||
fn get_captures(&self) -> Vec<Position> {
|
||||
let deltas = match self.piece_type {
|
||||
PieceType::Pawn => self._pawn_get_capture_deltas(),
|
||||
};
|
||||
self.deltas_to_valid_positions(&deltas)
|
||||
}
|
||||
fn deltas_to_valid_positions(
|
||||
&self,
|
||||
deltas: &Vec<(i8, i8)>,
|
||||
@@ -133,32 +167,17 @@ impl<T> Piece<T> {
|
||||
})
|
||||
.collect()
|
||||
}
|
||||
}
|
||||
|
||||
impl Piece<Pawn> {
|
||||
fn get_home_row_direction(&self) -> (Row, i8) {
|
||||
match self.color {
|
||||
Color::Black => (Row::_7, -1),
|
||||
Color::White => (Row::_2, 1),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Movement for Piece<Pawn> {
|
||||
fn get_moves(&self) -> Vec<Position> {
|
||||
let (home_row, direction) = self.get_home_row_direction();
|
||||
let deltas: Vec<(i8, i8)> = if self.position.row == home_row {
|
||||
fn _pawn_get_move_deltas(&self) -> Vec<(i8, i8)> {
|
||||
let direction = self.color.get_direction();
|
||||
if self.position.row == self.color.get_pawn_row() {
|
||||
vec![(1 * direction, 0), (2 * direction, 0)]
|
||||
} else {
|
||||
vec![(1 * direction, 0)]
|
||||
};
|
||||
self.deltas_to_valid_positions(&deltas)
|
||||
}
|
||||
}
|
||||
fn get_captures(&self) -> Vec<Position> {
|
||||
let (_, direction) = self.get_home_row_direction();
|
||||
let deltas: Vec<(i8, i8)> =
|
||||
vec![(1 * direction, 1), (1 * direction, -1)];
|
||||
self.deltas_to_valid_positions(&deltas)
|
||||
fn _pawn_get_capture_deltas(&self) -> Vec<(i8, i8)> {
|
||||
let direction = self.color.get_direction();
|
||||
vec![(1 * direction, 1), (1 * direction, -1)]
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user