mirror of
https://github.com/szymon-jozef/checkers.git
synced 2026-10-27 19:20:44 +01:00
chore: add can_capture method for pawn
This commit is contained in:
@@ -2,7 +2,7 @@ use crate::logic::{
|
|||||||
field::Field,
|
field::Field,
|
||||||
pawn::{Pawn, PawnState},
|
pawn::{Pawn, PawnState},
|
||||||
player::Player,
|
player::Player,
|
||||||
utils::{DeltaPosition, Position},
|
utils::{CapturePath, DeltaPosition, Position},
|
||||||
};
|
};
|
||||||
use std::ops::{Index, IndexMut};
|
use std::ops::{Index, IndexMut};
|
||||||
|
|
||||||
@@ -145,6 +145,42 @@ impl Board {
|
|||||||
Some(available_moves)
|
Some(available_moves)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn can_capture(&self, from: Position, to: Position, player: &Player) -> bool {
|
||||||
|
let Some(capturing_pawn) = &self[from].pawn else {
|
||||||
|
return false;
|
||||||
|
};
|
||||||
|
|
||||||
|
if !self.is_player_owner_of_the_pawn(player, capturing_pawn) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
let Some(captured_pawn) = &self[to].pawn else {
|
||||||
|
return false;
|
||||||
|
};
|
||||||
|
|
||||||
|
if self.is_player_owner_of_the_pawn(player, captured_pawn) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
let delta_distance: DeltaPosition = to - from;
|
||||||
|
let player_direction = player
|
||||||
|
.vertical_direction
|
||||||
|
.expect("Player vertical_direction not set!");
|
||||||
|
delta_distance.row == player_direction.row
|
||||||
|
&& delta_distance.column.abs() == 1
|
||||||
|
&& to
|
||||||
|
.checked_add(&delta_distance)
|
||||||
|
.is_some_and(|pos| self.is_position_empty(pos))
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn get_available_captures(
|
||||||
|
&self,
|
||||||
|
pos: Position,
|
||||||
|
player: &Player,
|
||||||
|
) -> Option<Vec<CapturePath>> {
|
||||||
|
todo!();
|
||||||
|
}
|
||||||
|
|
||||||
pub fn get_player_pawns_positions(&self, player: &Player) -> Vec<Position> {
|
pub fn get_player_pawns_positions(&self, player: &Player) -> Vec<Position> {
|
||||||
self.board
|
self.board
|
||||||
.iter()
|
.iter()
|
||||||
|
|||||||
@@ -36,6 +36,17 @@ impl Position {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl Sub for Position {
|
||||||
|
type Output = DeltaPosition;
|
||||||
|
|
||||||
|
fn sub(self, rhs: Self) -> Self::Output {
|
||||||
|
DeltaPosition {
|
||||||
|
row: self.row as i8 - rhs.row as i8,
|
||||||
|
column: self.column as i8 - rhs.column as i8,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/// Vector for moving `Position`
|
/// Vector for moving `Position`
|
||||||
#[derive(Debug, Copy, Clone)]
|
#[derive(Debug, Copy, Clone)]
|
||||||
pub struct DeltaPosition {
|
pub struct DeltaPosition {
|
||||||
@@ -82,6 +93,10 @@ impl Mul<usize> for DeltaPosition {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub struct CapturePath {
|
||||||
|
pub steps: Vec<Position>,
|
||||||
|
}
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod tests {
|
mod tests {
|
||||||
use super::*;
|
use super::*;
|
||||||
|
|||||||
Reference in New Issue
Block a user