Add methods to convert File and Rank to and from numbers
This commit is contained in:
parent
9338b3c669
commit
1cd980989a
60
src/main.rs
60
src/main.rs
|
@ -84,7 +84,7 @@ struct Move {
|
||||||
#[derive(serde::Deserialize, serde::Serialize)]
|
#[derive(serde::Deserialize, serde::Serialize)]
|
||||||
#[serde(rename_all = "camelCase")]
|
#[serde(rename_all = "camelCase")]
|
||||||
struct Start {
|
struct Start {
|
||||||
player_color: String,
|
player_color: Color,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Clone, PartialEq, Eq, PartialOrd, Ord, Hash, serde::Deserialize, serde::Serialize)]
|
#[derive(Clone, PartialEq, Eq, PartialOrd, Ord, Hash, serde::Deserialize, serde::Serialize)]
|
||||||
|
@ -127,6 +127,35 @@ enum Rank {
|
||||||
Eight = 8,
|
Eight = 8,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl Rank {
|
||||||
|
fn to_number(&self) -> u8 {
|
||||||
|
match self {
|
||||||
|
Self::One => 0,
|
||||||
|
Self::Two => 1,
|
||||||
|
Self::Three => 2,
|
||||||
|
Self::Four => 3,
|
||||||
|
Self::Five => 4,
|
||||||
|
Self::Six => 5,
|
||||||
|
Self::Seven => 6,
|
||||||
|
Self::Eight => 7,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn from_number(value: u8) -> Option<Self> {
|
||||||
|
match value {
|
||||||
|
0 => Some(Self::One),
|
||||||
|
1 => Some(Self::Two),
|
||||||
|
2 => Some(Self::Three),
|
||||||
|
3 => Some(Self::Four),
|
||||||
|
4 => Some(Self::Five),
|
||||||
|
5 => Some(Self::Six),
|
||||||
|
6 => Some(Self::Seven),
|
||||||
|
7 => Some(Self::Eight),
|
||||||
|
_ => None,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
#[derive(Clone, PartialEq, Eq, PartialOrd, Ord, Hash, serde::Deserialize, serde::Serialize)]
|
#[derive(Clone, PartialEq, Eq, PartialOrd, Ord, Hash, serde::Deserialize, serde::Serialize)]
|
||||||
#[serde(rename_all = "camelCase")]
|
#[serde(rename_all = "camelCase")]
|
||||||
enum File {
|
enum File {
|
||||||
|
@ -140,6 +169,35 @@ enum File {
|
||||||
H,
|
H,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl File {
|
||||||
|
fn to_number(&self) -> u8 {
|
||||||
|
match self {
|
||||||
|
Self::A => 0,
|
||||||
|
Self::B => 1,
|
||||||
|
Self::C => 2,
|
||||||
|
Self::D => 3,
|
||||||
|
Self::E => 4,
|
||||||
|
Self::F => 5,
|
||||||
|
Self::G => 6,
|
||||||
|
Self::H => 7,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn from_number(value: u8) -> Option<Self> {
|
||||||
|
match value {
|
||||||
|
0 => Some(Self::A),
|
||||||
|
1 => Some(Self::B),
|
||||||
|
2 => Some(Self::C),
|
||||||
|
3 => Some(Self::D),
|
||||||
|
4 => Some(Self::E),
|
||||||
|
5 => Some(Self::F),
|
||||||
|
6 => Some(Self::G),
|
||||||
|
7 => Some(Self::H),
|
||||||
|
_ => None,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
#[derive(Clone, Default)]
|
#[derive(Clone, Default)]
|
||||||
struct FileState {
|
struct FileState {
|
||||||
inner: HashMap<Rank, Piece>,
|
inner: HashMap<Rank, Piece>,
|
||||||
|
|
Loading…
Reference in a new issue