Skip to content

Commit

Permalink
Better History System
Browse files Browse the repository at this point in the history
Rewriting the history system to not rely on strings so it will be easier
to work with for other issues. Favors a struct instead of many string
conversions until the moves actually have to be displayed.

Signed-off-by: Nick Mello <[email protected]>
  • Loading branch information
nicholasmello committed Apr 20, 2024
1 parent 8a5258c commit 4b908a5
Show file tree
Hide file tree
Showing 9 changed files with 282 additions and 189 deletions.
237 changes: 158 additions & 79 deletions src/board.rs

Large diffs are not rendered by default.

8 changes: 4 additions & 4 deletions src/pieces/bishop.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use super::{Movable, PieceColor, PieceType, Position};
use super::{Movable, PieceColor, PieceMove, PieceType, Position};
use crate::board::DisplayMode;
use crate::utils::{
cleaned_positions, get_piece_color, impossible_positions_king_checked, is_cell_color_ally,
Expand All @@ -12,7 +12,7 @@ impl Movable for Bishop {
color: PieceColor,
board: [[Option<(PieceType, PieceColor)>; 8]; 8],
allow_move_on_ally_positions: bool,
_move_history: &[(Option<PieceType>, String)],
_move_history: &[PieceMove],
) -> Vec<Vec<i8>> {
let mut positions: Vec<Vec<i8>> = vec![];

Expand Down Expand Up @@ -168,7 +168,7 @@ impl Position for Bishop {
coordinates: [i8; 2],
color: PieceColor,
board: [[Option<(PieceType, PieceColor)>; 8]; 8],
move_history: &[(Option<PieceType>, String)],
move_history: &[PieceMove],
_is_king_checked: bool,
) -> Vec<Vec<i8>> {
// if the king is checked we clean all the position not resolving the check
Expand All @@ -184,7 +184,7 @@ impl Position for Bishop {
coordinates: [i8; 2],
color: PieceColor,
board: [[Option<(PieceType, PieceColor)>; 8]; 8],
move_history: &[(Option<PieceType>, String)],
move_history: &[PieceMove],
) -> Vec<Vec<i8>> {
Self::piece_move(coordinates, color, board, true, move_history)
}
Expand Down
34 changes: 26 additions & 8 deletions src/pieces/king.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use super::{Movable, PieceColor, PieceType, Position};
use super::{Movable, PieceColor, PieceMove, PieceType, Position};
use crate::board::DisplayMode;
use crate::utils::{
cleaned_positions, did_piece_already_move, get_all_protected_cells, get_piece_type,
Expand All @@ -12,7 +12,7 @@ impl Movable for King {
color: PieceColor,
board: [[Option<(PieceType, PieceColor)>; 8]; 8],
allow_move_on_ally_positions: bool,
_move_history: &[(Option<PieceType>, String)],
_move_history: &[PieceMove],
) -> Vec<Vec<i8>> {
let mut positions: Vec<Vec<i8>> = vec![];
let y = coordinates[0];
Expand Down Expand Up @@ -45,7 +45,7 @@ impl Position for King {
coordinates: [i8; 2],
color: PieceColor,
board: [[Option<(PieceType, PieceColor)>; 8]; 8],
move_history: &[(Option<PieceType>, String)],
move_history: &[PieceMove],
is_king_checked: bool,
) -> Vec<Vec<i8>> {
let mut positions: Vec<Vec<i8>> = vec![];
Expand Down Expand Up @@ -97,7 +97,7 @@ impl Position for King {
coordinates: [i8; 2],
color: PieceColor,
board: [[Option<(PieceType, PieceColor)>; 8]; 8],
move_history: &[(Option<PieceType>, String)],
move_history: &[PieceMove],
) -> Vec<Vec<i8>> {
Self::piece_move(coordinates, color, board, true, move_history)
}
Expand Down Expand Up @@ -154,7 +154,7 @@ impl King {
mod tests {
use crate::{
board::Board,
pieces::{king::King, PieceColor, PieceType, Position},
pieces::{king::King, PieceColor, PieceMove, PieceType, Position},
utils::is_getting_checked,
};

Expand Down Expand Up @@ -652,9 +652,27 @@ mod tests {
PieceColor::Black,
board.board,
&[
(Some(PieceType::Rook), "0747".to_string()),
(Some(PieceType::Pawn), "6252".to_string()),
(Some(PieceType::Rook), "4707".to_string()),
(PieceMove {
piece_type: PieceType::Rook,
from_y: 0,
from_x: 7,
to_y: 4,
to_x: 7,
}),
(PieceMove {
piece_type: PieceType::Pawn,
from_y: 6,
from_x: 2,
to_y: 5,
to_x: 2,
}),
(PieceMove {
piece_type: PieceType::Rook,
from_y: 4,
from_x: 7,
to_y: 0,
to_x: 7,
}),
],
false,
);
Expand Down
8 changes: 4 additions & 4 deletions src/pieces/knight.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use super::{Movable, PieceColor, PieceType, Position};
use super::{Movable, PieceColor, PieceMove, PieceType, Position};
use crate::board::DisplayMode;
use crate::utils::{
cleaned_positions, impossible_positions_king_checked, is_cell_color_ally, is_valid,
Expand All @@ -11,7 +11,7 @@ impl Movable for Knight {
color: PieceColor,
board: [[Option<(PieceType, PieceColor)>; 8]; 8],
allow_move_on_ally_positions: bool,
_move_history: &[(Option<PieceType>, String)],
_move_history: &[PieceMove],
) -> Vec<Vec<i8>> {
let mut positions: Vec<Vec<i8>> = Vec::new();

Expand Down Expand Up @@ -52,7 +52,7 @@ impl Position for Knight {
coordinates: [i8; 2],
color: PieceColor,
board: [[Option<(PieceType, PieceColor)>; 8]; 8],
move_history: &[(Option<PieceType>, String)],
move_history: &[PieceMove],
_is_king_checked: bool,
) -> Vec<Vec<i8>> {
impossible_positions_king_checked(
Expand All @@ -68,7 +68,7 @@ impl Position for Knight {
coordinates: [i8; 2],
color: PieceColor,
board: [[Option<(PieceType, PieceColor)>; 8]; 8],
_move_history: &[(Option<PieceType>, String)],
_move_history: &[PieceMove],
) -> Vec<Vec<i8>> {
Self::piece_move(coordinates, color, board, true, _move_history)
}
Expand Down
45 changes: 27 additions & 18 deletions src/pieces/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ impl PieceType {
coordinates: [i8; 2],
color: PieceColor,
board: [[Option<(PieceType, PieceColor)>; 8]; 8],
move_history: &[(Option<PieceType>, String)],
move_history: &[PieceMove],
is_king_checked: bool,
) -> Vec<Vec<i8>> {
match self {
Expand Down Expand Up @@ -65,7 +65,7 @@ impl PieceType {
piece_type: PieceType,
color: PieceColor,
board: [[Option<(PieceType, PieceColor)>; 8]; 8],
move_history: &[(Option<PieceType>, String)],
move_history: &[PieceMove],
) -> Vec<Vec<i8>> {
match piece_type {
PieceType::Pawn => {
Expand All @@ -90,22 +90,22 @@ impl PieceType {
}

pub fn piece_to_utf_enum(
piece_type: Option<PieceType>,
piece_type: PieceType,
piece_color: Option<PieceColor>,
) -> &'static str {
match (piece_type, piece_color) {
(Some(PieceType::Queen), Some(PieceColor::Black)) => "β™•",
(Some(PieceType::Queen), Some(PieceColor::White)) => "β™›",
(Some(PieceType::King), Some(PieceColor::Black)) => "β™”",
(Some(PieceType::King), Some(PieceColor::White)) => "β™š",
(Some(PieceType::Rook), Some(PieceColor::Black)) => "β™–",
(Some(PieceType::Rook), Some(PieceColor::White)) => "β™œ",
(Some(PieceType::Bishop), Some(PieceColor::Black)) => "β™—",
(Some(PieceType::Bishop), Some(PieceColor::White)) => "♝",
(Some(PieceType::Knight), Some(PieceColor::Black)) => "β™˜",
(Some(PieceType::Knight), Some(PieceColor::White)) => "β™ž",
(Some(PieceType::Pawn), Some(PieceColor::Black)) => "β™™",
(Some(PieceType::Pawn), Some(PieceColor::White)) => "β™Ÿ",
(PieceType::Queen, Some(PieceColor::Black)) => "β™•",
(PieceType::Queen, Some(PieceColor::White)) => "β™›",
(PieceType::King, Some(PieceColor::Black)) => "β™”",
(PieceType::King, Some(PieceColor::White)) => "β™š",
(PieceType::Rook, Some(PieceColor::Black)) => "β™–",
(PieceType::Rook, Some(PieceColor::White)) => "β™œ",
(PieceType::Bishop, Some(PieceColor::Black)) => "β™—",
(PieceType::Bishop, Some(PieceColor::White)) => "♝",
(PieceType::Knight, Some(PieceColor::Black)) => "β™˜",
(PieceType::Knight, Some(PieceColor::White)) => "β™ž",
(PieceType::Pawn, Some(PieceColor::Black)) => "β™™",
(PieceType::Pawn, Some(PieceColor::White)) => "β™Ÿ",
_ => "NONE",
}
}
Expand Down Expand Up @@ -148,6 +148,15 @@ impl PieceType {
}
}

#[derive(Debug, Copy, Clone, PartialEq)]
pub struct PieceMove {
pub piece_type: PieceType,
pub from_x: i8,
pub from_y: i8,
pub to_x: i8,
pub to_y: i8,
}

#[derive(Debug, Copy, Clone, PartialEq)]
pub enum PieceColor {
Black,
Expand All @@ -160,7 +169,7 @@ pub trait Movable {
color: PieceColor,
board: [[Option<(PieceType, PieceColor)>; 8]; 8],
allow_move_on_ally_positions: bool,
move_history: &[(Option<PieceType>, String)],
move_history: &[PieceMove],
) -> Vec<Vec<i8>>;
}

Expand All @@ -169,14 +178,14 @@ pub trait Position {
coordinates: [i8; 2],
color: PieceColor,
board: [[Option<(PieceType, PieceColor)>; 8]; 8],
move_history: &[(Option<PieceType>, String)],
move_history: &[PieceMove],
is_king_checked: bool,
) -> Vec<Vec<i8>>;

fn protected_positions(
coordinates: [i8; 2],
color: PieceColor,
board: [[Option<(PieceType, PieceColor)>; 8]; 8],
move_history: &[(Option<PieceType>, String)],
move_history: &[PieceMove],
) -> Vec<Vec<i8>>;
}
Loading

0 comments on commit 4b908a5

Please sign in to comment.