Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /// Suit of the card
- #[derive(Debug, Clone, Copy, PartialEq, Eq)]
- enum Suit {
- Hearts,
- Diamonds,
- Spades,
- Clubs,
- }
- /// Gives the Rank enum a value for ordering
- impl Rank {
- fn value(self) -> u32 {
- match self {
- Num(n) => n,
- Jack => 11,
- Queen => 12,
- King => 13,
- Ace => 1,
- }
- }
- }
- impl Ord for Rank {
- fn cmp(&self, other: &Rank) -> Ordering {
- self.cmp(&other)
- }
- }
- impl PartialOrd for Rank {
- fn partial_cmp(&self, other: &Rank) -> Option<Ordering> {
- Some(self.cmp(other))
- }
- }
- use Rank::*;
- /// Struct for playing card
- #[derive(Debug, Clone, Copy, Eq)]
- struct Card {
- rank: Rank,
- suit: Suit,
- }
- /// Comparison for cards by rank, ignoring suit
- impl PartialEq<Card> for Card {
- fn eq(&self, other: &Card) -> bool {
- self.rank == other.rank
- }
- }
- /// Comparison for cards to a rank value
- impl PartialEq<u32> for Card {
- fn eq(&self, other: &u32) -> bool {
- self.rank.value() == *other
- }
- }
- impl Ord for Card {
- fn cmp(&self, other: &Card) -> Ordering {
- self.rank.cmp(&other.rank)
- }
- }
- impl PartialOrd for Card {
- fn partial_cmp(&self, other: &Card) -> Option<Ordering> {
- Some(self.cmp(other))
- }
- }
- /// Create a new card
- impl Card {
- fn new(rank: Rank, suit: Suit) -> Card {
- Card {
- rank: rank,
- suit: suit,
- }
- }
- }
- /// Top three cards form a run in any order
- fn is_run(pile: &Vec<Card>) -> bool {
- let mut temp: Vec<Card> = Vec::from_iter(pile[0..3].iter().cloned());
- temp.sort_by_key(|x| x.rank);
- for t in &temp {
- println!("{}", t);
- }
- true
- }
- fn main() {
- let mut deck: Vec<Card> = shuffle_deck(make_deck());
- let mut pile: Vec<Card> = Vec::new();
- // Take off 10 cards to for testing
- for _ in 0..11 {
- pile.push(deck.pop().unwrap());
- }
- is_run(&pile);
- }
Add Comment
Please, Sign In to add comment