add per piece moves
This commit is contained in:
parent
bfbdc52e04
commit
716d731cc3
@ -1,3 +1,5 @@
|
|||||||
|
// TODO: remove this pub later
|
||||||
pub mod position;
|
pub mod position;
|
||||||
pub mod piece;
|
mod piece;
|
||||||
|
mod moves;
|
||||||
mod square;
|
mod square;
|
||||||
|
|||||||
150
src/chess/moves.rs
Normal file
150
src/chess/moves.rs
Normal file
@ -0,0 +1,150 @@
|
|||||||
|
use super::{piece::PieceVariant, square::Square};
|
||||||
|
|
||||||
|
pub struct Move {
|
||||||
|
from: Square,
|
||||||
|
to: Square,
|
||||||
|
promotion: Option<PieceVariant>
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Move {
|
||||||
|
pub fn from(self) -> Square {
|
||||||
|
self.from
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn to(self) -> Square {
|
||||||
|
self.to
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn promotion(self) -> Option<PieceVariant> {
|
||||||
|
self.promotion
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub struct Change {
|
||||||
|
x: i8,
|
||||||
|
y: i8
|
||||||
|
}
|
||||||
|
|
||||||
|
pub enum MoveVariant {
|
||||||
|
King([Change; 8]),
|
||||||
|
KingCastle([Change; 2]),
|
||||||
|
Queen([Change; 8]),
|
||||||
|
Rook([Change; 4]),
|
||||||
|
Bishop([Change; 4]),
|
||||||
|
Knight([Change; 8]),
|
||||||
|
PawnWhite([Change; 1]),
|
||||||
|
PawnWhiteDouble([Change; 1]),
|
||||||
|
PawnWhiteEnPassent([Change; 2]),
|
||||||
|
PawnWhiteCaptureLeft([Change; 1]),
|
||||||
|
PawnWhiteCaptureRight([Change; 1]),
|
||||||
|
PawnBlack([Change; 1]),
|
||||||
|
PawnBlackDouble([Change; 1]),
|
||||||
|
PawnBlackEnPassent([Change; 2]),
|
||||||
|
PawnBlackCaptureLeft([Change; 1]),
|
||||||
|
PawnBlackCaptureRight([Change; 1]),
|
||||||
|
}
|
||||||
|
|
||||||
|
impl MoveVariant {
|
||||||
|
pub fn king() -> [Change; 8] {
|
||||||
|
[
|
||||||
|
Change { x: 0, y: 1 },
|
||||||
|
Change { x: 0, y: -1 },
|
||||||
|
Change { x: 1, y: 0 },
|
||||||
|
Change { x: 1, y: 1 },
|
||||||
|
Change { x: 1, y: -1 },
|
||||||
|
Change { x: -1, y: 0 },
|
||||||
|
Change { x: -1, y: 1 },
|
||||||
|
Change { x: -1, y: -1 },
|
||||||
|
]
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn queen() -> [Change; 8] {
|
||||||
|
[
|
||||||
|
Change { x: 0, y: 1 },
|
||||||
|
Change { x: 0, y: -1 },
|
||||||
|
Change { x: 1, y: 0 },
|
||||||
|
Change { x: 1, y: 1 },
|
||||||
|
Change { x: 1, y: -1 },
|
||||||
|
Change { x: -1, y: 0 },
|
||||||
|
Change { x: -1, y: 1 },
|
||||||
|
Change { x: -1, y: -1 },
|
||||||
|
]
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn rook() -> [Change; 4] {
|
||||||
|
[
|
||||||
|
Change { x: 0, y: -1 },
|
||||||
|
Change { x: 0, y: 1 },
|
||||||
|
Change { x: 1, y: 0 },
|
||||||
|
Change { x: -1, y: 0 }
|
||||||
|
]
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn bishop() -> [Change; 4] {
|
||||||
|
[
|
||||||
|
Change { x: -1, y: -1 },
|
||||||
|
Change { x: -1, y: 1 },
|
||||||
|
Change { x: 1, y: -1 },
|
||||||
|
Change { x: 1, y: 1 }
|
||||||
|
]
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn knight() -> [Change; 8] {
|
||||||
|
[
|
||||||
|
Change { x: 1, y: 2 },
|
||||||
|
Change { x: 2, y: 1 },
|
||||||
|
Change { x: 2, y: -1 },
|
||||||
|
Change { x: 1, y: -2 },
|
||||||
|
Change { x: -1, y: -2 },
|
||||||
|
Change { x: -2, y: -1},
|
||||||
|
Change { x: -2, y: 1},
|
||||||
|
Change { x: -1, y: 2},
|
||||||
|
]
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn pawn_white() -> [Change; 1] {
|
||||||
|
[ Change { x: 0, y: -1 } ]
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn pawn_white_double() -> [Change; 1] {
|
||||||
|
[ Change { x: 0, y: -2 } ]
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn pawn_white_en_passent() -> [Change; 2] {
|
||||||
|
[
|
||||||
|
Change { x: -1, y: -1 },
|
||||||
|
Change { x: 1, y: -1 },
|
||||||
|
]
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn pawn_white_capture_left() -> [Change; 1] {
|
||||||
|
[ Change { x: -1, y: -1 } ]
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn pawn_white_capture_right() -> [Change; 1] {
|
||||||
|
[ Change { x: 1, y: -1 } ]
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn pawn_black() -> [Change; 1] {
|
||||||
|
[ Change { x: 0, y: 1 } ]
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn pawn_black_double() -> [Change; 1] {
|
||||||
|
[ Change { x: 0, y: 2 } ]
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn pawn_black_en_passent() -> [Change; 2] {
|
||||||
|
[
|
||||||
|
Change { x: -1, y: 1 },
|
||||||
|
Change { x: 1, y: 1 },
|
||||||
|
]
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn pawn_black_capture_left() -> [Change; 1] {
|
||||||
|
[ Change { x: 1, y: 1 } ]
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn pawn_black_capture_right() -> [Change; 1] {
|
||||||
|
[ Change { x: -1, y: 1 } ]
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -1,19 +1,43 @@
|
|||||||
use crate::chess::piece::Piece;
|
use crate::chess::piece::Piece;
|
||||||
use crate::chess::square::Square;
|
use crate::chess::square::Square;
|
||||||
|
use crate::chess::moves::Move;
|
||||||
|
use crate::chess::moves::MoveVariant;
|
||||||
|
|
||||||
|
|
||||||
pub struct Castling {
|
struct Castling {
|
||||||
pub king_side: bool,
|
king_side: bool,
|
||||||
pub queen_side: bool,
|
queen_side: bool,
|
||||||
}
|
}
|
||||||
|
|
||||||
pub struct Position {
|
pub struct Position {
|
||||||
pub rows: [[Option<Piece>; 8]; 8],
|
rows: [[Option<Piece>; 8]; 8],
|
||||||
pub whites_turn: bool,
|
whites_turn: bool,
|
||||||
pub castling: [Castling; 2],
|
castling: [Castling; 2],
|
||||||
pub en_pessant: Option<Square>
|
en_pessant: Option<Square>
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/// |-----------------------------------------------|
|
||||||
|
/// | 0;0 | 1;0 | 2;0 | 3;0 | 4;0 | 5;0 | 6;0 | 7;0 |
|
||||||
|
/// |-----------------------------------------------|
|
||||||
|
/// | 0;1 | 1;1 | 2;1 | 3;1 | 4;1 | 5;1 | 6;1 | 7;1 | Black
|
||||||
|
/// |-----------------------------------------------|
|
||||||
|
/// | 0;2 | 1;2 | 2;2 | 3;2 | 4;2 | 5;2 | 6;2 | 7;2 |
|
||||||
|
/// |-----------------------------------------------|
|
||||||
|
/// | 0;3 | 1;3 | 2;3 | 3;3 | 4;3 | 5;3 | 6;3 | 7;3 |
|
||||||
|
/// |-----------------------------------------------|
|
||||||
|
/// | 0;4 | 1;4 | 2;4 | 3;4 | 4;4 | 5;4 | 6;4 | 7;4 |
|
||||||
|
/// |-----------------------------------------------|
|
||||||
|
/// | 0;5 | 1;5 | 2;5 | 3;5 | 4;5 | 5;5 | 6;5 | 7;5 |
|
||||||
|
/// |-----------------------------------------------|
|
||||||
|
/// | 0;6 | 1;6 | 2;6 | 3;6 | 4;6 | 5;6 | 6;6 | 7;6 | White
|
||||||
|
/// |-----------------------------------------------|
|
||||||
|
/// | 0;7 | 1;7 | 2;7 | 3;7 | 4;7 | 5;7 | 6;7 | 7;7 |
|
||||||
|
/// |-----------------------------------------------|
|
||||||
|
/// Chess board repesented in code
|
||||||
|
///
|
||||||
|
/// NOTE: capture left/right is implemented relative to color
|
||||||
|
|
||||||
impl Default for Position {
|
impl Default for Position {
|
||||||
fn default() -> Self {
|
fn default() -> Self {
|
||||||
Position {
|
Position {
|
||||||
@ -46,3 +70,28 @@ impl Position {
|
|||||||
todo!("Implement this function")
|
todo!("Implement this function")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl Position {
|
||||||
|
pub fn rows(self) -> [[Option<Piece>; 8]; 8] {
|
||||||
|
self.rows
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn whites_turn(self) -> bool {
|
||||||
|
self.whites_turn
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn castling(self) -> [Castling; 2] {
|
||||||
|
self.castling
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn en_passent(self) -> Option<Square> {
|
||||||
|
self.en_pessant
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Position {
|
||||||
|
pub fn get_moves(self) -> Vec<Move> {
|
||||||
|
let king_moves = MoveVariant::king();
|
||||||
|
todo!("finish")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user