Skip to content

Commit 93baa3f

Browse files
committed
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.
1 parent 686549f commit 93baa3f

File tree

9 files changed

+109722
-18
lines changed

9 files changed

+109722
-18
lines changed

Cargo.lock

Lines changed: 44 additions & 17 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[workspace]
22

3-
members = ["engine", "fathom", "uci", "uciderive"]
3+
members = ["engine", "fathom", "goldchess", "goldchess-gen", "uci", "uciderive"]
44
resolver = "2"
55

66
[profile.release-lto]

goldchess/Cargo.toml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
[package]
2+
name = "goldchess"
3+
version = "0.1.0"
4+
edition = "2021"
5+
6+
[dependencies]
7+
thiserror = "1.0.65"

goldchess/src/error.rs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
use thiserror::Error;
2+
3+
#[derive(Debug, Error)]
4+
pub enum Error {
5+
#[error("Invalid square index: {0}")]
6+
InvalidSquare(u8),
7+
#[error("Invalid file index: {0}")]
8+
InvalidFile(u8),
9+
#[error("Invalid file notation: {0}")]
10+
InvalidFileChar(char),
11+
#[error("Invalid rank index: {0}")]
12+
InvalidRank(u8),
13+
#[error("Invalid rank notation: {0}")]
14+
InvalidRankChar(char),
15+
}
16+
17+
pub type Result<T, E = Error> = std::result::Result<T, E>;

goldchess/src/file.rs

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
use std::str::FromStr;
2+
3+
use crate::{Error, Result};
4+
5+
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
6+
pub struct File(pub u8);
7+
8+
impl File {
9+
/// Create a new file from a `u8`.
10+
///
11+
/// # Safety
12+
/// The input must be a valid file index, i.e. 0 <= file < 8.
13+
#[must_use]
14+
pub unsafe fn new_unchecked(file: u8) -> Self {
15+
Self(file)
16+
}
17+
18+
/// Create a new file from a `u8`.
19+
///
20+
/// # Errors
21+
/// Returns an error if the input is not a valid file index.
22+
pub fn new(file: u8) -> Result<Self, Error> {
23+
if file < 8 {
24+
Ok(Self(file))
25+
} else {
26+
Err(Error::InvalidFile(file))
27+
}
28+
}
29+
30+
/// Get the file to the left of this one, if it exists.
31+
#[must_use]
32+
pub fn left(self) -> Option<Self> {
33+
if self.0 == 0 {
34+
None
35+
} else {
36+
Some(Self(self.0 - 1))
37+
}
38+
}
39+
40+
/// Get the file to the right of this one, if it exists.
41+
#[must_use]
42+
pub fn right(self) -> Option<Self> {
43+
if self.0 == 7 {
44+
None
45+
} else {
46+
Some(Self(self.0 + 1))
47+
}
48+
}
49+
}
50+
51+
impl File {
52+
pub const A: File = File(0);
53+
pub const B: File = File(1);
54+
pub const C: File = File(2);
55+
pub const D: File = File(3);
56+
pub const E: File = File(4);
57+
pub const F: File = File(5);
58+
pub const G: File = File(6);
59+
pub const H: File = File(7);
60+
}
61+
62+
impl From<File> for u8 {
63+
fn from(file: File) -> u8 {
64+
file.0
65+
}
66+
}
67+
impl TryFrom<u8> for File {
68+
type Error = Error;
69+
70+
fn try_from(file: u8) -> Result<Self> {
71+
File::new(file)
72+
}
73+
}
74+
75+
impl FromStr for File {
76+
type Err = Error;
77+
78+
fn from_str(s: &str) -> Result<Self, Self::Err> {
79+
match s
80+
.chars()
81+
.next()
82+
.ok_or(Error::InvalidFileChar(std::char::REPLACEMENT_CHARACTER))?
83+
{
84+
'a' | 'A' => Ok(File::A),
85+
'b' | 'B' => Ok(File::B),
86+
'c' | 'C' => Ok(File::C),
87+
'd' | 'D' => Ok(File::D),
88+
'e' | 'E' => Ok(File::E),
89+
'f' | 'F' => Ok(File::F),
90+
'g' | 'G' => Ok(File::G),
91+
'h' | 'H' => Ok(File::H),
92+
other => Err(Error::InvalidFileChar(other)),
93+
}
94+
}
95+
}

0 commit comments

Comments
 (0)