-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Start on a custom chess representation crate
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
Showing
9 changed files
with
109,722 additions
and
18 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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>; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)), | ||
} | ||
} | ||
} |
Oops, something went wrong.