def comparePockets(p1, p2): d = deck.Deck() d.pull(p1[0].suit, p1[0].number) d.pull(p1[1].suit, p1[1].number) d.pull(p2[0].suit, p2[0].number) d.pull(p2[1].suit, p2[1].number) assert len(d.cards) == 48 seq = [4, 3, 2, 1, 0] numSeq = 1 score = 0 handOne = hand.Hand([], 1) handTwo = hand.Hand([], 2) while True: communityCards = getComboFromSequence(seq, d.cards) handOne.cards = p1 + communityCards handTwo.cards = p2 + communityCards winners = hand.winner([handOne, handTwo]) if len(winners) == 1: if winners[0] == 1: score += 1 else: score -= 1 seq = getNextCombinationSequence(seq, d.cards) numSeq += 1 if numSeq > 100000: numSeq = 0 print "Another 100000 combinations tested." if not seq: break return score
def endHand(self): theWinner = None maxWin = {} for p in self.players: maxWin[p.id] = p.pot if self.numInHand == 1: for p in self.players: if p.isInHand: winningIDs = [p.id] elif self.numInHand > 1: #NEED TO ADD REVEAL CODE HERE assert len(self.communityCards) == 5 hands = [] for p in self.players: if p.isInHand: hands.append(hand.Hand(p.pocket + self.communityCards, ID = p.id)) winningIDs = hand.winner(hands) else: assert False displayText = "" handName = "" for ID in winningIDs: p = self.getPlayerByID(ID) if len(displayText) == 0: displayText += p.name #If the winner revealed their hand include some text to show who won if p.hasRevealed: handName = " with "+hand.Hand(p.pocket + self.communityCards).handName() else: handName = "." token = " has won" else: displayText += ", "+p.name token = " have tied" displayText += token + " this hand" + handName splitPots = [] while len(winningIDs) > 0: #First, we need to create split pots for winners who only partially contributed lowestPotContribution = -10 for p in self.players: if (p.id in winningIDs) and (p.pot < lowestPotContribution or lowestPotContribution == -10): lowestPotContribution = p.pot #Make a new split pot potAmount = 0 for p in self.players: deduction = min(lowestPotContribution, p.pot) p.pot -= deduction potAmount += deduction splitIDs = winningIDs[:] splitPots.append((potAmount, splitIDs)) #Remove all players who are no longer inHand from the winningIDs for p in self.players: if p.id in winningIDs and p.pot <= 0: winningIDs.remove(p.id) #Any remaining money should return back to the original owner for p in self.players: p.bank += p.pot p.pot = 0 #Now we need to distribute the splitPots (or single pot if there was no all-in plays) for splitPot in splitPots: amount, ids = splitPot fraction = int(amount / len(ids)) remainder = amount % len(ids) for ID in ids: p = self.getPlayerByID(ID) p.bank += fraction if remainder > 0: p.bank += remainder remainder -= 1 #Remove bankrupt players from the game for p in self.players: if p.bank < 0: #Something went wrong and the player has negative money assert False elif p.bank == 0: self.removeFromGame(p) self.passToPlayers({state.State.CONTINUE_ONLY: True, state.State.CONTINUE_TEXT:displayText}) #Increment the dealer p = self.getPlayerByID(self.currentDealer) p.isDealer = False while True: self.currentDealer = (self.currentDealer + 1) % self.numPlayers p = self.getPlayerByID(self.currentDealer) assert p != None if p.isInGame == True: break p.isDealer = True
def comparePockets(p1, p2, c = [], analysis = False): d = deck.Deck() d.pull(p1[0].suit, p1[0].number) d.pull(p1[1].suit, p1[1].number) d.pull(p2[0].suit, p2[0].number) d.pull(p2[1].suit, p2[1].number) for ci in c: d.pull(ci.suit, ci.number) assert len(d.cards) == 48 - len(c) seq = [4, 3, 2, 1, 0] seq = seq[len(c):] numSeq = 1 wins = 0 ties = 0 losses = 0 if analysis: #Table is indexed by A's hand evaluations with an array storing counts of B's hand evalutations aTable = {} for i in range(9): aTable[i] = {} for l in range(9): aTable[i][l] = {} #Store win/tie/loss for m in range(3): aTable[i][l][m] = 0 handOne = hand.Hand([], 1) handTwo = hand.Hand([], 2) while True: communityCards = getComboFromSequence(seq, d.cards) + c handOne.cards = p1 + communityCards handTwo.cards = p2 + communityCards if analysis: aRank = handOne.evaluate()[0] bRank = handTwo.evaluate()[0] winners = hand.winner([handOne, handTwo]) if len(winners) == 1: if winners[0] == 1: wins += 1 if analysis: aTable[aRank][bRank][0] += 1 else: losses += 1 if analysis: aTable[aRank][bRank][2] += 1 else: ties += 1 if analysis: aTable[aRank][bRank][1] += 1 seq = getNextCombinationSequence(seq, d.cards) numSeq += 1 if numSeq > 100000: numSeq = 0 print "Another 100000 combinations tested." if not seq: break if analysis: return aTable return (wins, ties, losses)
def endHand(self): theWinner = None maxWin = {} for p in self.players: maxWin[p.id] = p.pot if self.numInHand == 1: for p in self.players: if p.isInHand: winningIDs = [p.id] elif self.numInHand > 1: #NEED TO ADD REVEAL CODE HERE assert len(self.communityCards) == 5 hands = [] for p in self.players: if p.isInHand: hands.append( hand.Hand(p.pocket + self.communityCards, ID=p.id)) winningIDs = hand.winner(hands) else: assert False displayText = "" handName = "" for ID in winningIDs: p = self.getPlayerByID(ID) if len(displayText) == 0: displayText += p.name #If the winner revealed their hand include some text to show who won if p.hasRevealed: handName = " with " + hand.Hand( p.pocket + self.communityCards).handName() else: handName = "." token = " has won" else: displayText += ", " + p.name token = " have tied" displayText += token + " this hand" + handName splitPots = [] while len(winningIDs) > 0: #First, we need to create split pots for winners who only partially contributed lowestPotContribution = -10 for p in self.players: if (p.id in winningIDs) and (p.pot < lowestPotContribution or lowestPotContribution == -10): lowestPotContribution = p.pot #Make a new split pot potAmount = 0 for p in self.players: deduction = min(lowestPotContribution, p.pot) p.pot -= deduction potAmount += deduction splitIDs = winningIDs[:] splitPots.append((potAmount, splitIDs)) #Remove all players who are no longer inHand from the winningIDs for p in self.players: if p.id in winningIDs and p.pot <= 0: winningIDs.remove(p.id) #Any remaining money should return back to the original owner for p in self.players: p.bank += p.pot p.pot = 0 #Now we need to distribute the splitPots (or single pot if there was no all-in plays) for splitPot in splitPots: amount, ids = splitPot fraction = int(amount / len(ids)) remainder = amount % len(ids) for ID in ids: p = self.getPlayerByID(ID) p.bank += fraction if remainder > 0: p.bank += remainder remainder -= 1 #Remove bankrupt players from the game for p in self.players: if p.bank < 0: #Something went wrong and the player has negative money assert False elif p.bank == 0: self.removeFromGame(p) self.passToPlayers({ state.State.CONTINUE_ONLY: True, state.State.CONTINUE_TEXT: displayText }) #Increment the dealer p = self.getPlayerByID(self.currentDealer) p.isDealer = False while True: self.currentDealer = (self.currentDealer + 1) % self.numPlayers p = self.getPlayerByID(self.currentDealer) assert p != None if p.isInGame == True: break p.isDealer = True