daily pastebin goal
3%
SHARE
TWEET

Untitled

a guest Feb 13th, 2018 53 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. struct Card: Comparable, CustomStringConvertible {
  2.  
  3.   // Represents the four suits of a traditional french playing card deck.
  4.   // Primarly import for physical representation purposes, as
  5.   // Sheepshead uses different definition of suit for playing purposes
  6.   // This actual suit however, is useful for determining the relative
  7.   // strength of the queens and the jacks which are each ranked in decreasing
  8.   // order of strength: clubs, spades, hearts, diamonds
  9.   enum _Suit: Int, Comparable {
  10.     case clubs = 1, spades, hearts, diamonds
  11.    
  12.     static func <(lhs: _Suit, rhs: _Suit) -> Bool {
  13.       return lhs.rawValue > rhs.rawValue
  14.     }
  15.    
  16.     var description: String { ... }
  17.   }
  18.  
  19.   // Represents the suit of each playing card as defined by Sheepshead.
  20.   // Trump include all queens, jacks and diamonds, while clubs, spades,
  21.   // hearts include the remaining clubs, spades, and hearts, respectively.
  22.   // The raw value of the suits can be used for sorting the cards
  23.   // within a hand.
  24.   enum Suit: Int, Comparable {
  25.     case trump = 1, clubs, spades, hearts
  26.    
  27.     static func <(lhs: Suit, rhs: Suit) -> Bool {
  28.       return lhs.rawValue > rhs.rawValue
  29.     }
  30.   }
  31.  
  32.   // The possible ranks of each playing card in a game of Sheepshead.  
  33.   // Since the game uses only the sevens through aces, the twos through
  34.   // sixes are removed altogether.
  35.   // The raw value of the suits is used for determining the strength of
  36.   // the ranks within a particular suit, with the queens and jacks applying
  37.   // to only the trump suit.
  38.   enum Rank: Int, Comparable {
  39.     case queen = 1, jack, ace, ten, king, nine, eight, seven
  40.    
  41.     static func <(lhs: Rank, rhs: Rank) -> Bool {
  42.       return lhs.rawValue > rhs.rawValue
  43.     }
  44.    
  45.     // The point value of each card for determining the winner of each hand
  46.     var pointValue: Int {
  47.       switch self {
  48.         case .ace: return 11; case .ten: return 10; case .king: return 4
  49.         case .queen: return 3; case .jack: return 2; default: return 0
  50.       }
  51.     }
  52.     var description: String { ... }
  53.   }
  54.  
  55.   var _suit: _Suit
  56.   var suit: Suit {
  57.     if rank.rawValue <= 2 || _suit == .diamonds { return .trump }
  58.     if _suit == .clubs { return .clubs }
  59.     if _suit == .spades { return .spades }
  60.     if _suit == .hearts { return .hearts }
  61.   }
  62.   var rank: Rank
  63.   var description: String { rank.description + suit.description }
  64.  
  65.   init(_suit: _Suit, rank: Rank) {
  66.     self._suit = _suit
  67.     self.rank = rank
  68.   }
  69. }
  70.  
  71. extension Card {
  72.   // Because of the nature of trick-taking card games, the strength of
  73.   // all non-trump cards varies depending on the leading suit.  
  74.   // For example, a seven of hearts defeats an ace of spades when
  75.   // hearts are lead, but is essentially equivalent to the ace of spades
  76.   // when clubs are lead, as neither card can take the trick in such instance.
  77.   // However, the comparison operator is useful for sorting cards within a hand,
  78.   // as well as determining the relative strengths of cards within a particular suit
  79.   static func <(lhs: Card, rhs: Card) -> Bool {
  80.    
  81.     // If suits are equivalent, compare ranks
  82.     if lhs.suit == rhs.suit {
  83.       // If suits and ranks are the same, we must be looking at either two
  84.       // queens or two jacks and need to compare their actual suit
  85.       if lhs.rank == rhs.rank {
  86.         return lhs._suit < rhs._suit
  87.       }
  88.       //Return the "lesser" suit
  89.       return lhs.suit < rhs.suit
  90.   }
  91.  
  92.   static func ==(lhs: Card, rhs: Card) -> Bool {
  93.     return lhs._suit == rhs._suit && lhs.rank == rhs.rank
  94.   }
  95. }
  96. extension Card {
  97.   // A comparison operator more useful within the game context.
  98.   // The strength of cards within the game is always made with reference
  99.   // to the leading suit, so we will take that into account when comparing
  100.   // two cards.  The first card of a particular trick will always be the best
  101.   // card of the trick when it is laid, and all further plays are evaulated
  102.   // based on the suit of that card.
  103.   // Further, two cards must never be compared unless one of them is, at
  104.   // the time of comparison, the best card of the trick.
  105.   func compareTo(bestCardThusFar best: Card, withLeadSuit leadSuit: Card.Suit) -> Bool {
  106.     // To win a trick the trick over the best card, the card must either be the same
  107.     // suit as the leading card, or a trump card.  Second, it must be a higher rank than the
  108.     // best card.  Conveniently, if it meets the first criteria, we can merely compare the
  109.     // cards using the standard comparison operator.
  110.     if self.suit == leadSuit || self.suit == .trump {
  111.       return this > other
  112.     }
  113.     return false
  114.    }
  115. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top