src/card.rs
changeset 5 0dd7f2c9fd81
parent 4 a2f0cb2b5c13
equal deleted inserted replaced
4:a2f0cb2b5c13 5:0dd7f2c9fd81
     1 use core::fmt;
     1 use core::fmt;
     2 
     2 
     3 /*** Card ****/
     3 /// This is the implementation of a Card. 
       
     4 /// Smart card! ;-) Can do a few things by herself.
       
     5 
     4 #[derive(Debug)]
     6 #[derive(Debug)]
       
     7 /// A playing Card. Knows its face `value` 
       
     8 /// and the punishment `points` (in the game 
       
     9 /// these are symbolised by little bull heads).
     5 pub struct Card {
    10 pub struct Card {
     6     pub value: i8,
    11     pub value: i8,
     7     pub points: i8,
    12     pub points: i8,
     8 }
    13 }
     9 
    14 
    10 impl Card {
    15 impl Card {
       
    16     /// Generate a new card with the face `value` parameter.
       
    17     /// Calculates the `points` for the value.
    11     pub fn new(value: i8)->Self {
    18     pub fn new(value: i8)->Self {
    12 
    19 
    13         let mut points = 0;
    20         let mut points = 0;
    14         if value % 10 == 5 {
    21         if value % 10 == 5 {
    15             // ends with 5 = 2 point
    22             // ends with 5 = 2 point
    22             points = 3;
    29             points = 3;
    23             // println!("*0 add 2, val={}, pt={}", value, points);
    30             // println!("*0 add 2, val={}, pt={}", value, points);
    24         }
    31         }
    25 
    32 
    26         if value % 10 == value / 10 {
    33         if value % 10 == value / 10 {
    27             // same numbers = 5 points (55=7)
    34             // same numbers = 5 points (11,22,33... but 55=7)
    28             points += 5;
    35             points += 5;
    29             // println!("NN add 5, val={}, pt={}", value, points);
    36             // println!("NN add 5, val={}, pt={}", value, points);
    30         }
    37         }
    31 
    38 
    32         if points == 0 {
    39         if points == 0 {
       
    40             // rest of the cards are 1 point
    33             points = 1;
    41             points = 1;
    34         }
    42         }
    35 
    43 
       
    44         // Return a Card
    36         Card {
    45         Card {
    37             value,
    46             value,
    38             points,
    47             points,
    39         }
    48         }
    40     }
    49     }
    41 }
    50 }
    42 
    51 
    43 impl fmt::Display for Card {
    52 impl fmt::Display for Card {
       
    53     /// Print formatter for a card, so it can be written in `println!()`
    44     fn fmt( &self, f: &mut fmt::Formatter ) -> fmt::Result {
    54     fn fmt( &self, f: &mut fmt::Formatter ) -> fmt::Result {
    45         write!(f, "(Card {}, points {})", self.value, self.points)
    55         write!(f, "(Card {}, points {})", self.value, self.points)
    46     }
    56     }
    47 }
    57 }
    48 
    58 
    49 impl PartialEq for Card {
    59 impl PartialEq for Card {
       
    60     /// This is used for sorting cards.
    50     fn eq(&self, other: &Self) -> bool {
    61     fn eq(&self, other: &Self) -> bool {
    51         self.value == other.value
    62         self.value == other.value
    52     }
    63     }
    53 }
    64 }
    54 
    65 
       
    66 /// This is used for sorting cards. Eq is required some by some strict sorting methods.
    55 impl Eq for Card {} 
    67 impl Eq for Card {} 
    56 
    68 
    57 impl PartialOrd for Card {
    69 impl PartialOrd for Card {
       
    70     /// This is used for sorting cards.
    58     fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> {
    71     fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> {
    59         match self.value.partial_cmp(&other.value) {
    72         match self.value.partial_cmp(&other.value) {
    60             Some(core::cmp::Ordering::Equal) => {None}
    73             Some(core::cmp::Ordering::Equal) => {None}
    61             ord => return ord,
    74             ord => return ord,
    62         }
    75         }
    63     }
    76     }
    64 }
    77 }
    65 
    78 
       
    79 /// This is used for sorting cards. Ord (and Eq) is required some by some strict sorting methods.
    66 impl Ord for Card {
    80 impl Ord for Card {
    67     fn cmp(&self, other: &Self) -> std::cmp::Ordering {
    81     fn cmp(&self, other: &Self) -> std::cmp::Ordering {
    68         self.value.cmp(&other.value)
    82         self.value.cmp(&other.value)
    69     }
    83     }
    70 }
    84 }