Compare commits
2 Commits
ff801f259d
...
6f5d7d7361
| Author | SHA1 | Date | |
|---|---|---|---|
| 6f5d7d7361 | |||
| 086135abb5 |
@@ -109,27 +109,27 @@ impl fmt::Display for Card {
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Error, Debug)]
|
||||
#[derive(Error, Debug, PartialEq)]
|
||||
pub enum StackingError {
|
||||
#[error("Trying to stack the same coloured suit")]
|
||||
SameColour,
|
||||
#[error("{0} is not \"next\" to {1}")]
|
||||
NotAdjacent(Card, Card),
|
||||
NotAdjacent(String, String),
|
||||
}
|
||||
|
||||
impl Card {
|
||||
pub fn can_be_placed_on_top(&self, top: Card) -> bool {
|
||||
pub fn can_be_placed_on_top(&self, top: Card) -> Result<(), StackingError> {
|
||||
// Can't be the same Colour
|
||||
if self.suit.colour() != top.suit.colour() {
|
||||
return false
|
||||
if self.suit.colour() == top.suit.colour() {
|
||||
return Err(StackingError::SameColour);
|
||||
}
|
||||
|
||||
// Needs to be adjascent
|
||||
if self.value == Value::King || self.value.indexed_values() != top.value.indexed_values() + 1 {
|
||||
return false
|
||||
// Needs to be adjacent
|
||||
if self.value == Value::King || self.value.indexed_values() + 1 != top.value.indexed_values() {
|
||||
return Err(StackingError::NotAdjacent(self.to_string(), top.to_string()));
|
||||
}
|
||||
|
||||
true
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
@@ -139,8 +139,38 @@ mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn it_works() {
|
||||
let result = add(2, 2);
|
||||
assert_eq!(result, 4);
|
||||
fn basic_card_stacking() {
|
||||
let testing_card = Card {
|
||||
suit: Suit::Heart,
|
||||
value: Value::Five,
|
||||
};
|
||||
let bad_same_suit = Card {
|
||||
suit: Suit::Heart,
|
||||
value: Value::Six,
|
||||
};
|
||||
assert_eq!(testing_card.can_be_placed_on_top(bad_same_suit), Err(StackingError::SameColour));
|
||||
let bad_same_colour = Card {
|
||||
suit: Suit::Diamond,
|
||||
value: Value::Six,
|
||||
};
|
||||
assert_eq!(testing_card.can_be_placed_on_top(bad_same_colour), Err(StackingError::SameColour));
|
||||
let should_stack_card = Card {
|
||||
suit: Suit::Club,
|
||||
value: Value::Six,
|
||||
};
|
||||
assert_eq!(testing_card.can_be_placed_on_top(should_stack_card), Ok(()));
|
||||
let value_too_high = Card {
|
||||
suit: Suit::Club,
|
||||
value: Value::Seven,
|
||||
};
|
||||
let not_adj_error = testing_card.can_be_placed_on_top(value_too_high);
|
||||
if let Err(e) = not_adj_error {
|
||||
match e {
|
||||
StackingError::NotAdjacent(_, _) => assert!(true),
|
||||
StackingError::SameColour => assert!(false, "Colour is different - incorrect error"),
|
||||
}
|
||||
} else {
|
||||
assert!(false, "Cards are not adjacent - should be an error")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user