src/card.rs
changeset 4 a2f0cb2b5c13
child 5 0dd7f2c9fd81
equal deleted inserted replaced
3:3d9dba5b16e0 4:a2f0cb2b5c13
       
     1 use core::fmt;
       
     2 
       
     3 /*** Card ****/
       
     4 #[derive(Debug)]
       
     5 pub struct Card {
       
     6     pub value: i8,
       
     7     pub points: i8,
       
     8 }
       
     9 
       
    10 impl Card {
       
    11     pub fn new(value: i8)->Self {
       
    12 
       
    13         let mut points = 0;
       
    14         if value % 10 == 5 {
       
    15             // ends with 5 = 2 point
       
    16             points = 2;
       
    17             // println!("*5 add 1, val={}, pt={}", value, points);
       
    18         }
       
    19 
       
    20         if value % 10 == 0 {
       
    21             // ends with 0 = 3 point
       
    22             points = 3;
       
    23             // println!("*0 add 2, val={}, pt={}", value, points);
       
    24         }
       
    25 
       
    26         if value % 10 == value / 10 {
       
    27             // same numbers = 5 points (55=7)
       
    28             points += 5;
       
    29             // println!("NN add 5, val={}, pt={}", value, points);
       
    30         }
       
    31 
       
    32         if points == 0 {
       
    33             points = 1;
       
    34         }
       
    35 
       
    36         Card {
       
    37             value,
       
    38             points,
       
    39         }
       
    40     }
       
    41 }
       
    42 
       
    43 impl fmt::Display for Card {
       
    44     fn fmt( &self, f: &mut fmt::Formatter ) -> fmt::Result {
       
    45         write!(f, "(Card {}, points {})", self.value, self.points)
       
    46     }
       
    47 }
       
    48 
       
    49 impl PartialEq for Card {
       
    50     fn eq(&self, other: &Self) -> bool {
       
    51         self.value == other.value
       
    52     }
       
    53 }
       
    54 
       
    55 impl Eq for Card {} 
       
    56 
       
    57 impl PartialOrd for Card {
       
    58     fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> {
       
    59         match self.value.partial_cmp(&other.value) {
       
    60             Some(core::cmp::Ordering::Equal) => {None}
       
    61             ord => return ord,
       
    62         }
       
    63     }
       
    64 }
       
    65 
       
    66 impl Ord for Card {
       
    67     fn cmp(&self, other: &Self) -> std::cmp::Ordering {
       
    68         self.value.cmp(&other.value)
       
    69     }
       
    70 }