def sorted_hand(hand, trump_suit=None): """Sorts a hand of cards. Puts trumps first, if any, then sorts the remaining suits arbitrarily. Returns this newly-sorted hand. """ trump_cards = Hand() other_suits = {} for card in hand: this_suit = card.suit if card.suit == trump_suit: hand_to_add_to = trump_cards else: if this_suit not in other_suits: other_suits[this_suit] = Hand() hand_to_add_to = other_suits[this_suit] hand_to_add_to.add(card) # Now that they're sorted out, combine them back together. s_hand = Hand() trump_cards.sort() for card in trump_cards: s_hand.add(card) for suit in sorted(other_suits.keys()): suit_hand = other_suits[suit] suit_hand.sort() for card in suit_hand: s_hand.add(card) return s_hand
def init_hand(self): # Depending on the number of suits requested for play, we build # structures. suit_list = DEFAULT_SUITS[:] if self.suit_count >= 6: suit_list.append(CYAN) if self.suit_count == 7: suit_list.append(MAGENTA) # If, on the other hand, the number of suits is /less/ than five, # we use a subset of the default suits. if self.suit_count < 5: suit_list = DEFAULT_SUITS[:self.suit_count] # All right, we have a list of suits involved in this game. Let's # build the various piles that are based on those suits. self.left.data.expeditions = [] self.right.data.expeditions = [] self.discards = [] for suit in suit_list: discard_pile = Struct() left_expedition = Struct() right_expedition = Struct() for pile in (discard_pile, left_expedition, right_expedition): pile.suit = suit pile.hand = Hand() pile.value = 0 self.left.data.expeditions.append(left_expedition) self.right.data.expeditions.append(right_expedition) self.discards.append(discard_pile) # We'll do a separate loop for generating the deck to minimize # confusion. self.draw_pile = Hand() for suit in suit_list: for rank in NUMERICAL_RANKS: self.draw_pile.add(ExpeditionsCard(rank, suit)) # Add as many agreements as requested. for agreement in range(self.agreement_count): self.draw_pile.add(ExpeditionsCard(AGREEMENT, suit)) # Lastly, shuffle the draw deck and initialize hands. self.draw_pile.shuffle() self.left.data.hand = Hand() self.right.data.hand = Hand()
def new_standard_deck(): deck = Hand() # Add two more agreements to the list of ranks for a total of 3. ranks = [AGREEMENT, AGREEMENT] ranks.extend(RANKS) for r in ranks: for s in DEFAULT_SUITS: deck.add(ExpeditionsCard(r, s)) return deck
def clear_trick(self): # Set the current trick to an empty hand... self.trick = Hand() self.led_suit = None # ...and set everyone's played card to None. for seat in self.seats: seat.data.card = None # Clear the layout as well. self.layout.clear()
def start_deal(self): # Set the trick counts to zero, appropriately for the mode. if self.mode == 4: self.ns.tricks = 0 self.ew.tricks = 0 else: self.west.data.tricks = 0 self.south.data.tricks = 0 self.east.data.tricks = 0 dealer_name = self.dealer.player_name self.bc_pre( "^R%s^~ (%s%s^~) gives the cards a good shuffle...\n" % (dealer_name, self.get_color_code(self.dealer), self.dealer)) self.new_deck() self.deck.shuffle() # Deal out five cards each. self.bc_pre("^R%s^~ deals five cards out to each of the players.\n" % dealer_name) for seat in self.seats: seat.data.hand = Hand() for i in range(5): for seat in self.seats: seat.data.hand.add(self.deck.discard()) # Clear the internal metadata about trumps. self.trump_suit = None # Sort the hakem's hand. self.hakem.data.hand = sorted_hand(self.hakem.data.hand) # Show the hakem their hand. if self.hakem.player: self.tell_pre(self.hakem.player, "Please choose a trump suit for this hand.\n") self.show_hand(self.hakem.player) # The hakem both chooses and, eventually, leads. self.turn = self.hakem self.layout.change_turn(self.hakem.data.who) # Shift into "choosing" mode. self.state.set("choosing")
def new_deal(self): dealer_name = self.dealer.player_name self.bc_pre( "^R%s^~ (%s%s^~) gives the cards a good shuffle...\n" % (dealer_name, self.get_color_code(self.dealer), self.dealer)) deck = new_deck() deck.shuffle() # Deal out all of the cards. We'll flip the last one; that determines # the trump suit for the hand. self.bc_pre("^R%s^~ deals the cards out to all the players.\n" % dealer_name) for seat in self.seats: seat.data.hand = Hand() for i in range(13): for seat in self.seats: seat.data.hand.add(deck.discard()) # Flip the dealer's last card; it determines the trump suit. last_card = self.dealer.data.hand[-1] self.bc_pre("^R%s^~ flips their last card; it is ^C%s^~.\n" % (dealer_name, card_to_str(last_card, LONG))) self.trump_suit = last_card.suit # Sort everyone's hands. for seat in self.seats: seat.data.hand = sorted_hand(seat.data.hand, self.trump_suit) # Show everyone their hands. self.show_hands() # Set the trick counts to zero. self.ns.tricks = 0 self.ew.tricks = 0
def new_deck(self): # In 4-player mode, it's a standard 52-card pack. if self.mode == 4: self.deck = new_deck() else: # If it's a short deck, 7-A are full; if a long deck, 3-A are. full_ranks = [ACE, KING, QUEEN, JACK, '10', '9', '8', '7', '6'] if self.short: short_rank = '5' else: full_ranks.extend(['5', '4', '3']) short_rank = '2' # Build the deck, full ranks first. self.deck = Hand() for suit in (CLUBS, DIAMONDS, HEARTS, SPADES): for rank in full_ranks: self.deck.add(PlayingCard(rank, suit)) # We only want three of the short rank. No hearts, because. for suit in (CLUBS, DIAMONDS, SPADES): self.deck.add(PlayingCard(short_rank, suit))
def new_deck(ace_high=True): deck = Hand() for r in RANKS: for s in SUITS: deck.add(PlayingCard(r, s, ace_high)) return deck