Implement Knight
This commit is contained in:
@@ -4,8 +4,10 @@ import subprocess
|
||||
import flask
|
||||
from flask_cors import CORS
|
||||
|
||||
|
||||
PIECE_TYPE = [("P", "pawn")]
|
||||
PIECE_TYPE = [
|
||||
("P", "pawn"),
|
||||
("N", "knight"),
|
||||
]
|
||||
COLOR = [
|
||||
("W", "white"),
|
||||
("B", "black"),
|
||||
|
||||
@@ -154,9 +154,11 @@ impl GridAxis for File {
|
||||
#[derive(Clone)]
|
||||
pub enum PieceType {
|
||||
Pawn,
|
||||
Knight,
|
||||
}
|
||||
|
||||
impl PieceType {
|
||||
// Pawn
|
||||
fn new_pawn(color: Color, file: File) -> (Piece, Position) {
|
||||
let pos = Position::new(file, color.get_pawn_rank());
|
||||
(
|
||||
@@ -207,6 +209,36 @@ impl PieceType {
|
||||
}
|
||||
capture_rays
|
||||
}
|
||||
|
||||
// Knight
|
||||
fn new_knight(color: Color, file: File) -> (Piece, Position) {
|
||||
let pos = Position::new(file, color.get_home_rank());
|
||||
(
|
||||
Piece {
|
||||
piece_type: PieceType::Knight,
|
||||
color,
|
||||
},
|
||||
pos,
|
||||
)
|
||||
}
|
||||
fn knight_rays(position: &Position) -> Vec<Ray> {
|
||||
[
|
||||
&[&Dir::Up, &Dir::Up, &Dir::Left],
|
||||
&[&Dir::Up, &Dir::Up, &Dir::Right],
|
||||
&[&Dir::Right, &Dir::Right, &Dir::Up],
|
||||
&[&Dir::Right, &Dir::Right, &Dir::Down],
|
||||
&[&Dir::Down, &Dir::Down, &Dir::Right],
|
||||
&[&Dir::Down, &Dir::Down, &Dir::Left],
|
||||
&[&Dir::Left, &Dir::Left, &Dir::Down],
|
||||
&[&Dir::Left, &Dir::Left, &Dir::Up],
|
||||
]
|
||||
.into_iter()
|
||||
.filter_map(|dirs| position.chain(dirs))
|
||||
.map(|p| Ray {
|
||||
positions: vec![p],
|
||||
})
|
||||
.collect()
|
||||
}
|
||||
fn initial_setup(&self) -> Vec<(Piece, Position)> {
|
||||
match &self {
|
||||
PieceType::Pawn => vec![
|
||||
@@ -227,6 +259,12 @@ impl PieceType {
|
||||
PieceType::new_pawn(Color::Black, File::G),
|
||||
PieceType::new_pawn(Color::Black, File::H),
|
||||
],
|
||||
PieceType::Knight => vec![
|
||||
PieceType::new_knight(Color::White, File::B),
|
||||
PieceType::new_knight(Color::White, File::G),
|
||||
PieceType::new_knight(Color::Black, File::B),
|
||||
PieceType::new_knight(Color::Black, File::G),
|
||||
],
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -246,6 +284,7 @@ impl Piece {
|
||||
PieceType::Pawn => {
|
||||
PieceType::pawn_move_rays(position, &self.color)
|
||||
}
|
||||
PieceType::Knight => PieceType::knight_rays(position),
|
||||
}
|
||||
}
|
||||
fn get_capture_rays(&self, position: &Position) -> Vec<Ray> {
|
||||
@@ -253,6 +292,7 @@ impl Piece {
|
||||
PieceType::Pawn => {
|
||||
PieceType::pawn_capture_rays(position, &self.color)
|
||||
}
|
||||
PieceType::Knight => PieceType::knight_rays(position),
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -294,7 +334,7 @@ impl Board {
|
||||
}
|
||||
pub fn reset(&mut self) {
|
||||
self.state.clear();
|
||||
for piece_type in &[PieceType::Pawn] {
|
||||
for piece_type in &[PieceType::Pawn, PieceType::Knight] {
|
||||
for (piece, position) in piece_type.initial_setup() {
|
||||
self.set_at(position, Some(piece));
|
||||
}
|
||||
|
||||
@@ -16,6 +16,7 @@ impl std::fmt::Display for board::PieceType {
|
||||
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
|
||||
match self {
|
||||
board::PieceType::Pawn => write!(f, "P"),
|
||||
board::PieceType::Knight => write!(f, "N"),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user