src/card.rs
author Peter Gervai <grin@grin.hu>
Tue, 31 Jan 2023 23:25:50 +0100
changeset 4 a2f0cb2b5c13
child 5 0dd7f2c9fd81
permissions -rw-r--r--
Split main into module files.

use core::fmt;

/*** Card ****/
#[derive(Debug)]
pub struct Card {
    pub value: i8,
    pub points: i8,
}

impl Card {
    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 (55=7)
            points += 5;
            // println!("NN add 5, val={}, pt={}", value, points);
        }

        if points == 0 {
            points = 1;
        }

        Card {
            value,
            points,
        }
    }
}

impl fmt::Display for Card {
    fn fmt( &self, f: &mut fmt::Formatter ) -> fmt::Result {
        write!(f, "(Card {}, points {})", self.value, self.points)
    }
}

impl PartialEq for Card {
    fn eq(&self, other: &Self) -> bool {
        self.value == other.value
    }
}

impl Eq for Card {} 

impl PartialOrd for Card {
    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,
        }
    }
}

impl Ord for Card {
    fn cmp(&self, other: &Self) -> std::cmp::Ordering {
        self.value.cmp(&other.value)
    }
}