Added some display stuff
For errors, and I guess just general displays I should try extend into colour I think too, just for fun
This commit is contained in:
65
card_stuffs/Cargo.lock
generated
65
card_stuffs/Cargo.lock
generated
@@ -2,6 +2,71 @@
|
||||
# It is not intended for manual editing.
|
||||
version = 4
|
||||
|
||||
[[package]]
|
||||
name = "anyhow"
|
||||
version = "1.0.96"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "6b964d184e89d9b6b67dd2715bc8e74cf3107fb2b529990c90cf517326150bf4"
|
||||
|
||||
[[package]]
|
||||
name = "card_stuffs"
|
||||
version = "0.1.0"
|
||||
dependencies = [
|
||||
"anyhow",
|
||||
"thiserror",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "proc-macro2"
|
||||
version = "1.0.93"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "60946a68e5f9d28b0dc1c21bb8a97ee7d018a8b322fa57838ba31cc878e22d99"
|
||||
dependencies = [
|
||||
"unicode-ident",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "quote"
|
||||
version = "1.0.38"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "0e4dccaaaf89514f546c693ddc140f729f958c247918a13380cccc6078391acc"
|
||||
dependencies = [
|
||||
"proc-macro2",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "syn"
|
||||
version = "2.0.98"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "36147f1a48ae0ec2b5b3bc5b537d267457555a10dc06f3dbc8cb11ba3006d3b1"
|
||||
dependencies = [
|
||||
"proc-macro2",
|
||||
"quote",
|
||||
"unicode-ident",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "thiserror"
|
||||
version = "2.0.11"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "d452f284b73e6d76dd36758a0c8684b1d5be31f92b89d07fd5822175732206fc"
|
||||
dependencies = [
|
||||
"thiserror-impl",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "thiserror-impl"
|
||||
version = "2.0.11"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "26afc1baea8a989337eeb52b6e72a039780ce45c3edfcc9c5b9d112feeb173c2"
|
||||
dependencies = [
|
||||
"proc-macro2",
|
||||
"quote",
|
||||
"syn",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "unicode-ident"
|
||||
version = "1.0.17"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "00e2473a93778eb0bad35909dff6a10d28e63f792f16ed15e404fca9d5eeedbe"
|
||||
|
||||
@@ -4,3 +4,5 @@ version = "0.1.0"
|
||||
edition = "2021"
|
||||
|
||||
[dependencies]
|
||||
anyhow = "1.0.96"
|
||||
thiserror = "2.0.11"
|
||||
|
||||
@@ -1,3 +1,8 @@
|
||||
use thiserror::Error;
|
||||
use anyhow::Result;
|
||||
use std::fmt;
|
||||
|
||||
#[derive(PartialEq, Debug)]
|
||||
pub enum Suit {
|
||||
Heart,
|
||||
Diamond,
|
||||
@@ -5,6 +10,35 @@ pub enum Suit {
|
||||
Spade
|
||||
}
|
||||
|
||||
#[derive(PartialEq, Debug)]
|
||||
pub enum Colour {
|
||||
Black,
|
||||
Red,
|
||||
}
|
||||
|
||||
impl Suit {
|
||||
pub fn colour(&self) -> Colour {
|
||||
match *self {
|
||||
Suit::Heart => Colour::Red,
|
||||
Suit::Diamond => Colour::Red,
|
||||
Suit::Club => Colour::Black,
|
||||
Suit::Spade => Colour::Black,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl fmt::Display for Suit {
|
||||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
||||
match self {
|
||||
Suit::Heart => write!(f, "♥"),
|
||||
Suit::Diamond => write!(f, "♦"),
|
||||
Suit::Club => write!(f, "♣"),
|
||||
Suit::Spade => write!(f, "♠"),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(PartialEq, Debug)]
|
||||
pub enum Value {
|
||||
Ace,
|
||||
Two,
|
||||
@@ -23,7 +57,8 @@ pub enum Value {
|
||||
|
||||
impl Value {
|
||||
pub fn indexed_values(&self) -> u8 {
|
||||
match *self {
|
||||
// It might also make sense for Ace to be high... depends on context
|
||||
match self {
|
||||
Value::Ace => 1,
|
||||
Value::Two => 2,
|
||||
Value::Three => 3,
|
||||
@@ -41,17 +76,64 @@ impl Value {
|
||||
}
|
||||
}
|
||||
|
||||
impl fmt::Display for Value {
|
||||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
||||
match self {
|
||||
Value::Ace => write!(f, "A"),
|
||||
Value::Two => write!(f, "2"),
|
||||
Value::Three => write!(f, "3"),
|
||||
Value::Four => write!(f, "4"),
|
||||
Value::Five => write!(f, "5"),
|
||||
Value::Six => write!(f, "6"),
|
||||
Value::Seven => write!(f, "7"),
|
||||
Value::Eight => write!(f, "8"),
|
||||
Value::Nine => write!(f, "9"),
|
||||
Value::Ten => write!(f, "T"),
|
||||
Value::Jack => write!(f, "J"),
|
||||
Value::Queen => write!(f, "Q"),
|
||||
Value::King => write!(f, "K"),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(PartialEq, Debug)]
|
||||
pub struct Card {
|
||||
pub suit: Suit,
|
||||
pub value: Value,
|
||||
}
|
||||
|
||||
|
||||
|
||||
pub fn add(left: u64, right: u64) -> u64 {
|
||||
left + right
|
||||
impl fmt::Display for Card {
|
||||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
||||
write!(f, "{}{}", self.value, self.suit)
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Error, Debug)]
|
||||
pub enum StackingError {
|
||||
#[error("Trying to stack the same coloured suit")]
|
||||
SameColour,
|
||||
#[error("{0} is not \"next\" to {1}")]
|
||||
NotAdjacent(Card, Card),
|
||||
}
|
||||
|
||||
impl Card {
|
||||
pub fn can_be_placed_on_top(&self, top: Card) -> bool {
|
||||
// Can't be the same Colour
|
||||
if self.suit.colour() != top.suit.colour() {
|
||||
return false
|
||||
}
|
||||
|
||||
// Needs to be adjascent
|
||||
if self.value == Value::King || self.value.indexed_values() != top.value.indexed_values() + 1 {
|
||||
return false
|
||||
}
|
||||
|
||||
true
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
Reference in New Issue
Block a user