def test_Simulator_with_HandGenerator(self): """Test Simulator with HandGenerator.""" hands = Hands() hands.addHand(HoldEm.Hand.fromString("AC 2C")) hands.addHand(HoldEm.Hand.fromString("AH KH")) hands.addHand(HoldEm.HandGenerator(SlanskyHand['class1'])) hands.addHand(HoldEm.HandGenerator(SlanskyHand['class2'])) hands.addHand(HoldEm.HandGenerator(SlanskyHand['class3'])) hands.addHand(HoldEm.HandGenerator(SlanskyHand['class4'])) simulator = HoldEm.Simulator(predefined_hands=hands) simulator.simulate_games(number_of_games=10)
sys.exit(1) if options.verbose: print "Reading hands from %s..." % configFileName config = ConfigParser.SafeConfigParser() try: config.read(configFileName) except Exception, e: print "Error parsing configuration file: %s" % e sys.exit(1) handTypes = {} handTypes.update(SlanskyHand) predefined_hands = Hands() while True: hand_num = len(predefined_hands) + 1 sectionName = "hand%d" % hand_num try: # Set raw to True so we don't try to parse %'s items = config.items(sectionName, raw=True) except: break if options.verbose: print "Parsing hand %d..." % hand_num hg = HoldEm.HandGenerator() hg.setName(sectionName) for item in items: handstr, p = item percent = int(p.rstrip("%"))
def testLowRanking(self): """Test basic low hand ranking.""" hands = Hands() # # List in ascending order, so that if any hand compares greater than # a preceding hand, we know there is a problem hands.addHandsFromStrings([ # Wheel "5S 4C 3D 2C AS", # Ten-high "TS 9C 8D 7C 6S", # Jack-high "AS JS TS 7S 3S", # High-card Jack "JC TS 9D 6H 2C", # High-card King "KC TS 9D 6H 2C", # King-high "KS JS TS 7S 3S", # King-high "AS KC QD JC TS", # King-high "KS QS JS TS 9S", # Pair of aces "AS AC 8D 3C 2S", # Pair of queens "QS QC 8D 3C 2S", # Aces and eights "AS AC 8D 8C 2S", # Queens and Jacks "QS QC JD JC 2S", # Queens and Jacks, higher kicker "QS QC JD JC 3S", # Trip Aces "AS AC AD 8C 2S", # Trip Fours "QS 9D 4S 4H 4D", # Trip Tens "JC TS TC TD 2S", # Aces full of sevens "AS AC AD 7C 7S", # Aces full of eights "AS AC AD 8C 8S", # Kings full of jacks "KS KC KD JC JS", # Quad aces "AS AC AD AH 8S", # Quad aces, higher kicker "AS AC AD AH TS", # Quad threes "9S 3S 3C 3D 3H"]) ranks = [self.ranker.rankHand(hand) for hand in hands] for i in range(len(hands)): for j in range(len(hands)): if (i < j): self.assert_(ranks[i] < ranks[j], "!(%d) %s (%s) < (%d) %s (%s)" % ( i, hands[i], ranks[i], j, hands[j], ranks[j])) elif (i > j): self.assert_(ranks[i] > ranks[j], "!(%d) %s (%s) > (%d) %s (%s)" % ( i, hands[i], ranks[i], j, hands[j], ranks[j])) else: self.assertEqual(ranks[i], ranks[j], "(%d) %s (%s) != (%d) %s (%s)" % ( i, hands[i], ranks[i], j, hands[j], ranks[j]))
def main(argv=None): if argv is None: argv = sys.argv usage = "usage: %prog [<options>]" version = "%prog version 1.0" parser = OptionParser(usage) parser.add_option("-B", "--board", type="string", dest="board", metavar="cards", help="specify the flop") parser.add_option("-g", "--game", type="string", dest="game", default="holdem", help="game to simulate") parser.add_option("-H", "--hand", type="string", dest="hands", metavar="cards", action="append", help="add a hand") parser.add_option("-n", "--numGames", type="int", dest="numGames", default=100, help="number of games to simulate") parser.add_option("-N", "--numHands", type="int", dest="numHands", default=10, help="number of hands in play") parser.add_option("-p", "--showProgress", action="store_true", dest="showProgress", default=False, help="show progress") parser.add_option("-P", "--profile", type="string", dest="profile", default=None, help="enable profiling") parser.add_option("-q", "--quiet", action="store_true", dest="quiet", default=False, help="run quietly") parser.add_option("-v", "--verbose", action="store_true", dest="verbose", default=False, help="show results of each hand") (options, args) = parser.parse_args() game = { "holdem" : HoldEm.Game, "5cardstud" : FiveCardStud.Game, "fivecardstud" : FiveCardStud.Game, "5cardstudhilo" : FiveCardStud.HiLoGame, "fivecardstudhilo" : FiveCardStud.HiLoGame, "7cardstud" : SevenCardStud.Game, "sevencardstud" : SevenCardStud.Game, "7cardstudhilo" : SevenCardStud.HiLoGame, "sevencardstudhilo" : SevenCardStud.HiLoGame, "omaha" : Omaha.Game, "omahahilo" : Omaha.HiLoGame, } if game.has_key(options.game): GameClass = game[options.game] else: print "Unknown game type \"%s\"" % options.game print "Known games are:" for name in game.keys(): print "\t%s" % name sys.exit(1) maxHands = GameClass.getMaxHands() if options.numHands > maxHands: options.numHands = maxHands if options.verbose: print "Reducing number of hands to %d" % maxHands game = GameClass(numHands = options.numHands) HandClass = GameClass.getHandClass() if options.hands is not None: hands = Hands() for hand in options.hands: hands.addHand(HandClass.fromString(hand)) game.addHands(hands) if options.board: game.setBoard(Board.fromString(options.board)) if options.verbose: callback=showHandCallback print "Simulating %d games of %s" % (options.numGames, GameClass.gameName) print "%d Hands" % game.getNumHands(), if game.hands: print ": %s" % game.hands else: print if game.board: print "Board: %s" % game.board elif options.showProgress: callback=showProgressCallback else: callback=None cmd="game.simulateGames(options.numGames, callback=callback)" if options.profile: import cProfile if options.verbose: print "Profiling to file %s" % options.profile # Need to supply context here as run() will just use __main__ cProfile.runctx(cmd, globals(), locals(), filename=options.profile) else: eval(cmd) if options.showProgress: print if not options.quiet: print game.statsToString()
def testRanking(self): """Test hand ranking.""" hands = Hands() # # List in ascending order, so that if any hand compares greater than # a preceding hand, we know there is a problem hands.addHandsFromStrings([ # High-card Jack "JC TS 9D 6H 2C", # High-card King "KC TS 9D 6H 2C", # Pair of queens "QS QC 8D 3C 2S", # Pair of aces "AS AC 8D 3C 2S", # Queens and Jacks "QS QC JD JC 2S", # Queens and Jacks, better kicker "QS QC JD JC 3S", # Aces and eights "AS AC 8D 8C 2S", # Trip Fours "QS 9D 4S 4H 4D", # Trip Fours, higher kicker "QS TD 4S 4H 4D", # Trip Tens "JC TS TC TD 2S", # Trip Aces "AS AC AD 8C 2S", # Wheel "5S 4C 3D 2C AS", # Straight, ten-high "TS 9C 8D 7C 6S", # Straight, ace-high "AS KC QD JC TS", # Flush, king-high "KS JS TS 7S 3S", # Flush, ace-high "AS JS TS 7S 3S", # Kings full of jacks "KS KC KD JC JS", # Aces full of sevens "AS AC AD 7C 7S", # Aces full of eights "AS AC AD 8C 8S", # Quad threes "9S 3S 3C 3D 3H", # Quad aces "AS AC AD AH 8S", # Quad aces, better kicker "AS AC AD AH TS", # Straight flush "KS QS JS TS 9S", # Royal flush "AS KS QS JS TS"]) ranks = [self.ranker.rankHand(hand) for hand in hands] # Sanity check ranks for i, rank in enumerate(ranks): self.assertTrue(rank is not None, "Hand \"%s\" rank == None" % hands[i]) self.assertNotEqual(rank, 0, "Hand \"%s\" rank == 0" % hands[i]) # Make sure ranks are increasing for i in range(len(hands)): for j in range(len(hands)): if (i < j): self.assert_(ranks[i] < ranks[j], "!(%d) %s (%s) < (%d) %s (%s)" % ( i, hands[i], ranks[i], j, hands[j], ranks[j])) elif (i > j): self.assert_(ranks[i] > ranks[j], "!(%d) %s (%s) > (%d) %s (%s)" % ( i, hands[i], ranks[i], j, hands[j], ranks[j])) else: self.assertEqual(ranks[i], ranks[j], "(%d) %s (%s) != (%d) %s (%s)" % ( i, hands[i], ranks[i], j, hands[j], ranks[j]))