def simulate_games(self, number_of_games = 100, callback=None, callbackArg=None, stats=None): """Simulate a bunch of games with starting hands. Returns a array with number of wins for each hand. Returns a Stats instance with the statistics from the games. If a stats instance is passed in, the same one, augmented, will be returned. callback should be a function that takes the form: callback(game, result, *callbackarg) """ assertInstance(number_of_games, int) if stats is None: stats = Stats(number_of_hands = self.number_of_hands) while number_of_games > 0: result = self.simulate_game() stats.record_game(result) if callback is not None: args = [self, result] if callbackArg is not None: args.append(callbackArg) callback(*args) number_of_games -= 1 return stats
def setBoard(self, board): if not self.hasBoard(): raise HandHasNoBoardException() if board: assertInstance(board, Cards) self.deck.removeCards(board) self.board = board
def addHandGroup(self, string): """Add a group of hands as described by string. string should have one of two forms: K4 - all unsuited combinations of K4. K4s - all suited combinations of K4. """ assertInstance(string, str) if len(string) == 2: rank1 = Rank.fromString(string[0]) rank2 = Rank.fromString(string[1]) suited = False elif len(string) == 3: rank1 = Rank.fromString(string[0]) rank2 = Rank.fromString(string[1]) suitedChar = string[2] if suitedChar == 's': suited = True else: raise InvalidHandTypeException("Invalid suited character \"%s\"" % suitedChar) else: raise InvalidHandTypeException("Invalid hand string \"%s\"" % string) if rank1 == rank2: self.addPair(rank1) elif suited: self.addSuitedHands(rank1, rank2) else: self.addUnsuitedHands(rank1, rank2)
def combinations(self, n): """Generator function return all combinations of n cards (including community cards).""" assertInstance(n, int) cards = Cards(self) if self.board: cards.extend(self.board) return Cards.combinations(cards, n)
def __init__(self, number_of_seats=9, players=None): """Create a table with given number of seats. Seat array of players if given.""" assertInstance(number_of_seats, int) self.number_of_seats = number_of_seats # One extra seat for seat 0 which we don't use to keep # indexing simple. self.players = [ None ] * (number_of_seats + 1) if players is not None: self.seat_players(players) self.dealer = None
def simulateGames(self, numGames = 100, callback=None, callbackArg=None, resetStats = True): """Simulate a bunch of games with starting hands. Returns a array with number of wins for each hand.""" assertInstance(numGames, int) if self.getNumHands() == 0: raise PokerGameStateException("Zero hands defined.") if resetStats: self.resetStats() while self.gameNum < numGames: self.simulateGame() if callback is not None: if callbackArg: callback(self, callbackArg) else: callback(self)
def combinations(self, n): """Generator function return all combinations of n cards (including community cards).""" assertInstance(n, int) holeCards = self.getHoleCards() if n <= 2: for holeCombo in holeCards.combinations(n): yield holeCombo elif self.board: for holeCombo in holeCards.combinations(2): for boardCombo in self.board.combinations(n-2): combo = holeCombo.copy() combo.extend(boardCombo) yield combo else: raise NotEnoughCardsException("Cannout generated hand of %d cards without board." % n)
def __init__(self, numHands = 0, hands=None): assertInstance(numHands, int) self.deck = self.deckClass() if numHands and (numHands > self.getMaxHands()): raise TooManyHandsException self.numHands = numHands self.hands = Hands() self.lowHandsWin = (self.lowHandRankerClass != None) if self.lowHandRankerClass: self.lowHandRanker = self.lowHandRankerClass() self.highHandsWin = (self.highHandRankerClass != None) if self.highHandRankerClass: self.highHandRanker = self.highHandRankerClass() self.highWins = [] self.lowWins = [] self.scoops = [] if hands: self.addHands(hands)
def eq(self, other): """Return True of cards are identicial, including suit.""" assertInstance(other, Card) return (self.suit == other.suit) and (self.rank == other.rank)
def append(self, card): """Append while checking to be sure maxCards is not exceeded.""" if self.maxCards and (len(self) == self.maxCards): raise TooManyCardsException() assertInstance(card, Card) Cards.append(self, card)
def setBoard(self, cards): """Set board for hand. cards should be a Board object.""" assertInstance(cards, Cards) self.board = cards
def addHandGenerator(self, hg): assertInstance(hg, HandGenerator) self.hands.addHand(hg)
def addHand(self, hand): assertInstance(hand, Hand) self.addHands(hand)