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