def handPotential(self, nb_simulation, nb_player, hole_card, community_card=None): community_card = self._fill_community_card(community_card, used_card=hole_card + community_card) ahead = 0 tied = 1 behind = 2 HP = [[0, 0, 0], [0, 0, 0], [0, 0, 0]] HPTotal = [0, 0, 0] index = -1 for _ in range(100): unused_cards = self._pick_unused_card((nb_player - 1) * 2, hole_card + community_card) opponents_hole = [ unused_cards[2 * i:2 * i + 2] for i in range(nb_player - 1) ] opponents_score = [ HandEvaluator.eval_hand(hole, community_card) for hole in opponents_hole ] my_score = HandEvaluator.eval_hand(hole_card, community_card) if (my_score > opponents_score): index = 0 if (my_score == opponents_score): index = 1 if (my_score < opponents_score): index = 2
def HandStrength(weight, hole_card, community_card): """Evaluate hand strength = probability that our hand is the strongest hand based on given pre-determined weights""" No_of_times = 5 # numround = 35 -> run 3 times for running alone without timeout, 2 for running together Ahead = Tied = Behind = 1 ourrank = HandEvaluator.eval_hand(hole_card, community_card) # Consider all two card combinations of the remaining cards. unused_cards = _pick_unused_card(45, hole_card + community_card) # Run defined number of simulations while (No_of_times > 0): oppcard1 = random.choice(unused_cards) oppcard2 = random.choice(unused_cards) if (oppcard1 != oppcard2): # initial opponent hand value oppcard = [oppcard1, oppcard2] opprank = HandEvaluator.eval_hand(oppcard, community_card) # enemy card weight oppweight = (Map_169(oppcard1, oppcard2, weight) / 3364.0) # print(ourrank, opprank, oppweight) if (ourrank > opprank): Ahead += oppweight elif (ourrank == opprank): Tied += oppweight else: Behind += oppweight # < No_of_times = No_of_times - 1 handstrength = (Ahead + Tied / 2) / (Ahead + Tied + Behind) return handstrength
def get_winner(hand1, hand2): hand_evaluator = HandEvaluator() lala = [str(hand1[0]), str(hand1[1])] # print "P1 hand %s" % lala lolo = [str(hand2[0]), str(hand2[1])] # print "P2 hand %s" % lolo community_cards = list(map(Card.from_str, [])) # empty for now, we assume community card is same for both p1_handdeck = list(map(Card.from_str, lala)) p2_handdeck = list(map(Card.from_str, lolo)) p1_hand = hand_evaluator.eval_hand(p1_handdeck, community_cards) p2_hand = hand_evaluator.eval_hand(p2_handdeck, community_cards) # print "P1 hand evaluate score %s" % p1_hand # print "P2 hand evaluate score %s" % p2_hand """ Gets the winner between the two hands Evaluated by PyPokerEngine la... returns 1 if the first hand wins returns 2 if the second hand wins returns 0 if the hands are tied """ if p1_hand > p2_hand: return 1 elif p2_hand > p1_hand: return 2 elif p1_hand == p2_hand: return 0
def __scale_evaluation_value(self, value): hand = self.__analyze_hand(value) high = HandEvaluator._HandEvaluator__high_rank(value) low = HandEvaluator._HandEvaluator__low_rank(value) strength = HandEvaluator._HandEvaluator__mask_strength(value) >> 8 scale = 0 if strength == 0 else math.log(strength, 2) + 1 return int(scale * 28 + high + low)
def evaluate_hand(hole_card, community_card): assert len(hole_card)==2 and len(community_card)==5 hand_info = HandEvaluator.gen_hand_rank_info(hole_card, community_card) return { "hand": hand_info["hand"]["strength"], "strength": HandEvaluator.eval_hand(hole_card, community_card) }
def _montecarlo_simulation(nb_player, hole_card, community_card): community_card = _fill_community_card(community_card, used_card=hole_card+community_card) unused_cards = _pick_unused_card((nb_player-1)*2, hole_card + community_card) opponents_hole = [unused_cards[2*i:2*i+2] for i in range(nb_player-1)] opponents_score = [HandEvaluator.eval_hand(hole, community_card) for hole in opponents_hole] my_score = HandEvaluator.eval_hand(hole_card, community_card) return 1 if my_score >= max(opponents_score) else 0
def simulation_with_opponent(cards, community_cards, opponent_cards): community_card = _fill_community_card(community_cards, used_card=cards + community_cards) opponents_hole = opponent_cards opponents_score = HandEvaluator.eval_hand(opponents_hole, community_cards) my_score = HandEvaluator.eval_hand(cards, community_cards) return 1 if my_score >= (opponents_score) else 0
def hand_strength_with_belief(my_cards, community_cards, belief): overall_strength = 0 nb_simulation = 100 num_cards = len(belief['Cards']) for i in range(num_cards): opp_cards = belief['Cards'][i][0] prob = belief['Probability'][i] ahead = 0 behind = 0 tied = 0 for i in range(nb_simulation): community_cards_ = _fill_community_card( community_cards, used_card=my_cards + community_cards + opp_cards) opp_stength = HandEvaluator.eval_hand(opp_cards, community_cards_) my_strength = HandEvaluator.eval_hand(my_cards, community_cards_) if (my_strength > opp_stength): ahead = ahead + 1 elif (my_strength < opp_stength): behind = behind + 1 else: tied = tied + 1 overall_strength += prob * (ahead + tied / 2) / (ahead + tied + behind) return overall_strength
def _montecarlo_simulation(nb_player, hole_card, community_card): community_card = _fill_community_card(community_card, used_card=hole_card+community_card) unused_cards = _pick_unused_card((nb_player-1)*2, hole_card + community_card) opponents_hole = [unused_cards[2*i:2*i+2] for i in range(nb_player-1)] opponents_score = [HandEvaluator.eval_hand(hole, community_card) for hole in opponents_hole] my_score = HandEvaluator.eval_hand(hole_card, community_card) return 1 if my_score >= max(opponents_score) else 0
def evaluate_hand(hole_card, community_card): assert len(hole_card) == 2 and len(community_card) == 5 hand_info = HandEvaluator.gen_hand_rank_info(hole_card, community_card) return { 'hand': hand_info['hand']['strength'], 'strength': HandEvaluator.eval_hand(hole_card, community_card) }
def montecarlo_simulation(self, nb_player, hole_card, community_card): # Do a Monte Carlo simulation given the current state of the game by evaluating the hands # get all the community cards, include your own hole card, put it in the community_card array community_card = _fill_community_card(community_card, used_card=hole_card + community_card) # get all the unused cards based on the community card currently (deck - community_card) unused_cards = _pick_unused_card((nb_player - 1) * 2, hole_card + community_card) # get 100 possibilities of opponent's hole opponents_hole = [ unused_cards[2 * i:2 * i + 2] for i in range(nb_player - 1) ] # calculate the score of opponent's hand based on the possibilities of opponent's hole opponents_score = [ HandEvaluator.eval_hand(hole, community_card) for hole in opponents_hole ] # calculate your current hand's score my_score = HandEvaluator.eval_hand(hole_card, community_card) # if my current hand is better than all the possibilities of opponent's hand if my_score >= max(opponents_score): return 1 else: return 0
def evaluate_hand(hole_card, community_card): assert len(hole_card) == 2 and len(community_card) == 5 hand_info = HandEvaluator.gen_hand_rank_info(hole_card, community_card) return { "hand": hand_info["hand"]["strength"], "strength": HandEvaluator.eval_hand(hole_card, community_card) }
def montecarlo_simulation_hp(self, nb_player, hole_card, community_card, hp, total_hp, agent_rank, num_simulation): unused_cards = _pick_unused_card((nb_player - 1) * 2, hole_card + community_card) opponents_hole = [unused_cards[2 * i:2 * i + 2] for i in range(nb_player - 1)] # get rank of hole_card with the community_card that are faced up opponents_rank = [HandEvaluator.eval_hand(hole, community_card) for hole in opponents_hole] index = 'ahead' if agent_rank > max(opponents_rank): index = 'ahead' elif agent_rank == max(opponents_rank): index = 'tied' else: index = 'behind' total_hp[index] += 1 for i in range(num_simulation): all_community_cards = _fill_community_card(community_card, used_card=hole_card + community_card) agent_best_rank = HandEvaluator.eval_hand(hole_card, all_community_cards) opp_best_rank = HandEvaluator.eval_hand(hole_card, all_community_cards) if agent_best_rank > opp_best_rank: hp[index]['ahead'] += (1 / num_simulation) # normalize so that output of ppot and npot is from 0 to 1 elif agent_best_rank == opp_best_rank: hp[index]['tied'] += (1 / num_simulation) # normalize so that output of ppot and npot is from 0 to 1 else: hp[index]['behind'] += (1 / num_simulation) # normalize so that output of ppot and npot is from 0 to 1
def montecarlo_simulation(self, nb_player, hole_card, community_card): # Do a Monte Carlo simulation given the current state of the game by evaluating the hands community_card = _fill_community_card(community_card, used_card=hole_card + community_card) unused_cards = _pick_unused_card((nb_player - 1) * 2, hole_card + community_card) opponents_hole = [unused_cards[2 * i:2 * i + 2] for i in range(nb_player - 1)] opponents_score = [HandEvaluator.eval_hand(hole, community_card) for hole in opponents_hole] my_score = HandEvaluator.eval_hand(hole_card, community_card) return 1 if my_score >= max(opponents_score) else 0
def montecarlo_simulation(nb_player, hole_card, community_card): # Do a Monte Carlo simulation given the current state of the game by evaluating the hands community_card = _fill_community_card(community_card, used_card=hole_card + community_card) unused_cards = _pick_unused_card((nb_player - 1) * 2, hole_card + community_card) opponents_hole = [unused_cards[2 * i:2 * i + 2] for i in range(nb_player - 1)] opponents_score = [HandEvaluator.eval_hand(hole, community_card) for hole in opponents_hole] my_score = HandEvaluator.eval_hand(hole_card, community_card) return 1 if my_score >= max(opponents_score) else 0
def HandPotential(weight, hole_card, community_card): """Evaluate hand potential = potential that our hand can become better""" ahead = 0 tied = 1 behind = 2 No_of_times = 5 # numround = 35 -> run 2 times -> good for running alone and together without timeout but 3 has few errors # Hand potential array, each index represents ahead, tied, and behind. HP = [[0.01 for x in range(3)] for y in range(3)] # initialize to low value 0.01 HPTotal = [0 for x in range(3)] # initialize to 0 ourrank = HandEvaluator.eval_hand(hole_card, community_card) # Consider all two card combinations of the remaining cards for the opponent. community_card = _fill_community_card(community_card, used_card=hole_card + community_card) unused_cards = _pick_unused_card(45, hole_card + community_card) # Run defined number of simulations while (No_of_times > 0): oppcard1 = random.choice(unused_cards) oppcard2 = random.choice(unused_cards) turn = random.choice(unused_cards) river = random.choice(unused_cards) if (oppcard1 != oppcard2 != turn != river): # initial opponent hand value oppcard = [oppcard1, oppcard2] opprank = HandEvaluator.eval_hand(oppcard, community_card) # enemy card weight oppweight = (Map_169(oppcard1, oppcard2, weight) / 3364.0) if (ourrank > opprank): index = ahead elif (ourrank == opprank): index = tied else: index = behind # < HPTotal[index] += oppweight # Final 5-card board board = community_card board.append(turn) board.append(river) ourbest = HandEvaluator.eval_hand(hole_card, board) oppbest = HandEvaluator.eval_hand(oppcard, board) if (ourbest > oppbest): HP[index][ahead] += oppweight elif (ourbest == oppbest): HP[index][tied] += oppweight else: HP[index][behind] += oppweight # < No_of_times = No_of_times - 1 sumBehind = HP[behind][ahead] + HP[behind][tied] + HP[behind][behind] sumTied = HP[tied][ahead] + HP[tied][tied] + HP[tied][behind] sumAhead = HP[ahead][ahead] + HP[ahead][tied] + HP[ahead][behind] # Ppot: were behind but moved ahead. Ppot = (HP[behind][ahead] + HP[behind][tied] / 2 + HP[tied][ahead] / 2) / (sumBehind + sumTied / 2) # Npot: were ahead but fell behind. # Npot = (HP[ahead][behind]+HP[tied][behind]/2+HP[ahead][tied]/2)/ (sumAhead+sumTied/2) return Ppot
def positive_potential(hole_cards, comm_cards): index = 0 ahead = 0 tied = 1 behind = 2 HP = list((0, 0, 0, 0, 0, 0, 0, 0, 0)) HPTotal = list((0, 0, 0)) deck = PlayerUtil.reduceDeck(PlayerUtil.getNewDeck(), comm_cards) deck = PlayerUtil.reduceDeck(deck, hole_cards) oppPossbileCards = PlayerUtil.getAllCombins(deck, 2) hole_cards = [Card.from_str(card) for card in hole_cards] comm_cards = [Card.from_str(card) for card in comm_cards] ourrank = HandEvaluator.eval_hand(hole_cards, comm_cards) if len(comm_cards) == 3: n = 1 elif len(comm_cards) == 4: n = 1 else: n = 0 for opp_cards in oppPossbileCards: possible_board_deck = copy.deepcopy(deck) possible_board_deck = PlayerUtil.reduceDeck( possible_board_deck, opp_cards) possible_board_cards = PlayerUtil.getAllCombins( possible_board_deck, n) opp_cards = [Card.from_str(card) for card in opp_cards] opprank = HandEvaluator.eval_hand(opp_cards, comm_cards) if ourrank > opprank: index = 0 elif ourrank == opprank: index = 1 else: index = 2 HPTotal[index] += 1 for p_board_card in possible_board_cards: p_board_card = [Card.from_str(card) for card in p_board_card] comm_cards_copy = copy.deepcopy(comm_cards) comm_cards_copy = comm_cards_copy + p_board_card ourbest = HandEvaluator.eval_hand(hole_cards, comm_cards_copy) oppbest = HandEvaluator.eval_hand(opp_cards, comm_cards_copy) if ourbest > oppbest: HP[index * 3 + ahead] += 1 elif ourbest == oppbest: HP[index * 3 + tied] += 1 else: HP[index * 3 + behind] += 1 PPot = (HP[behind * 3 + ahead] + HP[behind * 3 + tied] / 2 + HP[tied * 3 + ahead] / 2) / (HPTotal[behind] + HPTotal[tied] / 2) return PPot
def _montecarlo_simulation(hole_card, community_card, range_opponent): community_card = _fill_community_card(community_card, used_card = hole_card + community_card) opponents_score = [HandEvaluator.eval_hand(hole, community_card) for hole in range_opponent] my_score = HandEvaluator.eval_hand(hole_card, community_card) score = [] for opp_score in opponents_score: if my_score > opp_score: score.append(1) else: score.append(0) return score
def compute_showdown_cards_index(self, hole_card): hand_evaluator = HandEvaluator() b = bin(hand_evaluator.eval_hand(hole_card, self.community_card)) hand_high_in_deci = int(b[2:-12], 2) l = len(b) - 16 index = 0 if len(b) > 18: index = int(math.log(int(b[0:l], 2), 2) + 1) * 2 if hand_high_in_deci > 7: index += 1 return index '''
def EHS_3_4(self, hole_card, community_card): p_win = 0 for iter in range(1000): community_card_new, opp_hole_card_new = self.generate_cards( hole_card, community_card) hole_card_new = [Card.from_str(card) for card in hole_card] p_score = HandEvaluator.eval_hand(hole_card_new, community_card_new) o_score = HandEvaluator.eval_hand(opp_hole_card_new, community_card_new) p_win += int(p_score > o_score) return p_win / 1000
def montecarlo_simulation(nb_player, hole_card, community_card, next_cards): len_to_com = 5 - len(community_card) community_card = community_card + next_cards[-len_to_com:] opponents_hole = [ next_cards[2 * i:2 * i + 2] for i in np.arange(nb_player - 1) ] opponents_score = [ HandEvaluator.eval_hand(hole, community_card) for hole in opponents_hole ] my_score = HandEvaluator.eval_hand(hole_card, community_card) return 1 if my_score >= max(opponents_score) else 0
def EHS_5(self, hole_card, community_card): opp_possible_hole_cards = self.get_all_possible_opp_hole( hole_card, community_card) p_win = 0 hole_card_new = [Card.from_str(card) for card in hole_card] community_card_new = [Card.from_str(card) for card in community_card] for opp_hole_card in opp_possible_hole_cards: p_score = HandEvaluator.eval_hand(hole_card_new, community_card_new) o_score = HandEvaluator.eval_hand(opp_hole_card, community_card_new) p_win += int(p_score > o_score) return p_win / len(opp_possible_hole_cards)
def evaluateShowdown(holeCards, commCards, oppCards, potAmt): # =========================================== # # Using PyPokerEngine's HandEvaluator # =========================================== # holeCards = [Card.from_str(card) for card in holeCards] commCards = [Card.from_str(card) for card in commCards] oppCards = [Card.from_str(card) for card in oppCards] myScore = HandEvaluator.eval_hand(holeCards, commCards) oppScore = HandEvaluator.eval_hand(oppCards, commCards) if myScore > oppScore: payout = potAmt elif myScore == oppScore: payout = potAmt / 2 else: payout = -potAmt return payout
def evaluateShowdown(holeCards, commCards, oppCards): # =========================================== # # Using PyPokerEngine's HandEvaluator # =========================================== # holeCards = [Card.from_str(card) for card in holeCards] commCards = [Card.from_str(card) for card in commCards] oppCards = [Card.from_str(card) for card in oppCards] myScore = HandEvaluator.eval_hand(holeCards, commCards) oppScore = HandEvaluator.eval_hand(oppCards, commCards) if myScore > oppScore: return 1 elif myScore == oppScore: return 0 else: return -1
def test_twopair2(self): community = [ Card(Card.DIAMOND, 4), Card(Card.SPADE, 8), Card(Card.HEART, 4), Card(Card.DIAMOND, 7), Card(Card.CLUB, 8) ] hole = [Card(Card.CLUB, 7), Card(Card.SPADE, 5)] bit = HandEvaluator.eval_hand(hole, community) self.eq(HandEvaluator.TWOPAIR, HandEvaluator._HandEvaluator__mask_hand_strength(bit)) self.eq(8, HandEvaluator._HandEvaluator__mask_hand_high_rank(bit)) self.eq(7, HandEvaluator._HandEvaluator__mask_hand_low_rank(bit))
def test_twopair(self): community = [ Card(Card.CLUB, 7), Card(Card.CLUB, 9), Card(Card.DIAMOND, 2), Card(Card.DIAMOND, 3), Card(Card.DIAMOND, 5) ] hole = [Card(Card.CLUB, 9), Card(Card.DIAMOND, 3)] bit = HandEvaluator.eval_hand(hole, community) self.eq(HandEvaluator.TWOPAIR, HandEvaluator._HandEvaluator__mask_hand_strength(bit)) self.eq(9, HandEvaluator._HandEvaluator__mask_hand_high_rank(bit)) self.eq(3, HandEvaluator._HandEvaluator__mask_hand_low_rank(bit))
def simplify_hand(hand, community_cards): """ Takes a hand (array of size two) and compresses the hand into simpler representation Also puts higher card in front i.e. Th 9h becomes T9s as both cards share the same suit Th 9s becomes T9o as both cards do not share the same suit (off-suit) Th Ts becomes TT (pair of tens) """ generated_hand = gen_cards(hand) card1 = generated_hand[0] card2 = generated_hand[1] # pair if card1.rank == card2.rank: # print "Pair %s" % str(card1)[1] + str(card2)[1] return str(card1)[1] + str(card2)[1] # return the rank 2-9, J-A instead of all ints hand = str(CFR.get_higher_rank(card1, card2))[1] # print "Higher rank card %s" % hand hand += str(card2)[1] if hand == str(card1)[1] else str(card1)[1] hand += str("s") if str(card1)[0] == str(card2)[0] else str("o") # print "final hand %s" % hand if len(community_cards) >= 3: strength = HandEvaluator.gen_hand_rank_info(generated_hand, gen_cards(community_cards)) hand += "_%s" %strength.get("hand")["strength"] return hand
def eval(self): """ Generates and checks only 1 card per meaningful outcome. Returns expected utility. i.e. Given a set of cards, get all the cards that have not been drawn. Check the outcome of the hand given the additional card. Outcomes can be ONEPAIR, FLUSH, etc. Only 1 permutation that gives that outcome is checked. """ # TODO: Optimise gen_hand_rank_info as there are currently alot of useless steps remaining_cards = gen_deck(exclude_cards=self.hole_cards + self.community_cards) n = remaining_cards.size() #Store the utility value given for that outcome memo = {} #Number of counts that generates the OUTCOME count = defaultdict(int) while remaining_cards.size() > 0: new_card = remaining_cards.draw_card() strength = HandEvaluator.gen_hand_rank_info( self.hole_cards, self.community_cards + [new_card])["hand"]["strength"] if strength not in memo: memo[strength] = DecisionNode( self.our_player, 0, self.hole_cards, self.community_cards + [new_card], self.pot, self.heuristic_weights, self.is_flop).eval() count[strength] += 1 #Return expected value return sum( [count[strength] * memo[strength] for strength in memo.keys()]) / n
def montecarlo_simulation_hs(self, nb_player, hole_card, community_card, ahead_tied_behind, agent_rank): community_card = _fill_community_card(community_card, used_card=hole_card + community_card) unused_cards = _pick_unused_card((nb_player - 1) * 2, hole_card + community_card) opponents_hole = [ unused_cards[2 * i:2 * i + 2] for i in range(nb_player - 1) ] # get rank of hole_card with the community_card that are faced up opponents_rank = [ HandEvaluator.eval_hand(hole, community_card) for hole in opponents_hole ] if agent_rank > max(opponents_rank): # if win add increment ahead ahead_tied_behind['ahead'] += 1 elif agent_rank == max(opponents_rank): # if tie increment tied ahead_tied_behind['tied'] += 1 else: # if lose increment behind ahead_tied_behind['behind'] += 1
def determine_hand_strength_dist(community_card_list): prob_dict = {} deck = Deck() for card in community_card_list: deck.deck.remove(card) for possible_card_a in deck.deck: for possible_card_b in deck.deck: hand_strength = HandEvaluator.gen_hand_rank_info([possible_card_a, possible_card_b], community_card_list)['hand']['strength'] if hand_strength == 'FLASH': hand_strength = 'FLUSH' if hand_strength == 'STRAIGHTFLASH': hand_strength = 'STRAIGHT FLUSH' if hand_strength in prob_dict: prob_dict[hand_strength] += 1 else: prob_dict[hand_strength] = 1 for key, value in prob_dict.items(): prob_dict[key] = value / (len(deck.deck) ** 2) sorted_probs = sorted(prob_dict.items(), key=lambda kv: kv[1], reverse=True) value_list = [tuple[0] for tuple in sorted_probs] prob_list = [tuple[1] for tuple in sorted_probs] return value_list, prob_list
def test_twopair(self): community = [ Card(Card.CLUB, 7), Card(Card.CLUB, 9), Card(Card.DIAMOND, 2), Card(Card.DIAMOND, 3), Card(Card.DIAMOND, 5) ] hole = [ Card(Card.CLUB, 9), Card(Card.DIAMOND, 3) ] bit = HandEvaluator.eval_hand(hole, community) self.eq(HandEvaluator.TWOPAIR, HandEvaluator._HandEvaluator__mask_hand_strength(bit)) self.eq(9, HandEvaluator._HandEvaluator__mask_hand_high_rank(bit)) self.eq(3, HandEvaluator._HandEvaluator__mask_hand_low_rank(bit))
def test_twopair2(self): community = [ Card(Card.DIAMOND, 4), Card(Card.SPADE, 8), Card(Card.HEART, 4), Card(Card.DIAMOND, 7), Card(Card.CLUB, 8) ] hole = [ Card(Card.CLUB, 7), Card(Card.SPADE, 5) ] bit = HandEvaluator.eval_hand(hole, community) self.eq(HandEvaluator.TWOPAIR, HandEvaluator._HandEvaluator__mask_hand_strength(bit)) self.eq(8, HandEvaluator._HandEvaluator__mask_hand_high_rank(bit)) self.eq(7, HandEvaluator._HandEvaluator__mask_hand_low_rank(bit))
def __find_winners_from(self, community_card, players): score_player = lambda player: HandEvaluator.eval_hand(player.hole_card, community_card) active_players = [player for player in players if player.is_active()] scores = [score_player(player) for player in active_players] best_score = max(scores) score_with_players = [(score, player) for score, player in zip(scores, active_players)] winners = [s_p[1] for s_p in score_with_players if s_p[0] == best_score] return winners
def getScore(holeCards, commCards): # =========================================== # # Using PyPokerEngine's HandEvaluator # =========================================== # holeCards = [Card.from_str(card) for card in holeCards] commCards = [Card.from_str(card) for card in commCards] myScore = HandEvaluator.eval_hand(holeCards, commCards) return myScore
def __find_winners_from(self, community_card, players): score_player = lambda player: HandEvaluator.eval_hand_true(player.hole_card, community_card) active_players = [player for player in players if player.is_active()] scores = [score_player(player) for player in active_players] best_score = max(scores) score_with_players = [(score, player) for score, player in zip(scores, active_players)] winners = [s_p[1] for s_p in score_with_players if s_p[0] == best_score] return winners
def __gen_hand_info_if_needed(self, players, community): active_players = [player for player in players if player.is_active()] gen_hand_info = lambda player: { "uuid": player.uuid, "hand": HandEvaluator.gen_hand_rank_info(player.hole_card, community) } return [] if len(active_players) == 1 else [ gen_hand_info(player) for player in active_players ]
def test_gen_hand_info(self): community = [ Card(Card.CLUB, 3), Card(Card.CLUB, 7), Card(Card.CLUB, 10), Card(Card.DIAMOND, 5), Card(Card.DIAMOND, 6) ] hole = [ Card(Card.CLUB, 9), Card(Card.DIAMOND, 2) ] info = HandEvaluator.gen_hand_rank_info(hole, community) self.eq("HIGHCARD", info["hand"]["strength"]) self.eq(9, info["hand"]["high"]) self.eq(2, info["hand"]["low"]) self.eq(9, info["hole"]["high"]) self.eq(2, info["hole"]["low"])
def test_flash(self): community = [ Card(Card.CLUB, 7), Card(Card.DIAMOND, 2), Card(Card.DIAMOND, 3), Card(Card.DIAMOND, 5 ), Card(Card.DIAMOND, 6) ] hole = [ Card(Card.CLUB, 4), Card(Card.DIAMOND, 5) ] bit = HandEvaluator.eval_hand(hole, community) self.eq(HandEvaluator.FLASH, HandEvaluator._HandEvaluator__mask_hand_strength(bit)) self.eq(6, HandEvaluator._HandEvaluator__mask_hand_high_rank(bit)) self.eq(0, HandEvaluator._HandEvaluator__mask_hand_low_rank(bit)) self.eq(5, HandEvaluator._HandEvaluator__mask_hole_high_rank(bit)) self.eq(4, HandEvaluator._HandEvaluator__mask_hole_low_rank(bit))
def test_straightflash(self): community = [ Card(Card.DIAMOND, 4), Card(Card.DIAMOND, 5), Card(Card.HEART, 11), Card(Card.HEART, 12), Card(Card.HEART, 13) ] hole = [ Card(Card.HEART, 10), Card(Card.HEART, 1) ] bit = HandEvaluator.eval_hand(hole, community) self.eq(HandEvaluator.STRAIGHTFLASH, HandEvaluator._HandEvaluator__mask_hand_strength(bit)) self.eq(10, HandEvaluator._HandEvaluator__mask_hand_high_rank(bit)) self.eq(0, HandEvaluator._HandEvaluator__mask_hand_low_rank(bit)) self.eq(14, HandEvaluator._HandEvaluator__mask_hole_high_rank(bit)) self.eq(10, HandEvaluator._HandEvaluator__mask_hole_low_rank(bit))
def test_eval_high_card(self): community = [ Card(Card.CLUB, 3), Card(Card.CLUB, 7), Card(Card.CLUB, 10), Card(Card.DIAMOND, 5), Card(Card.DIAMOND, 6) ] hole = [ Card(Card.CLUB, 9), Card(Card.DIAMOND, 2) ] bit = HandEvaluator.eval_hand(hole, community) self.eq(HandEvaluator.HIGHCARD, HandEvaluator._HandEvaluator__mask_hand_strength(bit)) self.eq(9, HandEvaluator._HandEvaluator__mask_hand_high_rank(bit)) self.eq(2, HandEvaluator._HandEvaluator__mask_hand_low_rank(bit)) self.eq(9, HandEvaluator._HandEvaluator__mask_hole_high_rank(bit)) self.eq(2, HandEvaluator._HandEvaluator__mask_hole_low_rank(bit))
def test_fullhouse(self): community = [ Card(Card.CLUB, 4), Card(Card.DIAMOND, 2), Card(Card.DIAMOND, 4), Card(Card.DIAMOND, 5 ), Card(Card.DIAMOND, 6) ] hole = [ Card(Card.CLUB, 4), Card(Card.DIAMOND, 5) ] bit = HandEvaluator.eval_hand(hole, community) self.eq(HandEvaluator.FULLHOUSE, HandEvaluator._HandEvaluator__mask_hand_strength(bit)) self.eq(4, HandEvaluator._HandEvaluator__mask_hand_high_rank(bit)) self.eq(5, HandEvaluator._HandEvaluator__mask_hand_low_rank(bit)) self.eq(5, HandEvaluator._HandEvaluator__mask_hole_high_rank(bit)) self.eq(4, HandEvaluator._HandEvaluator__mask_hole_low_rank(bit))
def test_threecard(self): community = [ Card(Card.CLUB, 3), Card(Card.CLUB, 7), Card(Card.DIAMOND, 3), Card(Card.DIAMOND, 5), Card(Card.DIAMOND, 6) ] hole = [ Card(Card.CLUB, 9), Card(Card.DIAMOND, 3) ] bit = HandEvaluator.eval_hand(hole, community) self.eq(HandEvaluator.THREECARD, HandEvaluator._HandEvaluator__mask_hand_strength(bit)) self.eq(3, HandEvaluator._HandEvaluator__mask_hand_high_rank(bit)) self.eq(0, HandEvaluator._HandEvaluator__mask_hand_low_rank(bit)) self.eq(9, HandEvaluator._HandEvaluator__mask_hole_high_rank(bit)) self.eq(3, HandEvaluator._HandEvaluator__mask_hole_low_rank(bit))
def test_fullhouse2(self): community = [ Card(Card.CLUB, 3), Card(Card.DIAMOND, 7), Card(Card.DIAMOND, 3), Card(Card.HEART, 3), Card(Card.HEART, 7) ] hole = [ Card(Card.SPADE, 8), Card(Card.SPADE, 7) ] bit = HandEvaluator.eval_hand(hole, community) self.eq(HandEvaluator.FULLHOUSE, HandEvaluator._HandEvaluator__mask_hand_strength(bit)) self.eq(7, HandEvaluator._HandEvaluator__mask_hand_high_rank(bit)) self.eq(3, HandEvaluator._HandEvaluator__mask_hand_low_rank(bit)) self.eq(8, HandEvaluator._HandEvaluator__mask_hole_high_rank(bit)) self.eq(7, HandEvaluator._HandEvaluator__mask_hole_low_rank(bit))
def test_fourcard(self): community = [ Card(Card.CLUB, 3), Card(Card.DIAMOND, 7), Card(Card.DIAMOND, 3), Card(Card.HEART, 3), Card(Card.HEART, 7) ] hole = [ Card(Card.SPADE, 3), Card(Card.SPADE, 8) ] bit = HandEvaluator.eval_hand(hole, community) self.eq(HandEvaluator.FOURCARD, HandEvaluator._HandEvaluator__mask_hand_strength(bit)) self.eq(3, HandEvaluator._HandEvaluator__mask_hand_high_rank(bit)) self.eq(0, HandEvaluator._HandEvaluator__mask_hand_low_rank(bit)) self.eq(8, HandEvaluator._HandEvaluator__mask_hole_high_rank(bit)) self.eq(3, HandEvaluator._HandEvaluator__mask_hole_low_rank(bit))
def simulation(self, player_num, holecard, community_card): deck = self.__setup_deck(holecard + community_card) my_score = HandEvaluator.eval_hand(holecard, community_card) others_hole = [deck.draw_cards(2) for _ in range(player_num-1)] others_score = [HandEvaluator.eval_hand(hole, community_card) for hole in others_hole] return my_score >= max(others_score)
def __gen_hand_info_if_needed(self, players, community): active_players = [player for player in players if player.is_active()] gen_hand_info = lambda player: { "uuid": player.uuid, "hand" : HandEvaluator.gen_hand_rank_info(player.hole_card, community) } return [] if len(active_players) == 1 else [gen_hand_info(player) for player in active_players]