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