def testDealHand(self): """Test basic dealing to a single hand.""" hand = Hand() deck = Deck() deck.shuffle() deck.deal(hand,5) self.assertEquals(len(hand), 5)
def testCreateHands(self): """Test creating multiple hands.""" deck = Deck() deck.shuffle() hands = deck.createHands(8, handClass=HoldEm.Hand) self.assertEquals(len(hands), 8) for hand in hands: self.assertEquals(len(hand), 2)
def testDealHands(self): """Test dealing to multiple hands.""" hands = [Hand(), Hand(), Hand(), Hand()] deck = Deck() deck.shuffle() deck.deal(hands, 5) for hand in hands: self.assertEquals(len(hand), 5)
def test_Simulator(self): """Test basic Simulator construction""" simulator = Simulator() self.assertIsNotNone(simulator) self.assertEqual(simulator.GAME_NAME, "Poker") simulator = Simulator(number_of_hands=10) self.assertIsNotNone(simulator) deck = Deck() hands = deck.createHands(9) simulator = Simulator(predefined_hands=hands) self.assertIsNotNone(simulator) self.assertEqual(hands, simulator.get_predefined_hands())
def test_record_game(self): """Test Stats.record_game() and Stats.reset()""" # Generate a result deck = Deck() hands = deck.createHands(9) ranker = Ranker() high_winners, high_rank = ranker.bestHand(hands) low_ranker = LowRanker() low_winners, low_rank = low_ranker.bestHand(hands) result = Result(hands=hands, high_winners=high_winners, winning_high_rank=high_rank, low_winners=low_winners, winning_low_rank=low_rank) stats = Stats() stats.record_game(result) self.assertEqual(stats.number_of_hands, 9) self.assertEqual(stats.number_of_games, 1) self.assertEqual(len(stats.high_winners), stats.number_of_hands) self.assertEqual(len(stats.low_winners), stats.number_of_hands) self.assertEqual(len(stats.scoops), stats.number_of_hands) # Figure out if we have a scooper if ((len(stats.high_winners) == 1) and (len(stats.low_winners) == 1) and (stats.high_winners[0] == stats.low_winners[0])): scooper = stats.low_winners[0] else: scooper = None for index in range(stats.number_of_hands): if index in high_winners: self.assertEqual(1, stats.high_winners[index]) else: self.assertEqual(0, stats.high_winners[index]) if index in low_winners: self.assertEqual(1, stats.low_winners[index]) else: self.assertEqual(0, stats.low_winners[index]) if (scooper is not None) and (scooper == index): self.assertEqual(1, stats.scoops[index]) else: self.assertEqual(0, stats.scoops[index]) # OK, now try reseting stats stats.reset() self.assertEqual(stats.number_of_games, 0) self.assertEqual(len(stats.high_winners), stats.number_of_hands) self.assertEqual(len(stats.low_winners), stats.number_of_hands) self.assertEqual(len(stats.scoops), stats.number_of_hands) for index in range(stats.number_of_hands): self.assertEqual(0, stats.high_winners[index]) self.assertEqual(0, stats.low_winners[index]) self.assertEqual(0, stats.scoops[index])
def test_Simulator_with_predefined(self): """Test Simulator with HoldEm hands and board""" deck = Deck() board = Board() deck.dealHands(board) hands = deck.createHands(9, handClass=HoldEm.Hand) simulator = HoldEm.Simulator(predefined_hands=hands, predefined_board=board) self.assertIsNotNone(simulator) result = simulator.simulate_game() self.assertIsNotNone(result) self.assertIsInstance(result, Result) stats = simulator.simulate_games(number_of_games=4) self.assertIsNotNone(stats) self.assertIsInstance(stats, Stats)
def main(argv=None): if argv is None: argv = sys.argv usage = "usage: %prog [<options>]" version = "%prog version 1.0" parser = OptionParser(usage=usage, version=version) parser.add_option("-n", "--numDeals", type="int", dest="numDeals", default=1000, help="number of deals to simulate (Default is 1000)") (options, args) = parser.parse_args() print "Testing for %d deals" % options.numDeals for rank1 in range(Eight, King): for rank2 in range(rank1 + 1, Ace): hand = HoldEm.Hand([Card((rank1, Clubs)), Card((rank2, Clubs))]) rank = HoldEmStartingHandRanker.rankHand(hand) # Build starting deck startingDeck = Deck() startingDeck.removeCards([hand[0], hand[1]]) dominatedCount = 0 for deal in range(options.numDeals): deck = startingDeck.copy() deck.shuffle() hands = deck.createHands(8, HoldEm.Hand) dominated = False for h in hands: if ((h[0].rank == hand[0].rank) or (h[0].rank == hand[1].rank) or (h[1].rank == hand[0].rank) or (h[1].rank == hand[1].rank)): # We have domination if (HoldEmStartingHandRanker.rankHand(h) > rank): # Dealt hand is better dominated = True if dominated: dominatedCount += 1 print "%s: %3.2f" % (hand, 100 * dominatedCount/options.numDeals)
def test_Result(self): """Test basic construction and operation of Result instance""" deck = Deck() hands = deck.createHands(9) ranker = Ranker() high_winners, high_rank = ranker.bestHand(hands) low_ranker = LowRanker() low_winners, low_rank = low_ranker.bestHand(hands) result = Result(hands=hands, high_winners=high_winners, winning_high_rank=high_rank, low_winners=low_winners, winning_low_rank=low_rank) self.assertIsNotNone(result) self.assertListEqual(high_winners, result.high_winners) self.assertEqual(high_rank, result.winning_high_rank) self.assertListEqual(low_winners, result.low_winners) self.assertEqual(low_rank, result.winning_low_rank) self.assertListEqual(hands, result.hands)
def main(argv=None): if argv is None: argv = sys.argv usage = "usage: %prog [<options>]" version = "%prog version 1.0" parser = OptionParser(usage=usage, version=version) parser.add_option("-n", "--numDeals", type="int", dest="numDeals", default=1000, help="number of deals to simulate (Default is 100)") parser.add_option("-N", "--numHands", type="int", dest="numHands", default=8, help="number of Hands (Default is 8)") parser.add_option("-p", "--showProgress", action="store_true", dest="showProgress", default=False, help="show progress") (options, args) = parser.parse_args() print "Testing for %d deals against %d hands" % (options.numDeals, options.numHands) print "Rank\tOther\tBetter" suit = Suit(Suit.CLUBS) for rank in range(Rank.FIVE, Rank.ACE): startingDeck = Deck() hand = HoldEm.Hand() startingDeck.dealCard(hand, Card((Rank(rank), suit))) startingDeck.dealCard(hand, Card((Rank(Rank.TWO), suit))) otherSuitedHands = 0 betterSuitedHands = 0 for i in xrange(options.numDeals): deck = startingDeck.copy() deck.shuffle() hands = deck.createHands(options.numHands, handClass=HoldEm.Hand) otherSuitedHand = 0 betterSuitedHand = 0 for h in hands: if h.suitCount(suit) == 2: otherSuitedHand = 1 if (h[0].rank > rank) or (h[1].rank > rank): betterSuitedHand = 1 otherSuitedHands += otherSuitedHand betterSuitedHands += betterSuitedHand print "%s\t%5.2f%%\t%5.2f%%" % (Rank(rank).longString(), percent(otherSuitedHands, options.numDeals), percent(betterSuitedHands, options.numDeals))
def evaluateHand(hand, numDeals, numHands): handRank = HoldEmStartingHandRanker.rankHand(hand) betterThan = [ 0 ] * numHands for deal in range(numDeals): deck = Deck() deck.removeCards(hand) deck.shuffle() hands = [] for h in range(numHands): hands.append(HoldEm.Hand()) deck.dealHands(hands) for h in range(numHands): rank = HoldEmStartingHandRanker.rankHand(hands[h]) if handRank > rank: betterThan[h] += 1 else: break return betterThan
def main(argv=None): if argv is None: argv = sys.argv usage = "usage: %prog [<options>] Card1 Card2 Card3 Card4" version = "%prog version 1.0" parser = OptionParser(usage=usage, version=version) parser.add_option("-n", "--numDeals", type="int", dest="numDeals", default=1000, help="number of deals to simulate (Default is 100)") parser.add_option("-N", "--numHands", type="int", dest="numHands", default=8, help="number of Hands (Default is 8)") parser.add_option("-p", "--showProgress", action="store_true", dest="showProgress", default=False, help="show progress") (options, args) = parser.parse_args() total = 0 pointOpeningHands = 0 pointRaisingHands = 0 pointReraisingHands = 0 maxPointOpeners = 0 openingHands = 0 for deal in xrange(options.numDeals): deck = Deck() deck.shuffle() hands = deck.createHands(options.numHands, handClass = Omaha.Hand) dealOpeners = 0 for h in hands: value = h.pointValue() total += value if value > 25: dealOpeners += 1 pointOpeningHands += 1 if value > 40: pointRaisingHands += 1 if value > 50: pointReraisingHands += 1 if h.openingHand(): openingHands += 1 if dealOpeners > maxPointOpeners: maxPointOpeners = dealOpeners # Update progress if so requested if options.showProgress and (deal % 20 == 0): sys.stdout.write(".") sys.stdout.flush() if options.showProgress: print totalHands = options.numDeals * options.numHands avg = total/totalHands print "Average point value: %d" % avg print "\tOpeners: %5.2f%% Raisers: %5.2f%% Reraisers: %5.2f%%" % ( percent(pointOpeningHands, totalHands), percent(pointRaisingHands, totalHands), percent(pointReraisingHands, totalHands)) print "\tOpeners in a deal: Average %4.1f Max %d" % ( 1.0 * pointOpeningHands / options.numDeals, maxPointOpeners) print "Opening Hands by Krieger method: %5.2f%%" % percent(openingHands, totalHands)
def testDesk(self): """Test basic deck construction and shuffling.""" deck = Deck() self.assertEqual(len(deck), 52, "Deck has %d cards!" % len(deck)) deck.shuffle() deck.reset()
def main(argv=None): if argv is None: argv = sys.argv usage = "usage: %prog [<options>]" version = "%prog version 1.0" parser = OptionParser(usage=usage, version=version) parser.add_option("-n", "--numDeals", type="int", dest="numDeals", default=1000, help="number of deals to simulate (Default is 100)") parser.add_option("-p", "--showProgress", action="store_true", dest="showProgress", default=False, help="show progress") parser.add_option("-s", "--suitedCards", type="int", dest="suitedCards", default=2, help="number of suited cards in hand (default is 2)") (options, args) = parser.parse_args() print "Testing for %d deals, %d suited cards in hand" % (options.numDeals, options.suitedCards) # Two-dimensional array. First dimension is number of cards of given suit # on flop, second dimension is number of cards of given suit on full board count = [] for index in range(4): count.append([0] * 6) # The suit we care about suit = Suit(Suit.CLUBS) # Build starting deck startingDeck = Deck() for index in range(options.suitedCards): startingDeck.removeCards(Card((Rank(Rank.ranks[index]), suit))) madeFlushes = [0] * 4 for deal in range(options.numDeals): deck = startingDeck.copy() deck.shuffle() board = Board() # Deal out and save flop deck.deal(board, 3) flop = board.copy() # Deal turn and river deck.deal(board, 2) # Find highest number of cards of same suit flushCount = 0 flopCount = flop.suitCount(suit) boardCount = board.suitCount(suit) count[flopCount][boardCount] += 1 if (options.suitedCards + boardCount) > 4: madeFlushes[flopCount] += 1 # Update progress if so requested if options.showProgress and (deal % 20 == 0): sys.stdout.write(".") sys.stdout.flush() if options.showProgress: print colwidth = 6 for col in ["Flopped", "Occur", "Final", "0", "1", "2", "3", "4", "5", "Flushes"]: print col.center(colwidth), print for index in range(len(count)): array = count[index] total = reduce(lambda a,b: a + b, array) print str(index).rjust(colwidth), print str(total).rjust(colwidth), print " ".ljust(colwidth), if total > 0: for subindex in range(len(array)): if ((subindex < index) or (subindex > index + 3)): print " ".ljust(colwidth), else: s = "%3.0f%%" % (100.0 * array[subindex]/total) print s.rjust(colwidth), s = "%3.0f%%" % (100.0 * madeFlushes[index]/total) print s, print totalFlushes = reduce(lambda a,b: a + b, madeFlushes) print "Total made flushes: %d (%3.0f%%)" % (totalFlushes, 100.0 * totalFlushes / options.numDeals)
def main(argv=None): if argv is None: argv = sys.argv usage = "usage: %prog [<options>]" version = "%prog version 1.0" parser = OptionParser(usage=usage, version=version) parser.add_option("-n", "--numDeals", type="int", dest="numDeals", default=1000, help="number of deals to simulate (Default is 100)") parser.add_option("-p", "--showProgress", action="store_true", dest="showProgress", default=False, help="show progress") (options, args) = parser.parse_args() print "Testing for %d deals" % options.numDeals lowDraw = 0 lowDrawCompleted = 0 floppedLow = 0 lowPossible = 0 backdoorLow = 0 for deal in range(options.numDeals): deck = Deck() deck.shuffle() board = Board() # Deal out flop and count low cards deck.deal(board, 3) flopLowCount = board.countEightOrLower() if flopLowCount == 2: lowDraw += 1 # Finish dealing turn and river deck.deal(board, 2) # Analyze lowCount = board.countEightOrLower() if lowCount > 2: lowPossible += 1 if flopLowCount == 3: # Flopped a low floppedLow += 1 elif flopLowCount == 2: # Completed a draw lowDrawCompleted += 1 else: # Backdoor low backdoorLow += 1 # Update progress if so requested if options.showProgress and (deal % 20 == 0): sys.stdout.write(".") sys.stdout.flush() if options.showProgress: print statStr = lambda name, value: "%-30s %5d (%5.2f%%)" % (name.capitalize() + ":", value, 100.0 * value / options.numDeals) print statStr("eight-low possible", lowPossible) print statStr("flopped low", floppedLow) print statStr("flopped low draw", lowDraw) print statStr("completed draw", lowDrawCompleted) print statStr("backdoor low", backdoorLow)