Skip to content

Commit

Permalink
Merge pull request #16 from sharma-shray/refactoring
Browse files Browse the repository at this point in the history
ref
  • Loading branch information
adam-mcdaniel authored Apr 18, 2024
2 parents 2b384de + 2fb47e1 commit 6d83f50
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 33 deletions.
22 changes: 8 additions & 14 deletions src/board.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
use super::*;
use alloc::{
string::{String, ToString},
vec::Vec,
};

use core::cmp::Ordering;

pub struct BoardBuilder {
Expand Down Expand Up @@ -505,14 +502,14 @@ impl Board {
/// Otherwise, return false.
#[inline]
pub fn has_piece(&self, pos: Position) -> bool {
self.get_piece(pos) != None
self.get_piece(pos).is_some()
}

/// If a square at a given position has no piece, return true.
/// Otherwise, return false.
#[inline]
pub fn has_no_piece(&self, pos: Position) -> bool {
self.get_piece(pos) == None
self.get_piece(pos).is_none()
}

/// If there is a king on the board, return the position that it sits on.
Expand Down Expand Up @@ -734,7 +731,7 @@ impl Board {

pieces.sort();

if pieces.len() == 0 {
if pieces.is_empty() {
false
} else if pieces.len() == 1 && pieces[0].is_king() {
false
Expand All @@ -748,14 +745,11 @@ impl Board {
&& pieces[2].is_knight()
{
false
} else if pieces.len() == 3
&& pieces[0].is_king()
&& pieces[1].is_bishop()
&& pieces[2].is_bishop()
{
false
} else {
true
!(pieces.len() == 3
&& pieces[0].is_king()
&& pieces[1].is_bishop()
&& pieces[2].is_bishop())
}
}

Expand Down
34 changes: 17 additions & 17 deletions src/position.rs
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ impl Position {

/// Create a `Position` from its respective row or column number.
/// The row and column numbers can be any of 0, 1, 2, 3, 4, 5, 6, or 7.
///
///
/// Examples:
/// - `A1 = Position::new(0, 0)`
/// - `A8 = Position::new(7, 0)`
Expand Down Expand Up @@ -217,7 +217,7 @@ impl Position {
fn diagonal_distance(&self, other: Self) -> i32 {
(self.col - other.col).abs()
}

/// Is this position orthogonal to another position?
#[inline]
pub fn is_orthogonal_to(&self, other: Self) -> bool {
Expand All @@ -231,7 +231,7 @@ impl Position {
}

/// Is this position adjacent to another position?
///
///
/// Adjacent positions have either:
/// 1. A diagonal distance of one from each other
/// 2. An orthogonal distance of one from each other
Expand All @@ -248,7 +248,7 @@ impl Position {

/// Is this position beneath another position on the board?
/// Pieces "beneath" other pieces on the board have lower ranks.
///
///
/// So, for example, A7 is below A8.
#[inline]
pub fn is_below(&self, other: Self) -> bool {
Expand All @@ -257,7 +257,7 @@ impl Position {

/// Is this position above another position on the board?
/// Pieces "above" other pieces on the board have higher ranks.
///
///
/// So, for example, A8 is above A8.
#[inline]
pub fn is_above(&self, other: Self) -> bool {
Expand All @@ -267,7 +267,7 @@ impl Position {
/// Is this position left of another position on the board?
/// Pieces "left of" other pieces on the board have a lower
/// lexigraphical column character.
///
///
/// So, for example, A8 is left of B8.
#[inline]
pub fn is_left_of(&self, other: Self) -> bool {
Expand All @@ -277,15 +277,15 @@ impl Position {
/// Is this position right of another position on the board?
/// Pieces "right of" other pieces on the board have a higher
/// lexigraphical column character.
///
///
/// So, for example, B8 is right of A8.
#[inline]
pub fn is_right_of(&self, other: Self) -> bool {
self.col > other.col
}

/// Get the position directly below this position.
///
///
/// IMPORTANT NOTE: This will NOT check for positions
/// off of the board! You could easily get an invalid
/// position if you do not check with the `is_on_board`
Expand All @@ -296,7 +296,7 @@ impl Position {
}

/// Get the position directly above this position.
///
///
/// IMPORTANT NOTE: This will NOT check for positions
/// off of the board! You could easily get an invalid
/// position if you do not check with the `is_on_board`
Expand All @@ -308,7 +308,7 @@ impl Position {

/// Get the next square upwards from a respective player's
/// pawn.
///
///
/// IMPORTANT NOTE: This will NOT check for positions
/// off of the board! You could easily get an invalid
/// position if you do not check with the `is_on_board`
Expand All @@ -323,7 +323,7 @@ impl Position {

/// Get the next square backwards from a respective player's
/// pawn.
///
///
/// IMPORTANT NOTE: This will NOT check for positions
/// off of the board! You could easily get an invalid
/// position if you do not check with the `is_on_board`
Expand All @@ -332,9 +332,9 @@ impl Position {
pub fn pawn_back(&self, ally_color: Color) -> Self {
self.pawn_up(!ally_color)
}

/// Get the position directly left of this position.
///
///
/// IMPORTANT NOTE: This will NOT check for positions
/// off of the board! You could easily get an invalid
/// position if you do not check with the `is_on_board`
Expand All @@ -345,7 +345,7 @@ impl Position {
}

/// Get the position directly right of this position.
///
///
/// IMPORTANT NOTE: This will NOT check for positions
/// off of the board! You could easily get an invalid
/// position if you do not check with the `is_on_board`
Expand All @@ -369,7 +369,7 @@ impl Position {
pub fn is_kingside_rook(&self) -> bool {
(self.row == 0 || self.row == 7) && self.col == 7
}

/// Is this the starting position of the queenside rook?
#[inline]
pub fn is_queenside_rook(&self) -> bool {
Expand All @@ -378,7 +378,7 @@ impl Position {

/// Get the list of positions from this position to another
/// position, moving diagonally.
///
///
/// This does _not_ include the `from` position, and includes the `to` position.
pub fn diagonals_to(&self, to: Self) -> Vec<Self> {
if !self.is_diagonal_to(to) {
Expand Down Expand Up @@ -411,7 +411,7 @@ impl Position {

/// Get the list of positions from this position to another
/// position, moving orthogonally.
///
///
/// This does _not_ include the `from` position, and includes the `to` position.
pub fn orthogonals_to(&self, to: Self) -> Vec<Self> {
if !self.is_orthogonal_to(to) {
Expand Down
2 changes: 1 addition & 1 deletion src/square.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ impl Square {
/// Does this square contain a piece?
#[inline]
pub fn is_empty(&self) -> bool {
self.piece == None
self.piece.is_none()
}

/// Get the piece contained in this square.
Expand Down
2 changes: 1 addition & 1 deletion src/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ pub fn parse_fen(fen: &str) -> Result<Board, String> {
false => Color::Black,
};
// using as because row/col always in u8 range
let position = Position::new(row as i32, col as i32);
let position = Position::new(row, col);
let piece = match x.to_ascii_lowercase() {
'b' => Piece::Bishop(color, position),
'n' => Piece::Knight(color, position),
Expand Down

0 comments on commit 6d83f50

Please sign in to comment.