def write_table(): table = {} t = time.time() for c1, c2, c3, c4, c5 in combinations( product(STR_RANKS.keys(), STR_SUITS), 5): cset = CardSet([ Card("".join(c1)), Card("".join(c2)), Card("".join(c3)), Card("".join(c4)), Card("".join(c5)) ]) flop_eval = FlopEvaluator(cset) made = flop_eval.get_made() table[cset.get_bin()] = made.keys()[made.values().index(True)] with open('./lut/test.lut', 'w') as f: json.dump(table, f) print t, time.time(), time.time() - t
class FlopEvaluator: STFL = None # straight flush FL = None # flush ST = None # straight QUADS = None FH = None TRIPS = None TWOPAIR = None PAIR = None HIGH = None LUT = None def __init__(self, card_set=None): if card_set is None: self.card_set = CardSet() else: self.card_set = card_set def use_table(self, table): self.LUT = table def get_made(self): if self.LUT is not None: try: return self.LUT[u'%d' % self.card_set.get_bin()] except KeyError: pass self._calc_flush_or_straight() self._calc_multiples() made = { "Straight Flush": self.STFL, "Flush": self.FL, "Straight": self.ST, "Quads": self.QUADS, "Full House": self.FH, "Trips": self.TRIPS, "Two Pair": self.TWOPAIR, "Pair": self.PAIR, "High Card": self.HIGH, } return made.keys()[made.values().index(True)] def _calc_flush_or_straight(self): if self.STFL is not None: return if len(self.card_set) != 5: return self.card_set.sort_by_rank() straight_cur = STR_RANKS[self.card_set[0].rank] first_suit = self.card_set[0].suit for c in self.card_set[1:]: if self.ST is None and straight_cur == STR_RANKS[c.rank] + 1: straight_cur = STR_RANKS[c.rank] else: self.ST = False if c.suit != first_suit: self.FL = False if self.ST is None: self.ST = True if self.FL is None: self.FL = True if self.ST and self.FL: self.STFL = True self.ST = False self.FL = False else: self.STFL = False def _calc_multiples(self): if any((self.STFL, self.FL, self.ST)): self.QUADS = False self.FH = False self.TRIPS = False self.TWOPAIR = False self.PAIR = False self.HIGH = False return elif self.QUADS is not None: return ranks = self._rank_count() self.QUADS = ranks.values().count(4) == 1 self.TRIPS = ranks.values().count(3) == 1 self.TWOPAIR = ranks.values().count(2) == 2 self.PAIR = ranks.values().count(2) == 1 self.HIGH = ranks.values().count(1) == 5 if self.TRIPS and self.PAIR: self.FH = True self.TRIPS = False self.PAIR = False else: self.FH = False def _rank_count(self): ranks = {} for c in self.card_set: if c.rank in ranks: ranks[c.rank] += 1 else: ranks[c.rank] = 1 return ranks
cset = CardSet([ Card("".join(c1)), Card("".join(c2)), Card("".join(c3)), Card("".join(c4)), Card("".join(c5)) ]) flop_eval = FlopEvaluator(cset) made = flop_eval.get_made() table[cset.get_bin()] = made.keys()[made.values().index(True)] with open('./lut/test.lut', 'w') as f: json.dump(table, f) print t, time.time(), time.time() - t def read_table(): with open('./lut/test.lut', 'r') as f: return json.load(f) if __name__ == '__main__': c1 = Card('7c') c2 = Card('5c') c3 = Card('4c') c4 = Card('3c') c5 = Card('2s') cset = CardSet([c4, c2, c3, c1, c5]) table = read_table() print table[u'%d' % cset.get_bin()]
from itertools import product, combinations import basic_lut from card import Card from cardset import CardSet from evaluator import FlopEvaluator from tools import STR_RANKS, STR_SUITS c1 = Card('Ah') c2 = Card('Kh') c3 = Card('Qh') c4 = Card('Jh') c5 = Card('Th') cset = CardSet([c4, c2, c3, c1, c5]) print "{0:b}".format(cset.get_bin()) flop_eval = FlopEvaluator(cset) print cset print flop_eval.get_made() m = { "Straight Flush": 0, "Flush": 0, "Straight": 0, "Quads": 0, "Full House": 0, "Trips": 0, "Two Pair": 0, "Pair": 0,