Skip to content

Commit

Permalink
feat(pieces): nailing working
Browse files Browse the repository at this point in the history
Signed-off-by: Thomas Mauran <thomasmauran@yahoo.com>
  • Loading branch information
thomas-mauran committed Dec 1, 2023
1 parent bc79ccb commit 663d4f2
Show file tree
Hide file tree
Showing 8 changed files with 371 additions and 118 deletions.
2 changes: 0 additions & 2 deletions demo.tape
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@ Require echo
Set Margin 100
Set MarginFill "#674EBB"

Set Theme

Set BorderRadius 10
Set Shell zsh

Expand Down
57 changes: 20 additions & 37 deletions src/board.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,58 +33,40 @@ impl Default for Board {
Self {
board: [
[
Some((PieceType::Rook, PieceColor::Black)),
Some((PieceType::Knight, PieceColor::Black)),
Some((PieceType::Bishop, PieceColor::Black)),
Some((PieceType::Queen, PieceColor::Black)),
None,
None,
None,
None,
Some((PieceType::King, PieceColor::Black)),
Some((PieceType::Bishop, PieceColor::Black)),
Some((PieceType::Knight, PieceColor::Black)),
Some((PieceType::Rook, PieceColor::Black)),
None,
None,
None,
],
[
Some((PieceType::Pawn, PieceColor::Black)),
Some((PieceType::Pawn, PieceColor::Black)),
Some((PieceType::Pawn, PieceColor::Black)),
Some((PieceType::Pawn, PieceColor::Black)),
Some((PieceType::Pawn, PieceColor::Black)),
Some((PieceType::Pawn, PieceColor::Black)),
Some((PieceType::Pawn, PieceColor::Black)),
Some((PieceType::Pawn, PieceColor::Black)),
None,
None,
None,
None,
Some((PieceType::Knight, PieceColor::Black)),
None,
None,
None,
],
[None, None, None, None, None, None, None, None],
[
None,
None,
None,
None,
None,
Some((PieceType::Queen, PieceColor::White)),
None,
None,
None,
],
[None, None, None, None, None, None, None, None],
[None, None, None, None, None, None, None, None],
[None, None, None, None, None, None, None, None],
[
Some((PieceType::Pawn, PieceColor::White)),
Some((PieceType::Pawn, PieceColor::White)),
Some((PieceType::Pawn, PieceColor::White)),
Some((PieceType::Pawn, PieceColor::White)),
Some((PieceType::Pawn, PieceColor::White)),
Some((PieceType::Pawn, PieceColor::White)),
Some((PieceType::Pawn, PieceColor::White)),
Some((PieceType::Pawn, PieceColor::White)),
],
[
Some((PieceType::Rook, PieceColor::White)),
Some((PieceType::Knight, PieceColor::White)),
Some((PieceType::Bishop, PieceColor::White)),
Some((PieceType::Queen, PieceColor::White)),
Some((PieceType::King, PieceColor::White)),
Some((PieceType::Bishop, PieceColor::White)),
Some((PieceType::Knight, PieceColor::White)),
Some((PieceType::King, PieceColor::White)),
],
[None, None, None, None, None, None, None, None],
],
cursor_coordinates: [4, 4],
selected_coordinates: [UNDEFINED_POSITION, UNDEFINED_POSITION],
Expand Down Expand Up @@ -112,6 +94,7 @@ impl Board {
moves_history,
}
}

// Setters
pub fn set_board(&mut self, board: [[Option<(PieceType, PieceColor)>; 8]; 8]) {
self.board = board;
Expand Down Expand Up @@ -342,7 +325,7 @@ impl Board {
self.board[from[0]][from[1]] = None;

// We store it in the history
self.moves_history.push(tuple);
self.moves_history.push(tuple.clone());
}

pub fn unselect_cell(&mut self) {
Expand Down
85 changes: 70 additions & 15 deletions src/pieces/bishop.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use super::{Movable, PieceColor, PieceType, Position};
use crate::utils::{
cleaned_positions, get_piece_color, impossible_positions_when_king_checked, is_cell_color_ally,
cleaned_positions, get_piece_color, impossible_positions_king_checked, is_cell_color_ally,
is_piece_opposite_king, is_valid,
};
pub struct Bishop;
Expand Down Expand Up @@ -168,21 +168,16 @@ impl Position for Bishop {
color: PieceColor,
board: [[Option<(PieceType, PieceColor)>; 8]; 8],
move_history: Vec<(Option<PieceType>, String)>,
is_king_checked: bool,
_is_king_checked: bool,
) -> Vec<Vec<i8>> {
// If the king is not checked we get then normal moves
if !is_king_checked {
Self::piece_move(coordinates, color, board, false, move_history)
} else {
// if the king is checked we clean all the position not resolving the check
impossible_positions_when_king_checked(
coordinates,
Self::piece_move(coordinates, color, board, false, move_history.clone()),
board,
color,
move_history,
)
}
// if the king is checked we clean all the position not resolving the check
impossible_positions_king_checked(
coordinates,
Self::piece_move(coordinates, color, board, false, move_history.clone()),
board,
color,
move_history,
)
}
fn protected_positions(
coordinates: [i8; 2],
Expand Down Expand Up @@ -505,4 +500,64 @@ mod tests {

assert_eq!(right_positions, positions);
}

#[test]
fn nailing() {
let custom_board = [
[
None,
None,
None,
None,
Some((PieceType::King, PieceColor::Black)),
None,
None,
None,
],
[
None,
None,
None,
None,
None,
Some((PieceType::Bishop, PieceColor::Black)),
None,
None,
],
[None, None, None, None, None, None, None, None],
[
None,
None,
None,
None,
None,
None,
None,
Some((PieceType::Queen, PieceColor::White)),
],
[None, None, None, None, None, None, None, None],
[None, None, None, None, None, None, None, None],
[None, None, None, None, None, None, None, None],
[None, None, None, None, None, None, None, None],
];
let mut board = Board::new(custom_board, PieceColor::Black, vec![]);
board.set_board(custom_board);

let is_king_checked =
is_getting_checked(board.board, board.player_turn, board.moves_history);

let mut right_positions: Vec<Vec<i8>> = vec![vec![2, 6], vec![3, 7]];
right_positions.sort();

let mut positions = Bishop::authorized_positions(
[1, 5],
PieceColor::Black,
board.board,
vec![],
is_king_checked,
);
positions.sort();

assert_eq!(right_positions, positions);
}
}
85 changes: 69 additions & 16 deletions src/pieces/knight.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use super::{Movable, PieceColor, PieceType, Position};
use crate::utils::{
cleaned_positions, impossible_positions_when_king_checked, is_cell_color_ally, is_valid,
cleaned_positions, impossible_positions_king_checked, is_cell_color_ally, is_valid,
};
pub struct Knight;

Expand Down Expand Up @@ -52,21 +52,15 @@ impl Position for Knight {
color: PieceColor,
board: [[Option<(PieceType, PieceColor)>; 8]; 8],
move_history: Vec<(Option<PieceType>, String)>,
is_king_checked: bool,
_is_king_checked: bool,
) -> Vec<Vec<i8>> {
// If the king is not checked we get then normal moves
if !is_king_checked {
Self::piece_move(coordinates, color, board, false, move_history)
} else {
// if the king is checked we clean all the position not resolving the check
impossible_positions_when_king_checked(
coordinates,
Self::piece_move(coordinates, color, board, false, move_history.clone()),
board,
color,
move_history,
)
}
impossible_positions_king_checked(
coordinates,
Self::piece_move(coordinates, color, board, false, move_history.clone()),
board,
color,
move_history,
)
}

fn protected_positions(
Expand Down Expand Up @@ -95,7 +89,7 @@ impl Knight {
mod tests {
use crate::{
board::Board,
pieces::{knight::Knight, PieceColor, PieceType, Position},
pieces::{bishop::Bishop, knight::Knight, PieceColor, PieceType, Position},
utils::is_getting_checked,
};

Expand Down Expand Up @@ -311,6 +305,65 @@ mod tests {
);
positions.sort();

assert_eq!(right_positions, positions);
}
#[test]
fn nailing() {
let custom_board = [
[
None,
None,
None,
None,
Some((PieceType::King, PieceColor::Black)),
None,
None,
None,
],
[
None,
None,
None,
None,
Some((PieceType::Knight, PieceColor::Black)),
None,
None,
None,
],
[None, None, None, None, None, None, None, None],
[
None,
None,
None,
None,
Some((PieceType::Queen, PieceColor::White)),
None,
None,
None,
],
[None, None, None, None, None, None, None, None],
[None, None, None, None, None, None, None, None],
[None, None, None, None, None, None, None, None],
[None, None, None, None, None, None, None, None],
];
let mut board = Board::new(custom_board, PieceColor::Black, vec![]);
board.set_board(custom_board);

let is_king_checked =
is_getting_checked(board.board, board.player_turn, board.moves_history);

let mut right_positions: Vec<Vec<i8>> = vec![];
right_positions.sort();

let mut positions = Knight::authorized_positions(
[1, 4],
PieceColor::Black,
board.board,
vec![],
is_king_checked,
);
positions.sort();

assert_eq!(right_positions, positions);
}
}
Loading

0 comments on commit 663d4f2

Please sign in to comment.