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