def test_run_until_game_finish_when_one_player_is_left(self): uuids = [ "ruypwwoqwuwdnauiwpefsw", "sqmfwdkpcoagzqxpxnmxwm", "uxrdiwvctvilasinweqven" ] holecards = [[Card.from_str(s) for s in ss] for ss in [["C2", "C3"], ["HA", "CA"], ["D5", "H6"]]] game_state = restore_game_state(ThreePlayerGameStateSample.round_state) game_state = reduce( lambda state, item: attach_hole_card(state, item[0], item[1]), zip(uuids, holecards), game_state) sb_amount, ante = 5, 7 self.emu.set_game_rule(3, 10, sb_amount, ante) p1_acts = [("fold", 0), ("call", 10), ('call', 0), ('call', 10), ("fold", 0)] p2_acts = [] p3_acts = [("raise", 10)] players = [TestPlayer(acts) for acts in [p1_acts, p2_acts, p3_acts]] [ self.emu.register_player(uuid, player) for uuid, player in zip(uuids, players) ] game_state["table"].deck.deck.append(Card.from_str("C7")) game_state, events = self.emu.run_until_game_finish(game_state) self.eq("event_game_finish", events[-1]["type"]) self.eq(0, game_state["table"].seats.players[0].stack) self.eq(0, game_state["table"].seats.players[1].stack) self.eq(292, game_state["table"].seats.players[2].stack)
def generate_cards(self, hole_card, community_card): community_card_new = [card for card in community_card] original_deck_set = set(range(1, 53)) hole_card_set = set( [Card.from_str(card).to_id() for card in hole_card]) community_card_set = set( [Card.from_str(card).to_id() for card in community_card]) revealed_set = hole_card_set.union(community_card_set) cheat_deck = list(original_deck_set - revealed_set) opp_hole_card = [] while len(community_card_new) < 5: idx = rand.choice(cheat_deck) new_card = Card.from_id(idx).__str__() community_card_new.append(new_card) cheat_deck.remove(idx) while len(opp_hole_card) < 2: idx = rand.choice(cheat_deck) new_card = Card.from_id(idx).__str__() opp_hole_card.append(new_card) cheat_deck.remove(idx) community_card_new = [ Card.from_str(card) for card in community_card_new ] opp_hole_card = [Card.from_str(card) for card in opp_hole_card] return community_card_new, opp_hole_card
def estimate_win_rate(self, hole, community): hole = [Card.from_str(c).to_id() for c in hole] community = [Card.from_str(c).to_id() for c in community] if len(community) == 0: return win_rate_estimates.estimates[hole[0] - 1][hole[1] - 1] return fast_monte_carlo.estimate_win_rate(100, hole, community)
def test_run_until_game_finish_when_one_player_is_left(self): uuids = [ 'ruypwwoqwuwdnauiwpefsw', 'sqmfwdkpcoagzqxpxnmxwm', 'uxrdiwvctvilasinweqven' ] holecards = [[Card.from_str(s) for s in ss] for ss in [['2c', '3c'], ['Ah', 'Ac'], ['5d', '6d']]] game_state = restore_game_state(ThreePlayerGameStateSample.round_state) game_state = reduce( lambda state, item: attach_hole_card(state, item[0], item[1]), zip(uuids, holecards), game_state) sb_amount, ante = 5, 7 self.emu.set_game_rule(3, 10, sb_amount, ante) p1_acts = [('fold', 0), ('call', 10), ('call', 0), ('call', 10), ('fold', 0)] p2_acts = [] p3_acts = [('raise', 10)] players = [TestPlayer(acts) for acts in [p1_acts, p2_acts, p3_acts]] [ self.emu.register_player(uuid, player) for uuid, player in zip(uuids, players) ] game_state['table'].deck.deck.append(Card.from_str('7c')) game_state, events = self.emu.run_until_game_finish(game_state) self.eq('event_game_finish', events[-1]['type']) self.eq(0, game_state['table'].seats.players[0].stack) self.eq(0, game_state['table'].seats.players[1].stack) self.eq(292, game_state['table'].seats.players[2].stack)
def declare_action(self, valid_actions, hole_card, round_state, bot_state=None): rnd = random.randint(1, 101) c1 = Card.from_str(hole_card[0]) c2 = Card.from_str(hole_card[1]) own_stack, avg_stack = self.count_starting_stacks(round_state) blinds = min(own_stack, avg_stack) / self.bb # push/fold if round_state['street'] == 'preflop' and blinds < self.ins(14): return self.push_fold(valid_actions, round_state, blinds, c1, c2) # short stack if self.short or round_state[ 'street'] == 'preflop': # and blinds < self.ins(26): return self.play_short_stack(valid_actions, round_state, c1, c2) # monster if round_state['street'] == 'preflop': return self.play_monster(valid_actions, c1, c2) return self.check_or_fold(valid_actions)
def declare_action(self, valid_actions, hole_card, round_state, bot_state=None): rnd = random.randint(1, 101) c1 = Card.from_str(hole_card[0]) c2 = Card.from_str(hole_card[1]) own_stack, avg_stack = self.count_stacks(round_state) blinds = min(own_stack, avg_stack) / self.bb if round_state['street'] == 'preflop': pair = c1.rank == c2.rank suited = c1.suit == c2.suit if blinds < 14: return self.push_fold(valid_actions, round_state, blinds, c1, c2) is_kk_plus = pair and c1.rank > 12 is_low_k = min(c1.rank, c2.rank) == 13 is_hi_a = max(c1.rank, c2.rank) == 14 is_ak = is_low_k and is_hi_a is_aks = is_ak and suited if is_kk_plus or is_aks: # print('MONSTER', c1, c2) return self.raise_or_call(valid_actions, MAX) return self.check_or_fold(valid_actions)
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 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 getScore(holeCards, commCards): # =========================================== # # Using PyPokerEngine's HandEvaluator # =========================================== # if not isinstance(holeCards, list): holeCards = literal_eval(holeCards) if not isinstance(commCards, list): commCards = literal_eval(commCards) 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 raw_feed(self, hole, community, step): # step represent the number of community card: 0,3,4,5 assert (step in [0, 3, 4, 5]) hole_list = [] community_list = [] step_true = self.step_map[step] for card in hole: hole_list.append(Card.from_str(card)) for card in community: community_list.append(Card.from_str(card)) feature_vector = self.get_step_feature_vector(hole_list, community_list, step_true) self.step_list.append(feature_vector)
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 get_all_possible_opp_hole(self, hole_card, community_card): original_deck_set = set(range(1, 53)) hole_card_set = set( [Card.from_str(card).to_id() for card in hole_card]) community_card_set = set( [Card.from_str(card).to_id() for card in community_card]) revealed_set = hole_card_set.union(community_card_set) cheat_deck = list(original_deck_set - revealed_set) opp_possible_hole_card_id = self.combinations(cheat_deck, 2) opp_possible_hole_cards = [[ Card.from_id(id_pair[0]), Card.from_id(id_pair[1]) ] for id_pair in opp_possible_hole_card_id] return opp_possible_hole_cards
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_run_until_game_finish_when_final_round(self): uuids = ["ruypwwoqwuwdnauiwpefsw", "sqmfwdkpcoagzqxpxnmxwm", "uxrdiwvctvilasinweqven"] holecards = [[Card.from_str(s) for s in ss] for ss in [["C2","C3"],["HA","CA"],["D5","H6"]]] game_state = restore_game_state(ThreePlayerGameStateSample.round_state) game_state = reduce(lambda state, item: attach_hole_card(state, item[0], item[1]), zip(uuids, holecards), game_state) sb_amount, ante = 5, 7 self.emu.set_game_rule(3, 10, sb_amount, ante) [self.emu.register_player(uuid, FoldMan()) for uuid in uuids] game_state["table"].deck.deck.append(Card.from_str("C7")) game_state, events = self.emu.run_until_game_finish(game_state) self.eq("event_game_finish", events[-1]["type"]) self.eq(10, game_state["round_count"]) self.eq(35, game_state["table"].seats.players[0].stack) self.eq(0, game_state["table"].seats.players[1].stack) self.eq(265, game_state["table"].seats.players[2].stack)
def calculateHandValue(self, hole_cards, common_cards): properHoleCards = [] for c in hole_cards: properHoleCards.append(Card.from_str(c)) properCommunityCards = [] for c in common_cards: properCommunityCards.append(Card.from_str(c)) value = HandEvaluator.eval_hand(properHoleCards, properCommunityCards) # Massage the value to be [0-1] #adjvalue = (math.log(value,2)-14)/7 OLD FORMULA adjvalue = math.sqrt(math.sqrt(value))/37 #print(str(value) + " " + str(adjvalue)) return adjvalue
def __flop(self, state): for i in range(3): str_card = self.cardList[0][0] del self.cardList[0] createdCard = Card.from_str(str_card) state["table"].add_community_card(createdCard) return self.__forward_street(state)
def test_montecarlo_simulation(self): my_cards = U.gen_cards(['6d', '2d']) community = U.gen_cards(['5d', '9d', '6h', 'Kc']) mock_return = community + [Card.from_str('7d')] with patch('pypokerengine.utils.card_utils._fill_community_card', side_effect=[mock_return]): U._montecarlo_simulation(3, my_cards, community) U._fill_community_card.assert_called_with(community, used_card=my_cards + community) mock_return = [ U.gen_cards(a) for a in [['7d'], ['Kd', 'Kh', '8h', 'As']] ] with patch('pypokerengine.utils.card_utils._pick_unused_card', side_effect=mock_return): self.eq(1, U._montecarlo_simulation(3, my_cards, community)) U._pick_unused_card.assert_called_with(4, Any(list)) mock_return = [ U.gen_cards(a) for a in [['7s'], ['Kd', 'Kh', '8h', 'As']] ] with patch('pypokerengine.utils.card_utils._pick_unused_card', side_effect=mock_return): self.eq(0, U._montecarlo_simulation(3, my_cards, community)) U._pick_unused_card.assert_called_with(4, Any(list))
def test_run_until_game_finish_when_final_round(self): uuids = ["ruypwwoqwuwdnauiwpefsw", "sqmfwdkpcoagzqxpxnmxwm", "uxrdiwvctvilasinweqven"] holecards = [[Card.from_str(s) for s in ss] for ss in [["C2", "C3"], ["HA", "CA"], ["D5", "H6"]]] game_state = restore_game_state(ThreePlayerGameStateSample.round_state) game_state = reduce(lambda state, item: attach_hole_card(state, item[0], item[1]), zip(uuids, holecards), game_state) sb_amount, ante = 5, 7 self.emu.set_game_rule(3, 10, sb_amount, ante) [self.emu.register_player(uuid, FoldMan()) for uuid in uuids] game_state["table"].deck.deck.append(Card.from_str("C7")) game_state, events = self.emu.run_until_game_finish(game_state) self.eq("event_game_finish", events[-1]["type"]) self.eq(10, game_state["round_count"]) self.eq(35, game_state["table"].seats.players[0].stack) self.eq(0, game_state["table"].seats.players[1].stack) self.eq(265, game_state["table"].seats.players[2].stack)
def test_replace_community_card(self): game_state = restore_game_state(TwoPlayerSample.round_state) to_card = lambda s: Card.from_str(s) cards = [to_card(c) for c in ['SA', 'DA', 'CA', 'HA']] processed = replace_community_card(game_state, cards) self.eq(cards, processed["table"].get_community_card()) self.neq(cards, game_state["table"].get_community_card())
def test_montecarlo_simulation(self): my_cards = U.gen_cards(["D6", "D2"]) community = U.gen_cards(['D5', 'D9', 'H6', 'CK']) mock_return = community + [Card.from_str("D7")] with patch('pypokerengine.utils.card_utils._fill_community_card', side_effect=[mock_return]): U._montecarlo_simulation(3, my_cards, community) U._fill_community_card.assert_called_with(community, used_card=my_cards + community) mock_return = [ U.gen_cards(a) for a in [["D7"], ["DK", "HK", "H8", "SA"]] ] with patch('pypokerengine.utils.card_utils._pick_unused_card', side_effect=mock_return): self.eq(1, U._montecarlo_simulation(3, my_cards, community)) U._pick_unused_card.assert_called_with(4, Any(list)) mock_return = [ U.gen_cards(a) for a in [["S7"], ["DK", "HK", "H8", "SA"]] ] with patch('pypokerengine.utils.card_utils._pick_unused_card', side_effect=mock_return): self.eq(0, U._montecarlo_simulation(3, my_cards, community)) U._pick_unused_card.assert_called_with(4, Any(list))
def test_restore_game_state_three_players_game(self): restored = restore_game_state(ThreePlayerGameStateSample.round_state) table = restored['table'] players = restored['table'].seats.players self.eq(0, restored['next_player']) self.eq(2, restored['street']) self.eq(2, restored['round_count']) self.eq(5, restored['small_blind_amount']) self.eq(1, table.dealer_btn) self.eq(1, table.sb_pos()) self.eq(2, table.bb_pos()) self.eq(['Jh', '8c', '2d', '4h'], [str(card) for card in table.get_community_card()]) self._assert_deck(table.deck, [Card.from_str(s) for s in ['Jh', '8c', '2d', '4h']]) self.eq(3, len(players)) self._assert_player([ 'p1', 'ruypwwoqwuwdnauiwpefsw', [], 35, ThreePlayerGameStateSample.p1_round_action_histories, ThreePlayerGameStateSample.p1_action_histories, 0, 60 ], players[0]) self._assert_player([ 'p2', 'sqmfwdkpcoagzqxpxnmxwm', [], 0, ThreePlayerGameStateSample.p2_round_action_histories, ThreePlayerGameStateSample.p2_action_histories, 1, 50 ], players[1]) self._assert_player([ 'p3', 'uxrdiwvctvilasinweqven', [], 85, ThreePlayerGameStateSample.p3_round_action_histories, ThreePlayerGameStateSample.p3_action_histories, 0, 70 ], players[2])
def test_restore_game_state_three_players_game(self): restored = restore_game_state(ThreePlayerGameStateSample.round_state) table = restored["table"] players = restored["table"].seats.players self.eq(0, restored["next_player"]) self.eq(2, restored["street"]) self.eq(2, restored["round_count"]) self.eq(5, restored["small_blind_amount"]) self.eq(1, table.dealer_btn) self.eq(1, table.sb_pos()) self.eq(2, table.bb_pos()) self.eq(['HJ', 'C8', 'D2', 'H4'], [str(card) for card in table.get_community_card()]) self._assert_deck(table.deck, [Card.from_str(s) for s in ['HJ', 'C8', 'D2', 'H4']]) self.eq(3, len(players)) self._assert_player( ["p1", "ruypwwoqwuwdnauiwpefsw", [], 35, ThreePlayerGameStateSample.p1_round_action_histories, \ ThreePlayerGameStateSample.p1_action_histories, 0, 60], players[0]) self._assert_player( ["p2", "sqmfwdkpcoagzqxpxnmxwm", [], 0, ThreePlayerGameStateSample.p2_round_action_histories, \ ThreePlayerGameStateSample.p2_action_histories, 1, 50], players[1]) self._assert_player( ["p3", "uxrdiwvctvilasinweqven", [], 85, ThreePlayerGameStateSample.p3_round_action_histories, \ ThreePlayerGameStateSample.p3_action_histories, 0, 70], players[2])
def test_restore_game_state_two_players_game(self): restored = restore_game_state(TwoPlayerSample.round_state) table = restored['table'] players = restored['table'].seats.players self.eq(1, restored['next_player']) self.eq(2, restored['street']) self.eq(3, restored['round_count']) self.eq(5, restored['small_blind_amount']) self.eq(0, table.dealer_btn) self.eq(0, table.sb_pos()) self.eq(1, table.bb_pos()) self.eq(['5d', '9d', '6h', 'Kc'], [str(card) for card in table.get_community_card()]) self._assert_deck(table.deck, [Card.from_str(s) for s in ['5d', '9d', '6h', 'Kc']]) self.eq(2, len(players)) self._assert_player([ 'p1', 'tojrbxmkuzrarnniosuhct', [], 65, TwoPlayerSample.p1_round_action_histories, TwoPlayerSample.p1_action_histories, 0, 35 ], players[0]) self._assert_player([ 'p2', 'pwtwlmfciymjdoljkhagxa', [], 80, TwoPlayerSample.p2_round_action_histories, TwoPlayerSample.p2_action_histories, 0, 20 ], players[1])
def declare_action(self, valid_actions, hole_card, round_state, bot_state=None): rnd = random.randint(1,101) c1 = Card.from_str(hole_card[0]) c2 = Card.from_str(hole_card[1]) big = c1.rank + c2.rank >= 24 monster = c1.rank + c2.rank >= 26 pair = c1.rank == c2.rank big_pair = pair and c1.rank >= 12 if big or pair or rnd < self.random_percent: pot = self.count_pot(round_state) small_pot = pot < self.bb * self.small_stack_bb if small_pot or monster or big_pair: return self.raise_or_call(valid_actions, MAX) return self.check_or_fold(valid_actions)
def _restore_deck(str_exclude_cards): deck = Deck() exclude_ids = [Card.to_id(Card.from_str(s)) for s in str_exclude_cards] deck_cards = [ Card.from_id(cid) for cid in range(1, 53) if cid not in exclude_ids ] deck.deck = deck_cards return deck
def get_community_cards(self, round_state): cards = [] i = 0 while i < len(round_state['community_card']): c = Card.from_str(round_state['community_card'][i]) cards.append(c) i += 1 return cards
def __deal_holecard(self, deck, players): for player in players: holecard = [] for i in range(2): str_card = self.cardList[0][0] del self.cardList[0] createdCard = Card.from_str(str_card) holecard.append(createdCard) player.add_holecard(holecard)
def test_run_until_game_finish_when_one_player_is_left(self): uuids = ["ruypwwoqwuwdnauiwpefsw", "sqmfwdkpcoagzqxpxnmxwm", "uxrdiwvctvilasinweqven"] holecards = [[Card.from_str(s) for s in ss] for ss in [["C2","C3"],["HA","CA"],["D5","H6"]]] game_state = restore_game_state(ThreePlayerGameStateSample.round_state) game_state = reduce(lambda state, item: attach_hole_card(state, item[0], item[1]), zip(uuids, holecards), game_state) sb_amount, ante = 5, 7 self.emu.set_game_rule(3, 10, sb_amount, ante) p1_acts = [("fold",0), ("call", 10), ('call', 0), ('call', 10), ("fold",0)] p2_acts = [] p3_acts = [("raise", 10)] players = [TestPlayer(acts) for acts in [p1_acts, p2_acts, p3_acts]] [self.emu.register_player(uuid, player) for uuid, player in zip(uuids, players)] game_state["table"].deck.deck.append(Card.from_str("C7")) game_state, events = self.emu.run_until_game_finish(game_state) self.eq("event_game_finish", events[-1]["type"]) self.eq(0, game_state["table"].seats.players[0].stack) self.eq(0, game_state["table"].seats.players[1].stack) self.eq(292, game_state["table"].seats.players[2].stack)
def gen_deck(exclude_cards=None): deck_ids = range(1, 53) if exclude_cards: assert isinstance(exclude_cards, list) if isinstance(exclude_cards[0], str): exclude_cards = [Card.from_str(s) for s in exclude_cards] exclude_ids = [card.to_id() for card in exclude_cards] deck_ids = [i for i in deck_ids if not i in exclude_ids] return Deck(deck_ids)
def _restore_deck(str_exclude_cards): deck = Deck() exclude_ids = [Card.to_id(Card.from_str(s)) for s in str_exclude_cards] deck_cards = [ Card.from_id(cid) for cid in Deck.GetFullDeck() if cid not in exclude_ids ] deck.cards = deck_cards return deck
def declare_action(self, valid_actions, hole_card, round_state): try: if isActionTimed: start = time.time() if (round_state["street"] == "preflop"): win_rate = eval_pre_flop.eval_pre_flop(hole_card) if (win_rate < 36.0): # print("FOLD") return ("fold") elif (win_rate < 60): # print("CALL") return ("call") else: # print("RAISE") for i in valid_actions: if i["action"] == "raise": action = i["action"] return action return ("call") # If unable to raise uuid = 0 for p in round_state['seats']: if (p['name'] == 'Group02Player'): uuid = p['uuid'] cards = list(map(lambda x: Card.from_str(x), hole_card)) player = round_state['next_player'] game_state = get_game_state(round_state, cards, uuid) game = Game(cards, player, game_state, NUM_ROUNDS, valid_actions, round_state, self.weights) if isDebug: print("Getting action...") try: action = game.minimax(game_state, MINIMAX_DEPTH, hole_card, round_state['community_card']) except TypeError: for i in valid_actions: if i["action"] == "raise": action = i["action"] return action # action returned here is sent to the poker engine action = valid_actions[1]["action"] return action if isDebug: print("================Action selected...") if isActionTimed: end = time.time() print("..............Action selected in TIME: " + str((end - start) * 1000) + " ms") return action except: return 'call'
def test_attach_hole_card(self): game_state = restore_game_state(TwoPlayerSample.round_state) to_card = lambda s: Card.from_str(s) hole1, hole2 = [to_card(c) for c in ["SA", "DA"]], [to_card(c) for c in ["HK", "C2"]] processed1 = attach_hole_card(game_state, "tojrbxmkuzrarnniosuhct", hole1) processed2 = attach_hole_card(processed1, "pwtwlmfciymjdoljkhagxa", hole2) players = processed2["table"].seats.players self.eq(hole1, players[0].hole_card) self.eq(hole2, players[1].hole_card) self.eq([0,0], [len(p.hole_card) for p in game_state["table"].seats.players])
def test_run_until_round_finish_game_finish_detect(self): uuids = ["tojrbxmkuzrarnniosuhct", "pwtwlmfciymjdoljkhagxa"] holecards = [[Card.from_str(s) for s in ss] for ss in [["CA", "D2"], ["C8", "H5"]]] game_state = restore_game_state(TwoPlayerSample.round_state) game_state = reduce(lambda a,e: attach_hole_card(a, e[0], e[1]), zip(uuids, holecards), game_state) game_state = attach_hole_card_from_deck(game_state, "pwtwlmfciymjdoljkhagxa") self.emu.set_game_rule(2, 10, 5, 0) p1 = TestPlayer([("raise", 65)]) p2 = TestPlayer([("call", 15), ("call", 65)]) self.emu.register_player("tojrbxmkuzrarnniosuhct", p1) self.emu.register_player("pwtwlmfciymjdoljkhagxa", p2) game_state["table"].deck.deck.append(Card.from_str("C7")) game_state, events = self.emu.run_until_round_finish(game_state) self.eq("event_new_street", events[0]["type"]) self.eq("event_ask_player", events[1]["type"]) self.eq("event_ask_player", events[2]["type"]) self.eq("event_round_finish", events[3]["type"]) self.eq("event_game_finish", events[4]["type"]) self.eq(0, events[4]["players"][0]["stack"]) self.eq(200, events[4]["players"][1]["stack"])
def test_restore_game_state_two_players_game(self): restored = restore_game_state(TwoPlayerSample.round_state) table = restored["table"] players = restored["table"].seats.players self.eq(1, restored["next_player"]) self.eq(2, restored["street"]) self.eq(3, restored["round_count"]) self.eq(5, restored["small_blind_amount"]) self.eq(0, table.dealer_btn) self.eq(0, table.sb_pos()) self.eq(1, table.bb_pos()) self.eq(['D5', 'D9', 'H6', 'CK'], [str(card) for card in table.get_community_card()]) self._assert_deck(table.deck, [Card.from_str(s) for s in ['D5', 'D9', 'H6', 'CK']]) self.eq(2, len(players)) self._assert_player(["p1", "tojrbxmkuzrarnniosuhct", [], 65, TwoPlayerSample.p1_round_action_histories,\ TwoPlayerSample.p1_action_histories, 0, 35], players[0]) self._assert_player(["p2", "pwtwlmfciymjdoljkhagxa", [], 80, TwoPlayerSample.p2_round_action_histories,\ TwoPlayerSample.p2_action_histories, 0, 20], players[1])
def test_restore_game_state_three_players_game(self): restored = restore_game_state(ThreePlayerGameStateSample.round_state) table = restored["table"] players = restored["table"].seats.players self.eq(0, restored["next_player"]) self.eq(2, restored["street"]) self.eq(2, restored["round_count"]) self.eq(5, restored["small_blind_amount"]) self.eq(1, table.dealer_btn) self.eq(1, table.sb_pos()) self.eq(2, table.bb_pos()) self.eq(['HJ', 'C8', 'D2', 'H4'], [str(card) for card in table.get_community_card()]) self._assert_deck(table.deck, [Card.from_str(s) for s in ['HJ', 'C8', 'D2', 'H4']]) self.eq(3, len(players)) self._assert_player(["p1", "ruypwwoqwuwdnauiwpefsw", [], 35, ThreePlayerGameStateSample.p1_round_action_histories,\ ThreePlayerGameStateSample.p1_action_histories, 0, 60], players[0]) self._assert_player(["p2", "sqmfwdkpcoagzqxpxnmxwm", [], 0, ThreePlayerGameStateSample.p2_round_action_histories,\ ThreePlayerGameStateSample.p2_action_histories, 1, 50], players[1]) self._assert_player(["p3", "uxrdiwvctvilasinweqven", [], 85, ThreePlayerGameStateSample.p3_round_action_histories,\ ThreePlayerGameStateSample.p3_action_histories, 0, 70], players[2])
def gen_cards(cards_str): return [Card.from_str(s) for s in cards_str]
def _restore_community_card_on_table(table, card_data): for str_card in card_data: table.add_community_card(Card.from_str(str_card))
def test_from_str(self): self.eq(Card(Card.CLUB, 14), Card.from_str("CA")) self.eq(Card(Card.HEART, 10), Card.from_str("HT")) self.eq(Card(Card.SPADE, 9), Card.from_str("S9")) self.eq(Card(Card.DIAMOND, 12), Card.from_str("DQ"))
def _restore_deck(str_exclude_cards): deck = Deck() exclude_ids = [Card.to_id(Card.from_str(s)) for s in str_exclude_cards] deck_cards = [Card.from_id(cid) for cid in range(1, 53) if cid not in exclude_ids] deck.deck = deck_cards return deck