mirror of
https://github.com/szymon-jozef/checkers.git
synced 2026-10-27 19:20:44 +01:00
feat: add capturing
This commit is contained in:
@@ -113,6 +113,8 @@ pub struct Board {
|
|||||||
is_my_turn: bool,
|
is_my_turn: bool,
|
||||||
|
|
||||||
available_captures: Option<Vec<CapturePath>>,
|
available_captures: Option<Vec<CapturePath>>,
|
||||||
|
current_deepness: usize,
|
||||||
|
current_capture_path: Vec<Position>,
|
||||||
choosen_capture: Option<CapturePath>,
|
choosen_capture: Option<CapturePath>,
|
||||||
|
|
||||||
available_moves: Option<Vec<MovePath>>,
|
available_moves: Option<Vec<MovePath>>,
|
||||||
@@ -141,6 +143,8 @@ impl Board {
|
|||||||
is_my_turn: false,
|
is_my_turn: false,
|
||||||
|
|
||||||
available_captures: None,
|
available_captures: None,
|
||||||
|
current_capture_path: vec![],
|
||||||
|
current_deepness: 0,
|
||||||
choosen_capture: None,
|
choosen_capture: None,
|
||||||
|
|
||||||
available_moves: None,
|
available_moves: None,
|
||||||
@@ -199,8 +203,64 @@ impl Board {
|
|||||||
|
|
||||||
BoardState::ChooseCapture(from) => {
|
BoardState::ChooseCapture(from) => {
|
||||||
if self.is_back_move_wanted_by_player() {
|
if self.is_back_move_wanted_by_player() {
|
||||||
|
debug!("Going back");
|
||||||
|
self.current_capture_path.clear();
|
||||||
|
self.current_deepness = 0;
|
||||||
self.state = BoardState::ChoosePawn;
|
self.state = BoardState::ChoosePawn;
|
||||||
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
let Some(available_captures) = &self.available_captures else {
|
||||||
|
return;
|
||||||
|
};
|
||||||
|
|
||||||
|
let possible_moves: Vec<_> = available_captures
|
||||||
|
.iter()
|
||||||
|
.filter_map(|capture| {
|
||||||
|
if capture.from == from
|
||||||
|
&& capture.steps.starts_with(&self.current_capture_path)
|
||||||
|
{
|
||||||
|
Some(&capture.steps)
|
||||||
|
} else {
|
||||||
|
None
|
||||||
|
}
|
||||||
|
})
|
||||||
|
.collect();
|
||||||
|
|
||||||
|
let current_highlight: Vec<_> = possible_moves
|
||||||
|
.iter()
|
||||||
|
.filter_map(|possible_move| possible_move.get(self.current_deepness))
|
||||||
|
.copied()
|
||||||
|
.collect();
|
||||||
|
|
||||||
|
if let Some(clicked_field) = &self.get_clicked_field().map(|field| field.pos) {
|
||||||
|
if possible_moves.iter().any(|possible_move| {
|
||||||
|
if let Some(valid_move) = possible_move.get(self.current_deepness) {
|
||||||
|
valid_move == clicked_field
|
||||||
|
} else {
|
||||||
|
false
|
||||||
|
}
|
||||||
|
}) {
|
||||||
|
self.current_capture_path.push(*clicked_field);
|
||||||
|
self.current_deepness += 1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if current_highlight.is_empty() {
|
||||||
|
debug!("Capture path complete!");
|
||||||
|
self.choosen_capture = available_captures
|
||||||
|
.iter()
|
||||||
|
.find(|capture| {
|
||||||
|
capture.from == from && capture.steps == self.current_capture_path
|
||||||
|
})
|
||||||
|
.cloned();
|
||||||
|
|
||||||
|
self.current_capture_path.clear();
|
||||||
|
self.current_deepness = 0;
|
||||||
|
self.state = BoardState::View;
|
||||||
|
}
|
||||||
|
|
||||||
|
self.highlight_capture_path(current_highlight);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -372,6 +432,12 @@ impl Board {
|
|||||||
self.is_my_turn = is_my_turn;
|
self.is_my_turn = is_my_turn;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn highlight_capture_path(&mut self, fields: Vec<Position>) {
|
||||||
|
for field in fields {
|
||||||
|
self.fields[field].color = CAPTURE_HIGHLITGHT_COLOR;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
fn highlight_current_moves(&mut self) {
|
fn highlight_current_moves(&mut self) {
|
||||||
let Some(available_moves) = &self.available_moves else {
|
let Some(available_moves) = &self.available_moves else {
|
||||||
return;
|
return;
|
||||||
|
|||||||
@@ -93,7 +93,9 @@ impl GameClient {
|
|||||||
GuiCommands::Unready => {
|
GuiCommands::Unready => {
|
||||||
client.revoke_readiness().await;
|
client.revoke_readiness().await;
|
||||||
}
|
}
|
||||||
GuiCommands::Capture(capture_path) => todo!(),
|
GuiCommands::Capture(capture_path) => {
|
||||||
|
client.send_capture(capture_path).await;
|
||||||
|
}
|
||||||
|
|
||||||
GuiCommands::Move(move_path) => {
|
GuiCommands::Move(move_path) => {
|
||||||
client
|
client
|
||||||
@@ -262,7 +264,6 @@ impl GameClient {
|
|||||||
}
|
}
|
||||||
|
|
||||||
ServerMessage::AvailableMoves { moves } => {
|
ServerMessage::AvailableMoves { moves } => {
|
||||||
// TODO! Implement this (no todo to test drawing board!!)
|
|
||||||
self.board.update_available_moves(moves);
|
self.board.update_available_moves(moves);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user