def draw(self, number=1): """Draws one or more cards from the top of the deck and returns it or them in a list. Arguments: number -- the number of cards to draw. """ if tR('deck'): print("drawing {} card".format(number)) if number > len(self._cards): raise EmptyDeckError("Ran out of cards - Too many players?") else: drawn_cards = self._cards[-number:] # Call reverse() to simulate cards being popped # off the top of the deck in order drawn_cards.reverse() self._cards = self._cards[0:-number] if tR('deck'): list_string = "" for dcard in drawn_cards: if list_string != "": list_string += " " list_string += dcard.name_string() print("drawn cards: {}".format(list_string)) return drawn_cards
def __init__(self, deck=None, numcards=0, namelist=None, cardlist=None, nocopy=False, direction=PokerHandDirection.HIGH): """Initializes a PokerHandBase instance. Arguments: deck -- the Deck instance to draw the cards from numcards -- an optional number of cards to draw immediately from the deck, if a deck is provided namelist -- a list of short names (e.g. "AS", "10C", "7H") to populate the hand cardlist -- a list of Card instances to populate the hand nocopy -- set to True to add the provided cardlist list as the internal list of cards, rather than a copy of it which is the default behavior. """ self._score = [] self._cards = [] self._deck = deck self.numcards = numcards self.direction = direction if numcards and deck and not namelist: self.draw(numcards) self._cards_changed() elif namelist: if tR("hand"): names_str = "" names = Counter() for name in namelist: if tR("hand"): if names_str != "": names_str += " " names_str += name if name in names: print("Duplicate card {} in hand so far:{}".format( name, names_str)) print("Problem here") names[name] += 1 self._cards.append(PokerCardBase(name=name)) self._cards_changed() if tR("hand"): print("PokerHandBase names: {}".format(names_str)) elif cardlist: if nocopy: self._cards = cardlist else: for card in cardlist: self._cards.append(card.copy()) self._cards_changed()
def __init__(self, rank=None, suit=None, index=None, name=None, baseCard=None, inBoard=False, hidden=True, position=None, isEmpty=False): self.inBoard = inBoard self.hidden = hidden # hidden == face down self.position = position self.isEmpty = isEmpty if baseCard is not None: index = baseCard._index if index is None: raise ValueError("baseCard._index is None") if rank is not None or suit is not None or name is not None: rank = suit = name = None if tR('card'): print("baseCard={},index={}, rank={}, suit={}, name={}".format( baseCard, index, rank, suit, name)) PokerCardBase.__init__(self, rank=rank, suit=suit, index=index, name=name)
def winProb(self, player=None, inclNumbers=True, direction=None): """ Winning probablity, given player's knwlege inclNumbers - include (beats of other_hands) """ if direction is None: direction = self.direction if direction is None: direction.PokerHandDirection.HIGH hands_comb = self.getHands(player=player) other_hands_comb = self.getOtherHands(player=player) best_hand = hands_comb.bestHand() nbetter, nequal, nworse = other_hands_comb.betEqWorse( best_hand) # list of XX tuples prob = float(nworse) / (nbetter + nequal + nworse) prob_tie = float(nequal) / (nbetter + nequal + nworse) prob_str = "{:.3f}({:.3f})".format(prob, prob_tie) if inclNumbers: prob_str += " (>,==,<: {}, {}, {})".format(nbetter, nequal, nworse) if tR('prob'): card_str = best_hand.simpleString(short=False, full=True) print("{} win prob: {}".format(card_str, prob_str)) return prob_str
def combs(self, cards, n_in_hand): """ create compact set of hand descriptions of combinations of cards taken n__in_hand at a time Returns numpy arrays of hand description arrays """ """ TBD self made combinations to minimize storage """ if len(cards) == n_in_hand: hands_desc = [self.cards2desc(cards) ] # Workaround for combination issue else: hand_combs = itertools.combinations(cards, n_in_hand) nhands = self.nComb(len(cards), n_in_hand) hands_desc = np.empty((nhands, self.nHDesc), dtype=np.uint8) i = 0 if tR('comb'): print("list(hand_combs):{}".format(hand_combs)) for cs in hand_combs: hands_desc[i] = self.cards2desc(cs) i += 1 return hands_desc
def __init__( self, table=None, # Table shuff=True, deck=None, gameName=None, nplay=None, # Default from game minBet=None, pot=None, direction=None, # Direction: 1 - high, 2 - low, 3 - high-low lowFlushStrait=None): ''' Constructor Deal ''' """ The game "rules the show" and can specify almost all the playing conditions """ if gameName == None: gameName = table.game.name self.gameName = gameName game = self.game = Games[gameName] game.setPokerSettings() # Setup basic settings """ deal the deck """ if deck == None: deck = PokerDeck() self.deck = deck if table is None: table = PokerTable(deck=deck) self.table = table if direction == None: direction = game.direction self.direction = direction if lowFlushStrait == None: lowFlushStrait = game.direction self.lowFlushStrait = lowFlushStrait if nplay == None: nplay = len(game.plays) self.nPlay = nplay self.playNum = 0 if minBet == None: minBet = 1 if pot == None: pot = 0 self.pot = pot self.players = [] # Players participating # Join each player to deal for player in table.players: self.joinDeal(player) """ place the board cards """ self.board = PokerBoard(self, self.deck, gameName=self.gameName) """ Deal out cards, one to player, until per-player number is reached """ for i in range(self.game.nPlayerCards): pos = i + 1 for player in table.players: cards = self.deck.draw(1) if len(cards) == 1: card = cards[0] if tR("deal"): print("draw:{}".format(card)) if card is not None: player.addCard(card) else: print("draw:{} None".format(card)) raise ValueError("drawing None is not expected") else: print("draw:{} of {} cards".format(card, len(cards))) raise ValueError("len(cards) is not 1:{}".format( len(cards)))
else: print("one is NOT set") if traceObj.trace('two'): print("two is set") else: print("two is NOT set") if traceObj.trace('three'): print("three is set") else: print("three is NOT set") print("\nUsing tR static funcgtion") from PyTrace import tR if tR('input'): print("input is set") else: print("input is NOT set") if tR('output'): print("output is set") else: print("output is NOT set") if tR('throughput'): print("throughput is set") else: print("throughput is NOT set") if tR('shotput'):