Guest User

Untitled

a guest
May 26th, 2018
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.28 KB | None | 0 0
  1. /// Suit of the card
  2. #[derive(Debug, Clone, Copy, PartialEq, Eq)]
  3. enum Suit {
  4. Hearts,
  5. Diamonds,
  6. Spades,
  7. Clubs,
  8. }
  9.  
  10. /// Gives the Rank enum a value for ordering
  11. impl Rank {
  12. fn value(self) -> u32 {
  13. match self {
  14. Num(n) => n,
  15. Jack => 11,
  16. Queen => 12,
  17. King => 13,
  18. Ace => 1,
  19. }
  20. }
  21. }
  22.  
  23. impl Ord for Rank {
  24. fn cmp(&self, other: &Rank) -> Ordering {
  25. self.cmp(&other)
  26. }
  27. }
  28.  
  29. impl PartialOrd for Rank {
  30. fn partial_cmp(&self, other: &Rank) -> Option<Ordering> {
  31. Some(self.cmp(other))
  32. }
  33. }
  34.  
  35. use Rank::*;
  36.  
  37. /// Struct for playing card
  38. #[derive(Debug, Clone, Copy, Eq)]
  39. struct Card {
  40. rank: Rank,
  41. suit: Suit,
  42. }
  43.  
  44. /// Comparison for cards by rank, ignoring suit
  45. impl PartialEq<Card> for Card {
  46. fn eq(&self, other: &Card) -> bool {
  47. self.rank == other.rank
  48. }
  49. }
  50.  
  51. /// Comparison for cards to a rank value
  52. impl PartialEq<u32> for Card {
  53. fn eq(&self, other: &u32) -> bool {
  54. self.rank.value() == *other
  55. }
  56. }
  57.  
  58. impl Ord for Card {
  59. fn cmp(&self, other: &Card) -> Ordering {
  60. self.rank.cmp(&other.rank)
  61. }
  62. }
  63.  
  64. impl PartialOrd for Card {
  65. fn partial_cmp(&self, other: &Card) -> Option<Ordering> {
  66. Some(self.cmp(other))
  67. }
  68. }
  69.  
  70. /// Create a new card
  71. impl Card {
  72. fn new(rank: Rank, suit: Suit) -> Card {
  73. Card {
  74. rank: rank,
  75. suit: suit,
  76. }
  77. }
  78. }
  79.  
  80. /// Top three cards form a run in any order
  81. fn is_run(pile: &Vec<Card>) -> bool {
  82. let mut temp: Vec<Card> = Vec::from_iter(pile[0..3].iter().cloned());
  83.  
  84. temp.sort_by_key(|x| x.rank);
  85. for t in &temp {
  86. println!("{}", t);
  87. }
  88. true
  89. }
  90.  
  91. fn main() {
  92. let mut deck: Vec<Card> = shuffle_deck(make_deck());
  93. let mut pile: Vec<Card> = Vec::new();
  94. // Take off 10 cards to for testing
  95. for _ in 0..11 {
  96. pile.push(deck.pop().unwrap());
  97. }
  98.  
  99. is_run(&pile);
  100. }
Add Comment
Please, Sign In to add comment