Skip to content

Commit

Permalink
Start on a custom chess representation crate
Browse files Browse the repository at this point in the history
Meant to replace `chess` in the codebase.

Two reasons:

1. Because I'd like to make my own, including move generation with magic bitboards.
2. Allow more control of how moves are made and what is and isn't stored in the `Board` struct.
  • Loading branch information
bsamseth committed Oct 31, 2024
1 parent 686549f commit 93baa3f
Show file tree
Hide file tree
Showing 9 changed files with 109,722 additions and 18 deletions.
61 changes: 44 additions & 17 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[workspace]

members = ["engine", "fathom", "uci", "uciderive"]
members = ["engine", "fathom", "goldchess", "goldchess-gen", "uci", "uciderive"]
resolver = "2"

[profile.release-lto]
Expand Down
7 changes: 7 additions & 0 deletions goldchess/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
[package]
name = "goldchess"
version = "0.1.0"
edition = "2021"

[dependencies]
thiserror = "1.0.65"
17 changes: 17 additions & 0 deletions goldchess/src/error.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
use thiserror::Error;

#[derive(Debug, Error)]
pub enum Error {
#[error("Invalid square index: {0}")]
InvalidSquare(u8),
#[error("Invalid file index: {0}")]
InvalidFile(u8),
#[error("Invalid file notation: {0}")]
InvalidFileChar(char),
#[error("Invalid rank index: {0}")]
InvalidRank(u8),
#[error("Invalid rank notation: {0}")]
InvalidRankChar(char),
}

pub type Result<T, E = Error> = std::result::Result<T, E>;
95 changes: 95 additions & 0 deletions goldchess/src/file.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
use std::str::FromStr;

use crate::{Error, Result};

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct File(pub u8);

impl File {
/// Create a new file from a `u8`.
///
/// # Safety
/// The input must be a valid file index, i.e. 0 <= file < 8.
#[must_use]
pub unsafe fn new_unchecked(file: u8) -> Self {
Self(file)
}

/// Create a new file from a `u8`.
///
/// # Errors
/// Returns an error if the input is not a valid file index.
pub fn new(file: u8) -> Result<Self, Error> {
if file < 8 {
Ok(Self(file))
} else {
Err(Error::InvalidFile(file))
}
}

/// Get the file to the left of this one, if it exists.
#[must_use]
pub fn left(self) -> Option<Self> {
if self.0 == 0 {
None
} else {
Some(Self(self.0 - 1))
}
}

/// Get the file to the right of this one, if it exists.
#[must_use]
pub fn right(self) -> Option<Self> {
if self.0 == 7 {
None
} else {
Some(Self(self.0 + 1))
}
}
}

impl File {
pub const A: File = File(0);
pub const B: File = File(1);
pub const C: File = File(2);
pub const D: File = File(3);
pub const E: File = File(4);
pub const F: File = File(5);
pub const G: File = File(6);
pub const H: File = File(7);
}

impl From<File> for u8 {
fn from(file: File) -> u8 {
file.0
}
}
impl TryFrom<u8> for File {
type Error = Error;

fn try_from(file: u8) -> Result<Self> {
File::new(file)
}
}

impl FromStr for File {
type Err = Error;

fn from_str(s: &str) -> Result<Self, Self::Err> {
match s
.chars()
.next()
.ok_or(Error::InvalidFileChar(std::char::REPLACEMENT_CHARACTER))?
{
'a' | 'A' => Ok(File::A),
'b' | 'B' => Ok(File::B),
'c' | 'C' => Ok(File::C),
'd' | 'D' => Ok(File::D),
'e' | 'E' => Ok(File::E),
'f' | 'F' => Ok(File::F),
'g' | 'G' => Ok(File::G),
'h' | 'H' => Ok(File::H),
other => Err(Error::InvalidFileChar(other)),
}
}
}
Loading

0 comments on commit 93baa3f

Please sign in to comment.