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:
@@ -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