def test_additonal_info(self):
     uuid = "hoge"
     self.assertIn(uuid, U.visualize_game_start(game_info, uuid))
     self.assertIn(uuid, U.visualize_round_start(2, ['C2', 'HQ'], seats, uuid))
     self.assertIn(uuid, U.visualize_street_start("preflop", "dummy", uuid))
     self.assertIn(uuid, U.visualize_declare_action(valid_actions, ['CA', 'DK'], round_state, uuid))
     self.assertIn(uuid, U.visualize_game_update(new_action, round_state, uuid))
     self.assertIn(uuid, U.visualize_round_result(winners, hand_info, round_state, uuid))
 def test_visualize_declare_action(self):
     s = U.visualize_declare_action(valid_actions, ['CA', 'DK'], round_state)
     self.assertIn("fold", s)
     self.assertIn("call: 0", s)
     self.assertIn("raise: [20, 95]", s)
     self.assertIn("CA", s)
     self.assertIn("DK", s)
     self.assertIn("round state", s)
Exemple #3
0
 def test_visualize_declare_action(self):
     s = U.visualize_declare_action(valid_actions, ['Ac', 'Kd'],
                                    round_state)
     self.assertIn('fold', s)
     self.assertIn('call: 0', s)
     self.assertIn('raise: [20, 95]', s)
     self.assertIn('Ac', s)
     self.assertIn('Kd', s)
     self.assertIn('round state', s)
Exemple #4
0
 def declare_action(self,
                    valid_actions,
                    hole_card,
                    round_state,
                    bot_state=None):
     print(
         U.visualize_declare_action(valid_actions, hole_card, round_state,
                                    self.uuid))
     action, amount = self._receive_action_from_console(valid_actions)
     return action, amount
Exemple #5
0
    def _visualize_action_log(self, task, value_function, experience):
        state, action, _next_state, _reward = experience
        players = state["table"].seats.players
        me = [p for p in players if p.uuid == "uuid-0"][0]
        me_pos = players.index(me)
        sb_amount = state["small_blind_amount"]
        valid_actions = ActionChecker.legal_actions(players, me_pos, sb_amount)
        hole = [str(card) for card in me.hole_card]
        round_state = DataEncoder.encode_round_state(state)
        visualized_state = visualize_declare_action(valid_actions, hole,
                                                    round_state)
        action_log = "Agent took action [ %s: %s (%s) ] at round %d" % (
            action["action"], action["amount"], action["name"],
            state["round_count"])
        actions = task.generate_possible_actions(state)
        act_vals = [
            value_function.predict_value(state, act) for act in actions
        ]
        act_names = [act["name"] for act in actions]
        action_value_log = "  => %s" % zip(act_names, act_vals)
        if self.show_weights:
            weights_log = ["** weights and features in detail **"]
            features_title = value_function.delegate.generate_features_title()
            features = value_function.delegate.construct_poker_features(
                "dummy", "dummy", round_state, me.uuid, hole,
                value_function.delegate.handicappers, blind_structure)
            w_for_acts = value_function.delegate.model.get_weights()[0].T
            bias = value_function.delegate.model.layers[0].b.get_value()
            weights_log.append("features : %s" % features)
            weights_log.append("bias : %s" % bias)
            for act in actions:
                weights_log.append("")
                act_val = act_vals[actions.index(act)]
                weights = w_for_acts[actions.index(act)].tolist()
                linear_comb = [
                    (f * w, f, w, features_title[idx])
                    for idx, (f, w) in enumerate(zip(features, weights))
                ]
                linear_comb.append((bias[actions.index(act)], 0, 0, "bias"))
                linear_comb = sorted(linear_comb,
                                     key=lambda item: abs(item[0]))[::-1]

                display_items = []
                for item in linear_comb:
                    display_items.append(item)
                    if abs(act_val - sum([tpl[0]
                                          for tpl in display_items])) < 0.01:
                        break

                weights_log.append("linear combination for %s :" % act["name"])
                for comb in display_items:
                    weights_log.append("    %s" % str(comb))
            action_value_log += "\n" + "\n".join(weights_log)
        return "\n".join([visualized_state, action_log, action_value_log])
Exemple #6
0
 def test_additonal_info(self):
     uuid = 'hoge'
     self.assertIn(uuid, U.visualize_game_start(game_info, uuid))
     self.assertIn(uuid,
                   U.visualize_round_start(2, ['2c', 'Qh'], seats, uuid))
     self.assertIn(uuid, U.visualize_street_start('preflop', 'dummy', uuid))
     self.assertIn(
         uuid,
         U.visualize_declare_action(valid_actions, ['Ac', 'Kd'],
                                    round_state, uuid))
     self.assertIn(uuid,
                   U.visualize_game_update(new_action, round_state, uuid))
     self.assertIn(
         uuid,
         U.visualize_round_result(winners, hand_info, round_state, uuid))
 def declare_action(self, valid_actions, hole_card, round_state):
   print(U.visualize_declare_action(valid_actions, hole_card, round_state, self.uuid))
   action, amount = self.__receive_action_from_console(valid_actions)
   return action, amount
Exemple #8
0
    def declare_action(self, valid_actions, player_cards, round_state):
        # print("*** declare_action")
        # print("**** U.visualize_declare_action with Action: ", valid_actions, ", Hole card: ", player_cards,
        #      ", Round state:", round_state)
        print(
            U.visualize_declare_action(valid_actions, player_cards,
                                       round_state, self.uuid))

        advicePrompt = input("Do you need advice on your action? (y/n)")
        if advicePrompt == "y":
            bet_amount = 0

            # process PREFLOP actions
            if round_state.get('street') == "preflop":
                if self.console_player_name == "":
                    self.console_player_name = input(
                        "Confirm console player name (e.g. 'John' as in main/config): "
                    )

                # find console player uuid
                for key in round_state:
                    if key == 'seats':
                        for item in round_state[key]:
                            if item['name'] == self.console_player_name:
                                console_uuid = item['uuid']
                print(
                    "Console player", self.console_player_name,
                    "participates in",
                    colored(len(round_state.get('seats')),
                            "red",
                            on_color=None,
                            attrs=None), "players game")

                # find console player preflop bet amount
                for key in round_state.get('action_histories').get('preflop'):
                    if key['uuid'] == console_uuid:
                        bet_amount += key['amount']
                print(
                    "Current pot value is",
                    colored(round_state.get('pot').get('main').get('amount'),
                            "red",
                            on_color=None,
                            attrs=None))
                print("Console player", self.console_player_name,
                      "bet amount is",
                      colored(bet_amount, "red", on_color=None, attrs=None))

                # define preflop state cards (called here player_cards)
                # for key, value in round_state.items():
                #    if key == 'community_card':
                #        player_cards.extend(value)
                print("Console player", self.console_player_name,
                      "flop_cards are",
                      colored(player_cards, "red", on_color=None, attrs=None))

                # change player_cards to numbered values for scoring
                for card in range(len(player_cards)):
                    player_cards[card] = ps.change_card_value(
                        player_cards[card])
                    # print("**** flop_cards after change ", player_cards)

                print(
                    "No advice calculation for",
                    colored(round_state.get('street'),
                            "blue",
                            on_color=None,
                            attrs=None), "action")

                if not self.advice_image_opened:
                    adviceImage = input(
                        "Do you want to open starting hand advice image? (y/n)"
                    )

                    if adviceImage == "y":
                        print("-- Minimize, do not close the image window --")
                        self.advice_image_opened = True
                        path = r'.\hands_3.jpg'
                        img = Image.open(path)
                        img.show()
                    else:
                        print('...no need for advice image')

            # process FLOP actions
            if round_state.get('street') == "flop":
                if self.console_player_name == "":
                    self.console_player_name = input(
                        "Enter console player name (e.g. 'c1'): ")

                # find console player uuid
                for key in round_state:
                    if key == 'seats':
                        for item in round_state[key]:
                            if item['name'] == self.console_player_name:
                                console_uuid = item['uuid']
                print(
                    "Console player", self.console_player_name,
                    "participates in",
                    colored(len(round_state.get('seats')),
                            "red",
                            on_color=None,
                            attrs=None), "players game")

                # find console player bet amount
                for key in round_state.get('action_histories').get('preflop'):
                    if key['uuid'] == console_uuid:
                        bet_amount += key['amount']
                for key in round_state.get('action_histories').get('flop'):
                    if key['uuid'] == console_uuid:
                        bet_amount += key['amount']
                print(
                    "Current pot value is",
                    colored(round_state.get('pot').get('main').get('amount'),
                            "red",
                            on_color=None,
                            attrs=None))
                print("Console player", self.console_player_name,
                      "bet amount is",
                      colored(bet_amount, "red", on_color=None, attrs=None))

                # define flop state cards (called player_cards)
                for key, value in round_state.items():
                    if key == 'community_card':
                        player_cards.extend(value)
                print("Console player", self.console_player_name,
                      "flop_cards are",
                      colored(player_cards, "red", on_color=None, attrs=None))

                # change cards to numbered values for scoring
                for card in range(len(player_cards)):
                    player_cards[card] = ps.change_card_value(
                        player_cards[card])

                # count flop combinations
                print(
                    "Advice calculation for",
                    colored(round_state.get('street'),
                            "blue",
                            on_color=None,
                            attrs=None), "action in progress...")
                ps.flop_combinations(player_cards)

                flopScore = ps.expected_value(player_cards, comb)
                current = df.loc[
                    df['value'] >= flopScore[0]].index[0] / 2598960 * 100
                future = df.loc[
                    df['value'] >= flopScore[1]].index[1] / 2598960 * 100
                print(
                    '**** Your current score value is %s (out of max 135) with average expected score value being %s'
                    % (int(current), int(future)))
                players = len(round_state.get('seats'))
                pot = round_state.get('pot').get('main').get('amount')
                price = bet_amount
                if current > future:
                    ps.should_call(players, current, pot, price)
                else:
                    ps.should_call(players, future, pot, price)

            # process TURN actions
            elif round_state.get('street') == "turn":
                if self.console_player_name == "":
                    self.console_player_name = input(
                        "Enter console player name (e.g. 'c1'): ")

                # find console player uuid
                for key in round_state:
                    if key == 'seats':
                        for item in round_state[key]:
                            if item['name'] == self.console_player_name:
                                console_uuid = item['uuid']
                print(
                    "Console player", self.console_player_name,
                    "participates in",
                    colored(len(round_state.get('seats')),
                            "red",
                            on_color=None,
                            attrs=None), "players game")

                # find console player current bet amount
                for key in round_state.get('action_histories').get('preflop'):
                    if key['uuid'] == console_uuid:
                        bet_amount += key['amount']
                for key in round_state.get('action_histories').get('flop'):
                    if key['uuid'] == console_uuid:
                        bet_amount += key['amount']
                print(
                    "Current pot value is",
                    colored(round_state.get('pot').get('main').get('amount'),
                            "red",
                            on_color=None,
                            attrs=None))
                print("Console player", self.console_player_name,
                      "current bet amount is",
                      colored(bet_amount, "red", on_color=None, attrs=None))

                # define turn cards (called player_cards)
                for key, value in round_state.items():
                    if key == 'community_card':
                        player_cards.extend(value)
                print("Console player", self.console_player_name,
                      "turn_cards are",
                      colored(player_cards, "red", on_color=None, attrs=None))

                # change cards to numbered values for scoring
                for card in range(len(player_cards)):
                    player_cards[card] = ps.change_card_value(
                        player_cards[card])

                # calculate turn combinations
                print(
                    "Advice calculation for",
                    colored(round_state.get('street'),
                            "blue",
                            on_color=None,
                            attrs=None), "action in progress...")
                ps.turn_combinations(player_cards)

                combTurn = ps.expected_value(player_cards, comb)
                current = df.loc[
                    df['value'] >= combTurn[0]].index[0] / 2598960 * 100
                future = df.loc[
                    df['value'] >= combTurn[1]].index[0] / 2598960 * 100
                print(
                    '**** Your current score value is %s (out of max 135) with average expected score value being %s'
                    % (int(current), int(future)))
                players = len(round_state.get('seats'))
                pot = round_state.get('pot').get('main').get('amount')
                price = bet_amount
                if current > future:
                    ps.should_call(players, current, pot, price)
                else:
                    ps.should_call(players, future, pot, price)

            # process RIVER actions
            elif round_state.get('street') == "river":
                if self.console_player_name == "":
                    self.console_player_name = input(
                        "Enter console player name (e.g. 'c1'): ")

                # find console player uuid
                for key in round_state:
                    if key == 'seats':
                        for item in round_state[key]:
                            if item['name'] == self.console_player_name:
                                console_uuid = item['uuid']
                print(
                    "Console player", self.console_player_name,
                    "participates in",
                    colored(len(round_state.get('seats')),
                            "red",
                            on_color=None,
                            attrs=None), "players game")

                # find console player current bet amount
                for key in round_state.get('action_histories').get('preflop'):
                    if key['uuid'] == console_uuid:
                        bet_amount += key['amount']
                for key in round_state.get('action_histories').get('flop'):
                    if key['uuid'] == console_uuid:
                        bet_amount += key['amount']
                print(
                    "Current pot value is",
                    colored(round_state.get('pot').get('main').get('amount'),
                            "red",
                            on_color=None,
                            attrs=None))
                print("Console player", self.console_player_name,
                      "current bet amount is",
                      colored(bet_amount, "red", on_color=None, attrs=None))

                # define river cards (called player_cards)
                for key, value in round_state.items():
                    if key == 'community_card':
                        player_cards.extend(value)
                print("Console player", self.console_player_name,
                      "river_cards are",
                      colored(player_cards, "red", on_color=None, attrs=None))

                # change cards to numbered values for scoring
                for card in range(len(player_cards)):
                    player_cards[card] = ps.change_card_value(
                        player_cards[card])

                # calculate turn combinations
                print(
                    "Advice calculation for",
                    colored(round_state.get('street'),
                            "blue",
                            on_color=None,
                            attrs=None), "action in progress...")

                combRiver = ps.expected_value(player_cards, comb)
                current = df.loc[
                    df['value'] >= combRiver[0]].index[0] / 2598960 * 100
                print('**** Your final score value is %s' % int(current))
                players = len(round_state.get('seats'))
                pot = round_state.get('pot').get('main').get('amount')
                price = bet_amount
                ps.should_call(players, current, pot, price)

        else:
            print("...no need for advice in this round")

        action, amount = self.__receive_action_from_console(valid_actions)
        return action, amount