"Cleaned up" some things and started on a possible Klondike struct

This commit is contained in:
2025-02-24 23:52:17 +00:00
parent 71dab0cf5c
commit 499020b904

View File

@@ -20,8 +20,8 @@ pub enum Colour {
impl Suit { impl Suit {
pub fn colour(&self) -> Colour { pub fn colour(&self) -> Colour {
match *self { match *self {
Suit::Heart | Suit::Diamond => Colour::Red, Self::Heart | Suit::Diamond => Colour::Red,
Suit::Club | Suit::Spade => Colour::Black, Self::Club | Suit::Spade => Colour::Black,
} }
} }
} }
@@ -29,10 +29,10 @@ impl Suit {
impl fmt::Display for Suit { impl fmt::Display for Suit {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match self { match self {
Suit::Heart => write!(f, ""), Self::Heart => write!(f, ""),
Suit::Diamond => write!(f, ""), Self::Diamond => write!(f, ""),
Suit::Club => write!(f, ""), Self::Club => write!(f, ""),
Suit::Spade => write!(f, ""), Self::Spade => write!(f, ""),
} }
} }
} }
@@ -58,19 +58,19 @@ impl Value {
fn indexed_values(&self) -> u8 { fn indexed_values(&self) -> u8 {
// It might also make sense for Ace to be high... depends on context // It might also make sense for Ace to be high... depends on context
match self { match self {
Value::Ace => 1, Self::Ace => 1,
Value::Two => 2, Self::Two => 2,
Value::Three => 3, Self::Three => 3,
Value::Four => 4, Self::Four => 4,
Value::Five => 5, Self::Five => 5,
Value::Six => 6, Self::Six => 6,
Value::Seven => 7, Self::Seven => 7,
Value::Eight => 8, Self::Eight => 8,
Value::Nine => 9, Self::Nine => 9,
Value::Ten => 10, Self::Ten => 10,
Value::Jack => 11, Self::Jack => 11,
Value::Queen => 12, Self::Queen => 12,
Value::King => 13, Self::King => 13,
} }
} }
} }
@@ -78,19 +78,19 @@ impl Value {
impl fmt::Display for Value { impl fmt::Display for Value {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match self { match self {
Value::Ace => write!(f, "A"), Self::Ace => write!(f, "A"),
Value::Two => write!(f, "2"), Self::Two => write!(f, "2"),
Value::Three => write!(f, "3"), Self::Three => write!(f, "3"),
Value::Four => write!(f, "4"), Self::Four => write!(f, "4"),
Value::Five => write!(f, "5"), Self::Five => write!(f, "5"),
Value::Six => write!(f, "6"), Self::Six => write!(f, "6"),
Value::Seven => write!(f, "7"), Self::Seven => write!(f, "7"),
Value::Eight => write!(f, "8"), Self::Eight => write!(f, "8"),
Value::Nine => write!(f, "9"), Self::Nine => write!(f, "9"),
Value::Ten => write!(f, "T"), Self::Ten => write!(f, "T"),
Value::Jack => write!(f, "J"), Self::Jack => write!(f, "J"),
Value::Queen => write!(f, "Q"), Self::Queen => write!(f, "Q"),
Value::King => write!(f, "K"), Self::King => write!(f, "K"),
} }
} }
} }
@@ -99,6 +99,7 @@ impl fmt::Display for Value {
pub struct Card { pub struct Card {
pub suit: Suit, pub suit: Suit,
pub value: Value, pub value: Value,
pub visible: bool,
} }
@@ -108,6 +109,16 @@ impl fmt::Display for Card {
} }
} }
impl Default for Card {
fn default() -> Self {
Card {
suit: Suit::Spade,
value: Value::Ace, // If you like to gamble...
visible: true,
}
}
}
#[derive(Error, Debug, PartialEq)] #[derive(Error, Debug, PartialEq)]
pub enum StackingError { pub enum StackingError {
#[error("Trying to stack the same coloured suit")] #[error("Trying to stack the same coloured suit")]
@@ -138,7 +149,7 @@ pub struct Deck {
} }
impl Default for Deck { impl Default for Deck {
fn default() -> Deck { fn default() -> Self {
let mut array = Vec::new(); let mut array = Vec::new();
for suit in Suit::iter() { for suit in Suit::iter() {
for value in Value::iter() { for value in Value::iter() {
@@ -146,6 +157,7 @@ impl Default for Deck {
Card { Card {
suit, suit,
value, value,
..Default::default()
} }
); );
} }
@@ -156,6 +168,35 @@ impl Default for Deck {
} }
} }
enum NumPassesThroughDeck {
Unlimited,
Limited(u64),
}
pub struct Klondike {
piles: [Vec<Card>; 7],
deck: Vec<Card>,
waste: Vec<Card>,
foundation: [Vec<Card>; 4], // 4 = len of num suits
max_num_passes_through_deck: NumPassesThroughDeck,
current_num_passes_through_deck: u64,
num_cards_turned: u8,
}
/*
impl Default for Klondike {
fn default() -> Self {
let mut deck = Deck::default;
// shuffle deck
// deal some cards
let mut
// set some settings?
Self {
piles:
}
}
}
*/
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
@@ -166,25 +207,30 @@ mod tests {
let testing_card = Card { let testing_card = Card {
suit: Suit::Heart, suit: Suit::Heart,
value: Value::Five, value: Value::Five,
..Default::default()
}; };
let bad_same_suit = Card { let bad_same_suit = Card {
suit: Suit::Heart, suit: Suit::Heart,
value: Value::Six, value: Value::Six,
..Default::default()
}; };
assert_eq!(testing_card.can_be_placed_on_top(&bad_same_suit), Err(StackingError::SameColour)); assert_eq!(testing_card.can_be_placed_on_top(&bad_same_suit), Err(StackingError::SameColour));
let bad_same_colour = Card { let bad_same_colour = Card {
suit: Suit::Diamond, suit: Suit::Diamond,
value: Value::Six, value: Value::Six,
..Default::default()
}; };
assert_eq!(testing_card.can_be_placed_on_top(&bad_same_colour), Err(StackingError::SameColour)); assert_eq!(testing_card.can_be_placed_on_top(&bad_same_colour), Err(StackingError::SameColour));
let should_stack_card = Card { let should_stack_card = Card {
suit: Suit::Club, suit: Suit::Club,
value: Value::Six, value: Value::Six,
..Default::default()
}; };
assert_eq!(testing_card.can_be_placed_on_top(&should_stack_card), Ok(())); assert_eq!(testing_card.can_be_placed_on_top(&should_stack_card), Ok(()));
let value_too_high = Card { let value_too_high = Card {
suit: Suit::Club, suit: Suit::Club,
value: Value::Seven, value: Value::Seven,
..Default::default()
}; };
let not_adj_error = testing_card.can_be_placed_on_top(&value_too_high); let not_adj_error = testing_card.can_be_placed_on_top(&value_too_high);
if let Err(e) = not_adj_error { if let Err(e) = not_adj_error {
@@ -201,6 +247,6 @@ mod tests {
fn get_a_whole_deck() { fn get_a_whole_deck() {
let d = Deck::default(); let d = Deck::default();
assert_eq!(d.cards.len(), 52); // Probably should test whether all cards are in... eh assert_eq!(d.cards.len(), 52); // Probably should test whether all cards are in... eh
println!("{:#?}", d); println!("{:#?}", d); // A "manual" review looks alright
} }
} }