Skip to content

Commit

Permalink
chore: re-write evaluation in preparation for tuning
Browse files Browse the repository at this point in the history
bench: 1583604
  • Loading branch information
DeveloperPaul123 authored Dec 19, 2024
2 parents a78d965 + 6d87db3 commit 4f2f22b
Show file tree
Hide file tree
Showing 14 changed files with 639 additions and 101 deletions.
2 changes: 2 additions & 0 deletions .github/workflows/build_and_test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ jobs:
runs-on: ubuntu-22.04

steps:
- name: Rust nightly
run: rustup default nightly
- uses: actions/checkout@v4
with:
lfs: true
Expand Down
4 changes: 4 additions & 0 deletions .github/workflows/clippy.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@ jobs:
- uses: actions/checkout@v4
with:
lfs: true
- name: Rust nightly
run: |
rustup default nightly
rustup component add clippy
- name: Install just
uses: extractions/setup-just@v2
- name: clippy
Expand Down
7 changes: 6 additions & 1 deletion chess/src/pieces.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
* Created Date: Monday, November 25th 2024
* Author: Paul Tsouchlos (DeveloperPaul123) (developer.paul.123@gmail.com)
* -----
* Last Modified: Tue Nov 26 2024
* Last Modified: Tue Dec 17 2024
* -----
* Copyright (c) 2024 Paul Tsouchlos (DeveloperPaul123)
* GNU General Public License v3.0 or later
Expand Down Expand Up @@ -48,6 +48,11 @@ pub const ALL_PIECES: [Piece; 6] = [
Piece::Pawn,
];

/// Represents a chess piece.
///
/// **Note**: The ordinal value of the piece is used throughout the
/// code to index into arrays and tables. Changing the value of a piece
/// would likely be catastrophic and result in a number of bugs and possibly crashes.
#[repr(u8)]
#[derive(Debug, Default, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
pub enum Piece {
Expand Down
12 changes: 11 additions & 1 deletion chess/src/rank.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,16 @@
* Created Date: Monday, November 25th 2024
* Author: Paul Tsouchlos (DeveloperPaul123) (developer.paul.123@gmail.com)
* -----
* Last Modified: Tue Nov 26 2024
* Last Modified: Wed Dec 18 2024
* -----
* Copyright (c) 2024 Paul Tsouchlos (DeveloperPaul123)
* GNU General Public License v3.0 or later
* https://www.gnu.org/licenses/gpl-3.0-standalone.html
*
*/

use std::ops::Sub;

use crate::side::Side;
use anyhow::Result;

Expand Down Expand Up @@ -95,6 +97,14 @@ impl TryFrom<u8> for Rank {
}
}

impl Sub for Rank {
type Output = i8;

fn sub(self, other: Self) -> i8 {
self as i8 - other as i8
}
}

#[cfg(test)]
mod tests {
use super::*;
Expand Down
90 changes: 87 additions & 3 deletions chess/src/square.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
* Created Date: Friday, August 16th 2024
* Author: Paul Tsouchlos (DeveloperPaul123) (developer.paul.123@gmail.com)
* -----
* Last Modified: Tue Nov 26 2024
* Last Modified: Wed Dec 18 2024
* -----
* Copyright (c) 2024 Paul Tsouchlos (DeveloperPaul123)
* GNU General Public License v3.0 or later
Expand Down Expand Up @@ -109,6 +109,76 @@ impl Square {
Color::White
}
}

/// Flips the current square and returns a new instance at the flipped location.
pub fn flip(&self) -> Self {
let sq = self.to_square_index();
let flipped_sq = flip(sq);
Self::from_square_index(flipped_sq)
}
}

/// Flips the square vertically.
///
/// # Arguments
///
/// - `sq` - The square to flip.
///
/// # Returns
///
/// The flipped square
///
/// # Examples
///
/// ```
/// use chess::square::Square;
/// use chess::square::flip;
/// use chess::file::File;
/// use chess::rank::Rank;
///
/// let sq = Square::new(File::A, Rank::R1);
/// let flipped_sq = flip(sq.to_square_index());
/// assert_eq!(flipped_sq, 56);
/// let new_sq = Square::from_square_index(flipped_sq);
/// assert_eq!(new_sq.file, File::A);
/// assert_eq!(new_sq.rank, Rank::R8);
pub const fn flip(sq: u8) -> u8 {
sq ^ 56
}

/// Flips the square if the given boolean is `true`.
///
/// This will flip the square vertically if `flip` is `true`.
///
/// # Arguments
///
/// - `flip` - A boolean indicating if the square should be flipped.
/// - `sq` - The square to flip.
///
/// # Returns
///
/// The flipped square or sq if `flip` is `false`.
///
/// # Examples
///
/// ```
/// use chess::square::Square;
/// use chess::square::flip_if;
/// use chess::file::File;
/// use chess::rank::Rank;
///
/// let sq = Square::new(File::A, Rank::R1);
/// let flipped_sq = flip_if(true, sq.to_square_index());
/// assert_eq!(flipped_sq, 56);
/// let new_sq = Square::from_square_index(flipped_sq);
/// assert_eq!(new_sq.file, File::A);
/// assert_eq!(new_sq.rank, Rank::R8);
pub fn flip_if(should_flip: bool, sq: u8) -> u8 {
if should_flip {
flip(sq)
} else {
sq
}
}

impl TryFrom<&str> for Square {
Expand Down Expand Up @@ -184,10 +254,10 @@ pub const fn is_square_on_rank(square: u8, rank: u8) -> bool {
#[cfg(test)]
mod tests {
use crate::{
definitions::Squares,
definitions::{NumberOf, Squares},
file::File,
rank::Rank,
square::{is_square_on_rank, Square},
square::{is_square_on_rank, to_square, Square},
};

#[test]
Expand Down Expand Up @@ -215,4 +285,18 @@ mod tests {
let new_square = square.offset(-1, -1);
assert!(new_square.is_none());
}

#[test]
fn flip() {
for rank in 0..4_u8 {
for file in 0..NumberOf::FILES as u8 {
let sq = to_square(file, rank);
let square = Square::from_square_index(sq);
let flipped = square.flip();
assert_eq!(flipped.flip(), square);
assert_eq!(flipped.file, square.file);
assert_eq!(flipped.rank.as_number(), (Rank::R8 - square.rank) as u8);
}
}
}
}
Loading

0 comments on commit 4f2f22b

Please sign in to comment.