Comments. Lot of Comments.
use core::fmt;
/// This is the implementation of a Card.
/// Smart card! ;-) Can do a few things by herself.
#[derive(Debug)]
/// A playing Card. Knows its face `value`
/// and the punishment `points` (in the game
/// these are symbolised by little bull heads).
pub struct Card {
pub value: i8,
pub points: i8,
}
impl Card {
/// Generate a new card with the face `value` parameter.
/// Calculates the `points` for the value.
pub fn new(value: i8)->Self {
let mut points = 0;
if value % 10 == 5 {
// ends with 5 = 2 point
points = 2;
// println!("*5 add 1, val={}, pt={}", value, points);
}
if value % 10 == 0 {
// ends with 0 = 3 point
points = 3;
// println!("*0 add 2, val={}, pt={}", value, points);
}
if value % 10 == value / 10 {
// same numbers = 5 points (11,22,33... but 55=7)
points += 5;
// println!("NN add 5, val={}, pt={}", value, points);
}
if points == 0 {
// rest of the cards are 1 point
points = 1;
}
// Return a Card
Card {
value,
points,
}
}
}
impl fmt::Display for Card {
/// Print formatter for a card, so it can be written in `println!()`
fn fmt( &self, f: &mut fmt::Formatter ) -> fmt::Result {
write!(f, "(Card {}, points {})", self.value, self.points)
}
}
impl PartialEq for Card {
/// This is used for sorting cards.
fn eq(&self, other: &Self) -> bool {
self.value == other.value
}
}
/// This is used for sorting cards. Eq is required some by some strict sorting methods.
impl Eq for Card {}
impl PartialOrd for Card {
/// This is used for sorting cards.
fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> {
match self.value.partial_cmp(&other.value) {
Some(core::cmp::Ordering::Equal) => {None}
ord => return ord,
}
}
}
/// This is used for sorting cards. Ord (and Eq) is required some by some strict sorting methods.
impl Ord for Card {
fn cmp(&self, other: &Self) -> std::cmp::Ordering {
self.value.cmp(&other.value)
}
}