Skip to content

Commit

Permalink
fix: back to the roots, make tests pass
Browse files Browse the repository at this point in the history
  • Loading branch information
Jeromos Kovacs committed Jun 4, 2024
1 parent 068fbaa commit d127e50
Show file tree
Hide file tree
Showing 8 changed files with 109 additions and 147 deletions.
87 changes: 33 additions & 54 deletions src/board.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use crate::{
utils::{
col_to_letter, convert_notation_into_position, convert_position_into_notation,
did_piece_already_move, get_cell_paragraph, get_int_from_char, get_king_coordinates,
get_piece_color, get_piece_type, get_player_turn_in_modulo, is_getting_checked,
get_piece_color, get_piece_type, get_player_turn_in_modulo, is_getting_checked, is_valid,
},
};
use ratatui::{
Expand All @@ -19,33 +19,17 @@ use uci::Engine;
#[derive(PartialEq, Clone, Debug, Eq, PartialOrd, Ord)]
pub struct Coord {
/// row, line, y
pub row: u8,
pub row: i8,
/// column, x
pub col: u8,
pub col: i8,
}
impl Coord {
pub fn new<T: Into<u8>>(row: T, col: T) -> Self {
pub fn new<T: Into<i8>>(row: T, col: T) -> Self {
Coord {
row: row.into(),
col: col.into(),
}
}
/// is undefined
pub fn is_undefined(&self) -> bool {
*self == Self::undefined()
}
/// not yet set, has to later be set before using
pub fn undefined() -> Self {
Coord {
row: UNDEFINED_POSITION,
col: UNDEFINED_POSITION,
}
}

/// checks whether `self` is valid as a chess board coordinate
pub fn is_valid(&self) -> bool {
(0..8).contains(&self.col) && (0..8).contains(&self.col)
}
}

pub struct Board {
Expand Down Expand Up @@ -116,9 +100,9 @@ impl Default for Board {
],
],
cursor_coordinates: Coord::new(4, 4),
selected_coordinates: Coord::undefined(),
selected_coordinates: Coord::new(UNDEFINED_POSITION, UNDEFINED_POSITION),
selected_piece_cursor: 0,
old_cursor_position: Coord::undefined(),
old_cursor_position: Coord::new(UNDEFINED_POSITION, UNDEFINED_POSITION),
player_turn: PieceColor::White,
move_history: vec![],
is_draw: false,
Expand All @@ -142,9 +126,9 @@ impl Board {
Self {
board,
cursor_coordinates: Coord::new(4, 4),
selected_coordinates: Coord::undefined(),
selected_coordinates: Coord::new(UNDEFINED_POSITION, UNDEFINED_POSITION),
selected_piece_cursor: 0,
old_cursor_position: Coord::undefined(),
old_cursor_position: Coord::new(UNDEFINED_POSITION, UNDEFINED_POSITION),
player_turn,
move_history,
is_draw: false,
Expand Down Expand Up @@ -178,7 +162,8 @@ impl Board {

// Check if a cell has been selected
fn is_cell_selected(&self) -> bool {
!self.selected_coordinates.is_undefined()
self.selected_coordinates.row != UNDEFINED_POSITION
&& self.selected_coordinates.col != UNDEFINED_POSITION
}

fn get_authorized_positions(
Expand Down Expand Up @@ -257,7 +242,8 @@ impl Board {
// Method to unselect a cell
pub fn unselect_cell(&mut self) {
if self.is_cell_selected() {
self.selected_coordinates = Coord::undefined();
self.selected_coordinates.row = UNDEFINED_POSITION;
self.selected_coordinates.col = UNDEFINED_POSITION;
self.selected_piece_cursor = 0;
self.cursor_coordinates = self.old_cursor_position.clone()
}
Expand Down Expand Up @@ -292,7 +278,7 @@ impl Board {
self.cursor_coordinates = position.clone();
}
} else {
self.cursor_coordinates = Coord::undefined();
self.cursor_coordinates = Coord::new(UNDEFINED_POSITION, UNDEFINED_POSITION);
}
}

Expand Down Expand Up @@ -325,7 +311,7 @@ impl Board {
}
} else {
// We already selected a piece
if self.cursor_coordinates.is_valid() {
if is_valid(&self.cursor_coordinates) {
let selected_coords_usize = &self.selected_coordinates.clone();
let cursor_coords_usize = &self.cursor_coordinates.clone();
self.move_piece_on_the_board(selected_coords_usize, cursor_coords_usize);
Expand Down Expand Up @@ -386,19 +372,16 @@ impl Board {
let to_y = get_int_from_char(converted_move.chars().nth(2));
let to_x = get_int_from_char(converted_move.chars().nth(3));

self.move_piece_on_the_board(
&Coord::new(from_y as u8, from_x as u8),
&Coord::new(to_y as u8, to_x as u8),
);
self.move_piece_on_the_board(&Coord::new(from_y, from_x), &Coord::new(to_y, to_x));
}

// Convert the history and game status to a FEN string
pub fn fen_position(&self) -> String {
let mut result = String::new();

// We loop through the board and convert it to a FEN string
for i in 0..8u8 {
for j in 0..8u8 {
for i in 0..8i8 {
for j in 0..8i8 {
// We get the piece type and color
let (piece_type, piece_color) = (
get_piece_type(self.board, &Coord::new(i, j)),
Expand Down Expand Up @@ -508,10 +491,8 @@ impl Board {
_ => unreachable!("Promotion cursor out of boundaries"),
};

let current_piece_color = get_piece_color(
self.board,
&Coord::new(last_move.to_y as u8, last_move.to_x as u8),
);
let current_piece_color =
get_piece_color(self.board, &Coord::new(last_move.to_y, last_move.to_x));
if let Some(piece_color) = current_piece_color {
// we replace the piece by the new piece type
self.board[last_move.to_y as usize][last_move.to_x as usize] =
Expand All @@ -524,7 +505,7 @@ impl Board {

// Move a piece from a cell to another
pub fn move_piece_on_the_board(&mut self, from: &Coord, to: &Coord) {
if !from.is_valid() || !to.is_valid() {
if !is_valid(from) || !is_valid(to) {
return;
}
let direction_y: i32 = if self.player_turn == PieceColor::White {
Expand Down Expand Up @@ -611,10 +592,10 @@ impl Board {
// We store it in the history
self.move_history.push(PieceMove {
piece_type: piece_type_from,
from_y: from.row as i8,
from_x: from.col as i8,
to_y: to.row as i8,
to_x: to.col as i8,
from_y: from.row,
from_x: from.col,
to_y: to.row,
to_x: to.col,
});
}

Expand All @@ -629,7 +610,7 @@ impl Board {
possible_moves.extend(self.get_authorized_positions(
Some(piece_type),
Some(piece_color),
&Coord::new(i as u8, j as u8),
&Coord::new(i as i8, j as i8),
))
}
}
Expand Down Expand Up @@ -676,14 +657,12 @@ impl Board {
// Check if the latest move is a promotion
fn is_latest_move_promotion(&self) -> bool {
if let Some(last_move) = self.move_history.last() {
if let Some(piece_type_to) = get_piece_type(
self.board,
&Coord::new(last_move.to_y as u8, last_move.to_x as u8),
) {
if let Some(piece_color) = get_piece_color(
self.board,
&Coord::new(last_move.to_y as u8, last_move.to_x as u8),
) {
if let Some(piece_type_to) =
get_piece_type(self.board, &Coord::new(last_move.to_y, last_move.to_x))
{
if let Some(piece_color) =
get_piece_color(self.board, &Coord::new(last_move.to_y, last_move.to_x))
{
let last_row = if piece_color == PieceColor::White {
0
} else {
Expand Down Expand Up @@ -759,7 +738,7 @@ impl Board {
.split(area);

// For each line we set 8 layout
for i in 0..8u8 {
for i in 0..8i8 {
let lines = Layout::default()
.direction(Direction::Horizontal)
.constraints(
Expand All @@ -778,7 +757,7 @@ impl Board {
.as_ref(),
)
.split(columns[i as usize + 1]);
for j in 0..8u8 {
for j in 0..8i8 {
// Color of the cell to draw the board
let mut cell_color: Color = if (i + j) % 2 == 0 { WHITE } else { BLACK };

Expand Down
2 changes: 1 addition & 1 deletion src/constants.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use std::path::PathBuf;

use ratatui::style::Color;

pub const UNDEFINED_POSITION: u8 = u8::MAX;
pub const UNDEFINED_POSITION: i8 = -1;
pub const WHITE: Color = Color::Rgb(160, 160, 160);
pub const BLACK: Color = Color::Rgb(128, 95, 69);

Expand Down
42 changes: 13 additions & 29 deletions src/pieces/bishop.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use crate::board::Coord;
use crate::constants::DisplayMode;
use crate::utils::{
cleaned_positions, get_piece_color, impossible_positions_king_checked, is_cell_color_ally,
is_piece_opposite_king,
is_piece_opposite_king, is_valid,
};
pub struct Bishop;

Expand All @@ -21,21 +21,13 @@ impl Movable for Bishop {
let x = coordinates.col;

// for diagonal from piece to top left
for i in 1..8u8 {
let new_x = if let Some(new_x) = x.checked_sub(i) {
new_x
} else {
break;
};
let new_y = if let Some(new_y) = y.checked_sub(i) {
new_y
} else {
break;
};
for i in 1..8i8 {
let new_x = x - i;
let new_y = y - i;
let new_coordinates = Coord::new(new_y, new_x);

// Invalid coords
if !new_coordinates.is_valid() {
if !is_valid(&new_coordinates) {
break;
}

Expand Down Expand Up @@ -64,14 +56,14 @@ impl Movable for Bishop {
}

// for diagonal from piece to bottom right
for i in 1..8u8 {
for i in 1..8i8 {
let new_x = x + i;
let new_y = y + i;

let new_coordinates = Coord::new(new_y, new_x);

// Invalid coords
if !new_coordinates.is_valid() {
if !is_valid(&new_coordinates) {
break;
}

Expand Down Expand Up @@ -100,17 +92,13 @@ impl Movable for Bishop {
}

// for diagonal from piece to bottom left
for i in 1..8u8 {
let new_x = if let Some(new_x) = x.checked_sub(i) {
new_x
} else {
break;
};
for i in 1..8i8 {
let new_x = x - i;
let new_y = y + i;
let new_coordinates = Coord::new(new_y, new_x);

// Invalid coords
if !new_coordinates.is_valid() {
if !is_valid(&new_coordinates) {
break;
}

Expand Down Expand Up @@ -139,17 +127,13 @@ impl Movable for Bishop {
}

// for diagonal from piece to top right
for i in 1..8u8 {
for i in 1..8i8 {
let new_x = x + i;
let new_y = if let Some(new_y) = y.checked_sub(i) {
new_y
} else {
break;
};
let new_y = y - i;
let new_coordinates = Coord::new(new_y, new_x);

// Invalid coords
if !new_coordinates.is_valid() {
if !is_valid(&new_coordinates) {
break;
}

Expand Down
22 changes: 11 additions & 11 deletions src/pieces/king.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use crate::board::Coord;
use crate::constants::DisplayMode;
use crate::utils::{
cleaned_positions, did_piece_already_move, get_all_protected_cells, get_piece_type,
is_cell_color_ally, is_vec_in_array,
is_cell_color_ally, is_valid, is_vec_in_array,
};
pub struct King;

Expand All @@ -21,18 +21,18 @@ impl Movable for King {

// can move on a complete row
// Generate positions in all eight possible directions
for &dy in &[-1i8, 0, 1] {
for &dx in &[-1i8, 0, 1] {
for &dy in &[-1, 0, 1] {
for &dx in &[-1, 0, 1] {
// Skip the case where both dx and dy are zero (the current position)
let new_x = x as i8 + dx;
let new_y = y as i8 + dy;
let new_x = x + dx;
let new_y = y + dy;

let new_coordinates = Coord::new(new_y as u8, new_x as u8);
if new_coordinates.is_valid()
let new_coordinates = Coord::new(new_y, new_x);
if is_valid(&new_coordinates)
&& (!is_cell_color_ally(board, &new_coordinates, color)
|| allow_move_on_ally_positions)
{
positions.push(Coord::new((y as i8 + dy) as u8, (x as i8 + dx) as u8));
positions.push(Coord::new(y + dy, x + dx));
}
}
}
Expand Down Expand Up @@ -69,15 +69,15 @@ impl Position for King {
(Some(PieceType::Rook), [king_line, rook_big_castle_x]),
) && King::check_castling_condition(board, color, 0, 3, &checked_cells)
{
positions.push(Coord::new(king_line as u8, 0));
positions.push(Coord::new(king_line, 0));
}
// Small castle check
if !did_piece_already_move(
move_history,
(Some(PieceType::Rook), [king_line, rook_small_castle_x]),
) && King::check_castling_condition(board, color, 5, 7, &checked_cells)
{
positions.push(Coord::new(king_line as u8, 7));
positions.push(Coord::new(king_line, 7));
}
}

Expand Down Expand Up @@ -133,7 +133,7 @@ impl King {
let mut valid_for_castling = true;

for i in start..=end {
let new_coordinates = Coord::new(king_line, i as u8);
let new_coordinates = Coord::new(king_line, i);

if is_vec_in_array(checked_cells.to_owned(), &new_coordinates) {
valid_for_castling = false;
Expand Down
Loading

0 comments on commit d127e50

Please sign in to comment.