def test_state_after_forward_to_river(self): state, _ = self.__start_round() state, _ = RoundManager.apply_action(state, "fold", 0) state, _ = RoundManager.apply_action(state, "call", 10) state, _ = RoundManager.apply_action(state, "call", 10) state, _ = RoundManager.apply_action(state, "call", 0) state, _ = RoundManager.apply_action(state, "call", 0) state, _ = RoundManager.apply_action(state, "call", 0) state, msgs = RoundManager.apply_action(state, "call", 0) self.eq(Const.Street.RIVER, state["street"]) self.eq([Card.from_id(cid) for cid in range(7,12)], state["table"].get_community_card()) self.eq(3, len(msgs)) fetch_player = lambda uuid: [p for p in state["table"].seats.players if p.uuid==uuid][0] self.true(all(map(lambda p: len(p.action_histories)==0, state["table"].seats.players))) self.eq(2, len(fetch_player("uuid0").round_action_histories[Const.Street.PREFLOP])) self.eq(2, len(fetch_player("uuid1").round_action_histories[Const.Street.PREFLOP])) self.eq(1, len(fetch_player("uuid2").round_action_histories[Const.Street.PREFLOP])) self.eq(1, len(fetch_player("uuid0").round_action_histories[Const.Street.FLOP])) self.eq(1, len(fetch_player("uuid1").round_action_histories[Const.Street.FLOP])) self.eq(0, len(fetch_player("uuid2").round_action_histories[Const.Street.FLOP])) self.eq(1, len(fetch_player("uuid0").round_action_histories[Const.Street.TURN])) self.eq(1, len(fetch_player("uuid1").round_action_histories[Const.Street.TURN])) self.eq(0, len(fetch_player("uuid2").round_action_histories[Const.Street.TURN])) self.assertIsNone(fetch_player("uuid0").round_action_histories[Const.Street.RIVER])
def deserialize(self, serial): hole = [Card.from_id(cid) for cid in serial[3]] player = self(serial[1], serial[2], serial[0]) if len(hole)!=0: player.add_holecard(hole) player.action_histories = serial[4] player.pay_info = PayInfo.deserialize(serial[5]) player.round_action_histories = serial[6] return player
def initialize_belief(self): used = [card.to_id() for card in self.my_cards + self.community_card] unused = [card_id for card_id in range(1, 53) if card_id not in used] Cards = [[Card.from_id(card1), Card.from_id(card2)] for card1, card2 in itertools.combinations(unused, 2)] start = time() Cards_Strength = list( map(lambda x: hand_strength(x, self.community_card, self.my_cards), Cards)) print('my', time() - start) self.belief['Cards'] = list(zip(Cards, Cards_Strength)) self.belief['Probability'] = np.ones(len(Cards)) / len(Cards) self.belief['Cards'].sort(key=lambda x: x[1])
def test_cheat_serialization(self): cards = [Card.from_id(cid) for cid in [12, 15, 17]] cheat = Deck(cheat=True, cheat_card_ids=[12, 15, 17]) serial = cheat.serialize() restored = Deck.deserialize(serial) self.eq(cheat.deck, restored.deck) self.eq(cheat.cheat, restored.cheat) self.eq(cheat.cheat_card_ids, restored.cheat_card_ids)
def _get_community_combination(used_card, card_for_generate_combination): used = [card.to_id() for card in used_card] unused = [card_id for card_id in range(1, 53) if card_id not in used] if card_for_generate_combination[0] == 0: for num in range(len(card_for_generate_combination)): card_for_generate_combination[num] = unused.pop(0) unused, choiced_to_id = return_num(len(card_for_generate_combination)-1, unused, card_for_generate_combination) return [Card.from_id(card_id) for card_id in choiced_to_id], unused
def __setup_player_for_serialization(self): player = Player("uuid", 50, "hoge") player.add_holecard([Card.from_id(cid) for cid in range(1,3)]) player.add_action_history(Const.Action.SMALL_BLIND, sb_amount=5) player.save_street_action_histories(Const.Street.PREFLOP) player.add_action_history(Const.Action.CALL, 10) player.add_action_history(Const.Action.RAISE, 10, 5) player.add_action_history(Const.Action.FOLD) player.pay_info.update_by_pay(15) player.pay_info.update_to_fold() return player
def __generate_input_for_cnn(self, hole, community): to_id = lambda card: card.to_id() gen_card_rank_vec = lambda card: [1 if r == card.rank else 0 for r in range(2,15)] gen_card_suit_vec = lambda card: [1 if s == card.suit else 0 for s in [Card.CLUB, Card.DIAMOND, Card.HEART, Card.SPADE]] gen_card_vec = lambda card: gen_card_rank_vec(card) + gen_card_suit_vec(card) gen_img = lambda zipped: [gen_card_vec(Card.from_id(card_id)) for card_id in zipped[0]+zipped[1]] wrap = lambda lst: np.array(lst) to_ndarray = lambda X: wrap([wrap(x) for x in X]) hole_ids = map(to_id, hole) community_ids = map(to_id, community) x = gen_img((hole_ids, community_ids)) X = to_ndarray(x) X = np.array([X.reshape(1, X.shape[0], X.shape[1])]) return X
def setup_table(): table = Table() players = [Player("uuid%d"%i, 100, "hoge") for i in range(3)] table.seats.players = players table.add_community_card(Card.from_id(1)) table.dealer_btn = 2 table.set_blind_pos(2, 0) p1, p2, p3 = table.seats.players p3.add_action_history(Const.Action.RAISE, 10, 5) p1.add_action_history(Const.Action.FOLD) p2.add_action_history(Const.Action.RAISE, 20, 10) p3.add_action_history(Const.Action.CALL, 20) [p.save_street_action_histories(Const.Street.PREFLOP) for p in [p1, p2, p3]] p3.add_action_history(Const.Action.CALL, 5) p2.add_action_history(Const.Action.RAISE, 5, 5) return table
def estimate_hole_card_win_rate(nb_simulation, nb_player, hole_card, community_card=None): if not community_card: community_card = [] need_cards = 5 - len(community_card) + 2 * (nb_player - 1) possible_cards = (map( lambda id: Card.from_id(id + 1), np.random.choice( a=52, size=need_cards, replace=False, )) for _ in np.arange(nb_simulation)) win_count = np.sum( montecarlo_simulation(nb_player, hole_card, community_card, list(next_cards)) for next_cards in possible_cards) return 1.0 * win_count / nb_simulation
def __fill_blank_card(self, holecard, community_): community = [Card.from_id(card.to_id()) for card in community_] # deep copy need_card = 5 - len(community) for card in self.draw_unknown_card(need_card, holecard + community): community.append(card) return community
def test_clear_holecard(self): self.player.add_holecard([Card.from_id(cid) for cid in range(1,3)]) self.player.clear_holecard() self.eq(0, len(self.player.hole_card))
def draw_unknown_card(self, draw_num, known_cards): unknown_card_id = [cardid for cardid in range(1, 53)\ if cardid not in [card.to_id() for card in known_cards]] return [Card.from_id(cardid) for cardid in random.sample(unknown_card_id, draw_num)]
def test_add_too_many_hole_card(self): self.player.add_holecard([Card.from_id(cid) for cid in range(1,4)])
def test_add_hole_card_twice(self): self.player.add_holecard([Card.from_id(cid) for cid in range(1,3)]) self.player.add_holecard([Card.from_id(cid) for cid in range(1,3)])
def __setup_52_cards(self): return [Card.from_id(cid) for cid in range(1,53)]
def test_add_single_hole_card(self): self.player.add_holecard([Card.from_id(1)])
def _pick_unused_card(card_num, used_card): used = [card.to_id() for card in used_card] unused = [card_id for card_id in range(1, 53) if card_id not in used] choiced = random.sample(unused, card_num) return [Card.from_id(card_id) for card_id in choiced]
def test_add_holecard(self): cards = [Card.from_id(cid) for cid in range(1,3)] self.player.add_holecard(cards) self.true(cards[0] in self.player.hole_card) self.true(cards[1] in self.player.hole_card)
def test_cheat_restore(self): cards = [Card.from_id(cid) for cid in [12, 15, 17]] cheat = Deck(cheat=True, cheat_card_ids=[12, 15, 17]) cheat.draw_cards(2) cheat.restore() self.eq(cheat.draw_cards(3), cards)
def setup_player(): player = setup_player_with_payinfo(0, "hoge", 50, PayInfo.FOLDED) player.add_holecard([Card.from_id(1), Card.from_id(2)]) player.add_action_history(Const.Action.CALL, 50) return player
def __setup_table(self): table = Table() table.set_blind_pos(0, 1) table.seats = self.__setup_seats() table.add_community_card(Card.from_id(1)) return table
def __init__(self, deck_ids=None, cheat=False, cheat_card_ids=[]): self.cheat = cheat self.cheat_card_ids = cheat_card_ids self.deck = [Card.from_id(cid) for cid in deck_ids] if deck_ids else self.__setup()
def __setup_cheat_deck(self): cards = [Card.from_id(cid) for cid in self.cheat_card_ids] return cards[::-1]
# # Data Processing # ## card id -> 1-hot vector # In[63]: import numpy as np from pypokerengine.engine.card import Card fetch_hole = lambda row: [row[key] for key in ['hole1_id', 'hole2_id']] fetch_community = lambda row: [row[key] for key in ['community1_id', 'community2_id', 'community3_id']] gen_card_rank_vec = lambda card: [1 if r == card.rank else 0 for r in range(2,15)] gen_card_suit_vec = lambda card: [1 if s == card.suit else 0 for s in [Card.CLUB, Card.DIAMOND, Card.HEART, Card.SPADE]] gen_card_vec = lambda card: gen_card_rank_vec(card) + gen_card_suit_vec(card) gen_img = lambda zipped: [gen_card_vec(Card.from_id(card_id)) for card_id in zipped[0]+zipped[1]] train_hole = train_df.apply(lambda row: fetch_hole(row), axis=1) train_community = train_df.apply(lambda row: fetch_community(row), axis=1) train_df["onehot"] = map(gen_img, zip(train_hole, train_community)) test_hole = test_df.apply(lambda row: fetch_hole(row), axis=1) test_community = test_df.apply(lambda row: fetch_community(row), axis=1) test_df["onehot"] = map(gen_img, zip(test_hole, test_community)) # ## Format data (pandas.df -> numpy.ndarray) # In[65]: wrap = lambda lst: np.array(lst)
def test_from_id(self): self.eq(Card.from_id(1), Card(Card.CLUB, 1)) self.eq(Card.from_id(29), Card(Card.HEART, 3)) self.eq(Card.from_id(40), Card(Card.SPADE, 1))
def __extract_used_card(self, deck, used_cards): deck.deck = [Card.from_id(card.to_id()) for card in deck.deck \ if not card.to_id() in [used_card.to_id() for used_card in used_cards]] return deck
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 __setup_players(self): hole = [Card.from_id(1), Card.from_id(2)] players = [self.__setup_player() for _ in range(3)] players[1].add_holecard(hole) return players
def test_deal_holecard(self): state, _ = self.__start_round() players = state["table"].seats.players self.eq([Card.from_id(1), Card.from_id(2)], players[0].hole_card) self.eq([Card.from_id(3), Card.from_id(4)], players[1].hole_card)