mirror of
https://github.com/szymon-jozef/checkers.git
synced 2026-10-27 19:20:44 +01:00
refactor: remove position from pawn
This commit is contained in:
@@ -46,7 +46,7 @@ impl Board {
|
|||||||
debug!("Placing new pawn at: {}, owned by: {}", position, player);
|
debug!("Placing new pawn at: {}, owned by: {}", position, player);
|
||||||
Field {
|
Field {
|
||||||
position,
|
position,
|
||||||
pawn: Some(Pawn::new(position, player)),
|
pawn: Some(Pawn::new(player)),
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
debug!("Not placing any pawn at: {}", position);
|
debug!("Not placing any pawn at: {}", position);
|
||||||
@@ -96,10 +96,8 @@ impl Board {
|
|||||||
// we can safely unwrap, because if there's not pawn function returns false earlier
|
// we can safely unwrap, because if there's not pawn function returns false earlier
|
||||||
let mut mowing_pawn: Pawn = self[from].pawn.take().unwrap();
|
let mut mowing_pawn: Pawn = self[from].pawn.take().unwrap();
|
||||||
|
|
||||||
if to.row == player.end_row || matches!(mowing_pawn.state, PawnState::Dame(_)) {
|
if to.row == player.end_row {
|
||||||
mowing_pawn.state = super::pawn::PawnState::Dame(to);
|
mowing_pawn.state = super::pawn::PawnState::Dame;
|
||||||
} else {
|
|
||||||
mowing_pawn.state = super::pawn::PawnState::Man(to);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
self[to.row][to.column].pawn = Some(mowing_pawn);
|
self[to.row][to.column].pawn = Some(mowing_pawn);
|
||||||
@@ -178,10 +176,7 @@ mod tests {
|
|||||||
assert!(test_board[row][column].pawn.is_some());
|
assert!(test_board[row][column].pawn.is_some());
|
||||||
// we unwrap, because some value has to be here, if it's not then something is wrong
|
// we unwrap, because some value has to be here, if it's not then something is wrong
|
||||||
// beside we check for that in the assert above
|
// beside we check for that in the assert above
|
||||||
assert!(
|
assert!(test_board[row][column].pawn.as_ref().unwrap().state == PawnState::Man);
|
||||||
test_board[row][column].pawn.as_ref().unwrap().state
|
|
||||||
== PawnState::Man(Position { row, column })
|
|
||||||
);
|
|
||||||
|
|
||||||
if row < 3 {
|
if row < 3 {
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
|
|||||||
@@ -4,8 +4,8 @@ use crate::logic::{player::Player, utils::Position};
|
|||||||
|
|
||||||
#[derive(PartialEq, Eq, Debug)]
|
#[derive(PartialEq, Eq, Debug)]
|
||||||
pub enum PawnState {
|
pub enum PawnState {
|
||||||
Man(Position),
|
Man,
|
||||||
Dame(Position),
|
Dame,
|
||||||
Captured,
|
Captured,
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -17,9 +17,9 @@ pub struct Pawn {
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl Pawn {
|
impl Pawn {
|
||||||
pub fn new(start_pos: Position, owner: &Player) -> Self {
|
pub fn new(owner: &Player) -> Self {
|
||||||
Self {
|
Self {
|
||||||
state: PawnState::Man(start_pos),
|
state: PawnState::Man,
|
||||||
owner: owner.id,
|
owner: owner.id,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user