Esempio n. 1
0
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
Esempio n. 2
0
 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)
Esempio n. 3
0
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
Esempio n. 4
0
def gen_deck(exclude_cards=None):
    deck_ids = Deck.GetFullDeck()
    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 for card in exclude_cards]
        deck_ids = [i for i in deck_ids if not i in exclude_ids]
    return Deck(deck_ids)
Esempio n. 5
0
 def test_serialization(self):
     table = self.__setup_players_with_table()
     for card in table.deck.draw_cards(3):
         table.add_community_card(card)
     table.shift_dealer_btn()
     table.set_blind_pos(1, 2)
     serial = table.serialize()
     restored = Table.deserialize(serial)
     self.eq(table.dealer_btn, restored.dealer_btn)
     self.eq(Seats.serialize(table.seats), Seats.serialize(restored.seats))
     self.eq(Deck.serialize(table.deck), Deck.serialize(restored.deck))
     self.eq(table.get_community_card(), restored.get_community_card())
     self.eq(1, restored.sb_pos())
     self.eq(2, restored.bb_pos())
Esempio n. 6
0
    def test_everyone_agree_logic_regression(self):
        players = [Player("uuid%d" % i, 100) for i in range(4)]
        players[0].stack = 150
        players[1].stack = 150
        players[2].stack = 50
        players[3].stack = 50
        deck = Deck(cheat=True, cheat_card_ids=range(1, 53))
        table = Table(cheat_deck=deck)
        for player in players:
            table.seats.sitdown(player)
        table.dealer_btn = 3
        table.set_blind_pos(0, 1)

        state, _ = RoundManager.start_new_round(1, 5, 0, table)
        state, _ = RoundManager.apply_action(state, "raise", 15)
        state, _ = RoundManager.apply_action(state, "raise", 20)
        state, _ = RoundManager.apply_action(state, "raise", 25)
        state, _ = RoundManager.apply_action(state, "raise", 30)
        state, _ = RoundManager.apply_action(state, "raise", 50)
        state, _ = RoundManager.apply_action(state, "call", 50)
        state, _ = RoundManager.apply_action(state, "raise", 125)
        state, _ = RoundManager.apply_action(state, "call", 125)
        state, _ = RoundManager.apply_action(state, "fold", 0)
        state, _ = RoundManager.apply_action(state, "fold", 0)
        self.eq(Const.Street.FINISHED, state["street"])
Esempio n. 7
0
 def serialize(self):
     community_card = [card.to_id() for card in self._community_card]
     return [
         self.dealer_btn,
         Seats.serialize(self.seats),
         Deck.serialize(self.deck), community_card, self._blind_pos
     ]
Esempio n. 8
0
 def test_serialization(self):
   self.deck.draw_cards(5)
   self.deck.shuffle()
   serial = self.deck.serialize()
   restored = Deck.deserialize(serial)
   self.eq(self.deck.cheat, restored.cheat)
   self.eq(self.deck.deck, restored.deck)
    def test_update_game_when_game_finished(self):
        uuid_list = ["hoge", "fuga", "boo"]
        name_list = ["HOGE", "FUGA", "BOO"]
        players_info = Engine.gen_players_info(uuid_list, name_list)
        game_config = Engine.gen_game_config(5, 100, 10, 1)

        engine = Engine.EngineWrapper()
        engine.start_game(players_info, game_config)

        # Fix cards used in the game to make game result deterministic
        engine.current_state['table'].deck = Deck(cheat=True,
                                                  cheat_card_ids=range(6, 11))
        for idx, player in enumerate(
                engine.current_state['table'].seats.players):
            player.hole_card = [
                Card.from_id(idx * 2),
                Card.from_id(idx * 2 + 1)
            ]

        engine.update_game("raise", 99)
        engine.update_game("call", 99)
        original_msgs = engine.update_game("call", 99)
        hoge_msg, fuga_msg, boo_msg = [
            _simplify_messages(msgs)
            for msgs in _classify_messages_by_destination(
                original_msgs, uuid_list)
        ]
        self.eq([MSG_GU, MSG_SS, MSG_SS, MSG_SS, MSG_RF, MSG_GF], hoge_msg)
        self.eq([MSG_GU, MSG_SS, MSG_SS, MSG_SS, MSG_RF, MSG_GF], fuga_msg)
        self.eq([MSG_GU, MSG_SS, MSG_SS, MSG_SS, MSG_RF, MSG_GF], boo_msg)
Esempio n. 10
0
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
Esempio n. 11
0
 def __setup_table(self):
     players = [Player("uuid%d" % i, 100) for i in range(3)]
     deck = Deck(cheat=True, cheat_card_ids=range(1, 53))
     table = Table(cheat_deck=deck)
     for player in players: table.seats.sitdown(player)
     table.dealer_btn = 2
     table.set_blind_pos(0, 1)
     return table
Esempio n. 12
0
def get_best_hand(community_card_list):
    hand_strength_set = {0}
    deck = Deck()
    for card in community_card_list:
        deck.deck.remove(card)
    for possible_card_a in deck.deck:
        deck.deck.remove(possible_card_a)
        for possible_card_b in deck.deck:
            hand_strength = HandEvaluator.eval_hand([possible_card_a, possible_card_b], community_card_list)
            if hand_strength > max(hand_strength_set):
                nuts_cards = {(str(possible_card_a), str(possible_card_b))}
            elif hand_strength == max(hand_strength_set):
                if not (str(possible_card_b), str(possible_card_a)) in nuts_cards:
                    nuts_cards.add((str(possible_card_a), str(possible_card_b)))
            hand_strength_set.add(hand_strength)
        deck.deck = [possible_card_a] + deck.deck
    return nuts_cards
Esempio n. 13
0
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)
Esempio n. 14
0
 def deserialize(self, serial):
     deck = Deck.deserialize(serial[2])
     community_card = [Card.from_id(cid) for cid in serial[3]]
     table = self(cheat_deck=deck)
     table.dealer_btn = serial[0]
     table.seats = Seats.deserialize(serial[1])
     table._community_card = community_card
     table._blind_pos = serial[4]
     return table
Esempio n. 15
0
def generate_holecard_ranking(nb_entry):
    nb_simulation = 1000
    deck = Deck()
    ranking = []
    for _ in range(nb_entry):
        deck.shuffle()
        hole = deck.draw_cards(2)
        win_rate = estimate_hole_card_win_rate(nb_simulation, 10, hole)
        ranking.append((win_rate, hole))
        deck.restore()
    return sorted(ranking)[::-1]
Esempio n. 16
0
def subtask_hole_community_winrate(q, community_num, nb_sample):
    print "task started"
    nb_simulation = 1000
    deck = Deck()
    res = []
    for i in range(nb_sample):
        deck.shuffle()
        hole = deck.draw_cards(2)
        community = deck.draw_cards(community_num)
        win_rate = estimate_hole_card_win_rate(nb_simulation, 10, hole,
                                               community)
        res.append((win_rate, hole, community))
        deck.restore()
    q.put(res)
    print "task finished"
Esempio n. 17
0
    def __init__(self, num_players, num_rounds):
        self.round_count = 0
        self.community_from = 0
        self.cards = []
        for i in range(num_rounds):
            d = Deck()
            d.shuffle()
            cards = {'hole': [], 'community': d.draw_cards(5)}
            for j in range(num_players):
                cards['hole'].append(d.draw_cards(2))

            self.cards.append(cards)
 def __setup_deck(self, used_cards):
   deck = Deck()
   deck = self.__extract_used_card(deck, used_cards)
   deck.shuffle()
   return deck
Esempio n. 19
0
def _pick_unused_card(card_num, used_card):
    used = [card for card in used_card]
    unused = [card_id for card_id in Deck.GetFullDeck() if card_id not in used]
    choiced = random.sample(unused, card_num)
    return [Card.from_id(card_id) for card_id in choiced]
Esempio n. 20
0
 def __init__(self, cheat_deck=None):
     self.dealer_btn = 0
     self._blind_pos = None
     self.seats = Seats()
     self.deck = cheat_deck if cheat_deck else Deck()
     self._community_card = []
Esempio n. 21
0
 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)
Esempio n. 22
0
class DeckTest(BaseUnitTest):

  def setUp(self):
    self.deck = Deck()

  def test_draw_card(self):
    card = self.deck.draw_card()
    self.eq("SK", str(card))
    self.eq(51, self.deck.size())

  def test_draw_cards(self):
    cards = self.deck.draw_cards(3)
    self.eq("SJ", str(cards[2]))
    self.eq(49, self.deck.size())

  def test_restore(self):
    self.deck.draw_cards(5)
    self.deck.restore()
    self.eq(52, self.deck.size())

  def test_serialization(self):
    self.deck.draw_cards(5)
    self.deck.shuffle()
    serial = self.deck.serialize()
    restored = Deck.deserialize(serial)
    self.eq(self.deck.cheat, restored.cheat)
    self.eq(self.deck.deck, restored.deck)

  def test_cheat_draw(self):
    cards = [Card.from_id(cid) for cid in [12, 15, 17]]
    cheat = Deck(cheat=True, cheat_card_ids=[12, 15, 17])
    self.eq(cheat.draw_cards(3), cards)

  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 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 gen_card_set(community_num):
  deck = Deck()
  deck.shuffle()
  return deck.draw_cards(2), deck.draw_cards(5)[:community_num]
Esempio n. 24
0
 def test_gen_deck_without_some_card(self):
     expected = Deck(deck_ids=range(2, 52))
     exclude_obj = [Card.from_id(1), Card.from_id(52)]
     exclude_str = [str(card) for card in exclude_obj]
     self.eq(expected.serialize(), U.gen_deck(exclude_obj).serialize())
     self.eq(expected.serialize(), U.gen_deck(exclude_str).serialize())
Esempio n. 25
0
 def setUp(self):
   self.deck = Deck()
Esempio n. 26
0
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