Example #1
0
 def test_puts_blinds_to_pot(self):
     assert self.deal.pot == 0
     self.deal.deal_preflop({
         'John': [Card('Ad'), Card('Kd')],
         'Phil': [Card('As'), Card('5c')]
     })
     assert self.deal.pot == 3
Example #2
0
 def test_raises_error_if_player_acts_out_of_turn(self):
     self.deal.deal_preflop({
         'John': [Card('Ad'), Card('Kd')],
         'Phil': [Card('As'), Card('5c')]
     })
     with raises(ActOutOfTurn):
         self.phil.call(self.deal)
Example #3
0
 def test_raises_exception_if_all_players_havent_acted(self):
     self.john.call(self.deal)
     self.phil.check(self.deal)
     self.deal.deal_flop([Card('6s'), Card('7s'), Card('Ks')])
     self.john.check(self.deal)
     self.phil.check(self.deal)
     self.deal.deal_turn([Card('Kc')])
     with raises(PlayersNotActed):
         self.deal.deal_river([Card('2s')])
Example #4
0
    def pick_card(self, data):
        candidate_cards = data['self']['candidateCards']
        cards = data['self']['cards']
        self.my_hand_cards = []
        for card_str in cards:
            card = Card(card_str)
            self.my_hand_cards.append(card)
        message = "My Cards:{}".format(self.my_hand_cards)
        self.system_log.show_message(message)
        card_index = 0
        message = "Pick Card Event Content:{}".format(data)
        self.system_log.show_message(message)
        message = "Candidate Cards:{}".format(candidate_cards)
        self.system_log.show_message(message)
        self.system_log.save_logs(message)
        message = "Pick Card:{}".format(candidate_cards[card_index])
        self.system_log.show_message(message)
        self.system_log.save_logs(message)

        curPlayers = []
        for player_name in data['roundPlayers']:
            if self.player_name == player_name:
                break
            else:
                curPlayers.append(self.player_index[player_name])

        card_scores = [0] * len(candidate_cards)
        scores = [self.players[i]["score"] for i in range(4)]
        score_cards = [self.players[i]["scoreCards"] for i in range(4)]

        sampleN = 0
        endTime = time.time() + self.maxTime
        while time.time() < endTime:

            samplePlayers = self.gen_sample()

            for i in range(len(candidate_cards)):
                card_scores[i] += self.simulator.run(curPlayers, \
                                            self.cur_board, self.heart_broken, \
                                            samplePlayers, Card(candidate_cards[i]), \
                                            self.player_index[self.player_name], scores, \
                                            score_cards, self.is_card_exposed) # do simulator
            sampleN += 1

        message = "[MonteCarloBot] #samples: {}".format(sampleN)
        self.system_log.show_message(message)
        self.system_log.save_logs(message)

        for i in range(len(card_scores)):
            if card_scores[card_index] < card_scores[i]:
                card_index = i

        message = "[MonteCarloBot] card scores: {}".format(card_scores)
        self.system_log.show_message(message)
        self.system_log.save_logs(message)

        return candidate_cards[card_index]
Example #5
0
 def test_all_players_acted(self):
     self.deal.deal_preflop({
         'John': [Card('Ad'), Card('Kd')],
         'Phil': [Card('As'), Card('5c')]
     })
     self.john.call(self.deal)
     assert not self.deal.all_players_acted
     self.phil.check(self.deal)
     assert self.deal.all_players_acted
Example #6
0
 def _decide(self, player_id):
     # this candidate_cards is list<Card>
     candidate_cards = self._get_candidate_cards(
         self.hand_cards_of_players[player_id])
     choosen_card = candidate_cards[0]
     if not self.cur_board:
         choosen_card = functools.reduce(
             lambda c1, c2: c1
             if c1.value < c2.value else c2, candidate_cards)
     else:
         lead_card = functools.reduce(
             lambda c1, c2: c1
             if c1.suit == c2.suit and c1.value > c2.value else c2,
             self.cur_board)
         for card in candidate_cards:
             if card.suit == choosen_card.suit:
                 if card.suit == lead_card.suit:
                     if card.value < lead_card.value:
                         if choosen_card.value > lead_card.value or choosen_card.value < card.value:
                             choosen_card = card
                     elif choosen_card.value > lead_card.value and len(
                             self.cur_board) == 3:
                         if card.value > choosen_card.value:
                             choosen_card = card
                     elif choosen_card.value > card.value:
                         choosen_card = card
                 else:
                     if card.value > choosen_card.value or card == Card(
                             'QS'):
                         choosen_card = card
             else:
                 if card == Card("QS"):
                     choosen_card = card
                 elif choosen_card == Card("QS"):
                     pass
                 elif card.suit == "H" and choosen_card.suit != "H":
                     choosen_card = card
                 elif card.suit != "H" and choosen_card.suit == "H":
                     pass
                 elif card.value > choosen_card.value:
                     choosen_card = card
     assert isinstance(choosen_card,
                       Card), "choosen_card is not type Card {} {}".format(
                           candidate_cards, map(type, candidate_cards))
     return choosen_card
Example #7
0
 def expose_my_cards(self, yourcards):
     expose_card = []
     for card in self.my_hand_cards:
         if card == Card("AH"):
             expose_card.append(str(card))
     message = "Expose Cards:{}".format(expose_card)
     self.system_log.show_message(message)
     self.system_log.save_logs(message)
     return expose_card
Example #8
0
    def _end_round(self):
        #print("round end, cur board: {}".format(self.cur_board))
        #print("round end, turn_end_player_id: {}".format(self.turn_end_player_id))

        lead_card = self.cur_board[0]
        lead_player_id = self.turn_end_player_id[0]
        for i in range(4):
            card = self.cur_board[i]
            if card.suit == lead_card.suit and card.value > lead_card.value:
                lead_card = card
                lead_player_id = self.turn_end_player_id[i]

        for card in self.cur_board:
            if card in self.game_score_cards:
                self.score_cards[lead_player_id].append(card)

        for player_id in range(4):
            picked_score_cards = self.score_cards[player_id]
            round_score = 0
            round_heart_score = 0
            is_double = False
            if len(picked_score_cards) > 0:
                for card in picked_score_cards:
                    if card == Card("QS"):
                        round_score -= 13
                    elif card == Card("TC"):
                        is_double = True
                    else:
                        round_heart_score -= 1
                if self.is_card_exposed:
                    round_heart_score *= 2
                round_score += round_heart_score
                if is_double:
                    round_score *= 2
            self.scores[player_id] = round_score

        #for i in range(4):
        #    print("round end, player {} score card: {}".format(i, self.score_cards[i]))
        #print("round end, scores: {}".format(self.scores))

        self.next_first = lead_player_id
        self.cur_board = []
        self.turn_end_player_id = []
Example #9
0
    def pass_cards(self, data):
        cards = data['self']['cards']
        self.my_hand_cards = []
        for card_str in cards:
            card = Card(card_str)
            self.my_hand_cards.append(card)
        pass_cards = []
        count = 0
        for i in range(len(self.my_hand_cards)):
            card = self.my_hand_cards[len(self.my_hand_cards) - (i + 1)]
            if card == Card("QS"):
                pass_cards.append(card)
                count += 1
            elif card == Card("TC"):
                pass_cards.append(card)
                count += 1
        for i in range(len(self.my_hand_cards)):
            card = self.my_hand_cards[len(self.my_hand_cards) - (i + 1)]
            if card.suit_index == 2:
                pass_cards.append(card)
                count += 1
                if count == 3:
                    break
        if count < 3:
            for i in range(len(self.my_hand_cards)):
                card = self.my_hand_cards[len(self.my_hand_cards) - (i + 1)]
                if card not in self.game_score_cards:
                    pass_cards.append(card)
                    count += 1
                    if count == 3:
                        break
        return_values = []
        for card in pass_cards:
            return_values.append(str(card))
        message = "Pass Cards:{}".format(return_values)
        self.system_log.show_message(message)
        self.system_log.save_logs(message)
        self.my_pass_card = pass_cards
        self.update_pass_cards(data["dealNumber"], pass_cards)

        return return_values
Example #10
0
    def _get_candidate_cards(self, cards):
        candidate_cards = []
        if not self.cur_board:
            if Card('2C') in cards:
                candidate_cards.append('2C')
            elif self.heart_broken:
                candidate_cards.extend(cards)
            else:
                candidate_cards = list(filter(lambda c: c.suit != "H", cards))
        else:
            lead_card = self.cur_board[0]
            candidate_cards = list(
                filter(lambda c: c.suit == lead_card.suit, cards))

        if len(candidate_cards) == 0:
            candidate_cards.extend(cards)

        return candidate_cards
Example #11
0
    def pick_card(self, data):
        candidate_cards = data['self']['candidateCards']
        cards = data['self']['cards']
        self.my_hand_cards = []
        for card_str in cards:
            card = Card(card_str)
            self.my_hand_cards.append(card)
        message = "My Cards:{}".format(self.my_hand_cards)
        self.system_log.show_message(message)
        card_index = 0
        message = "Pick Card Event Content:{}".format(data)
        self.system_log.show_message(message)
        message = "Candidate Cards:{}".format(candidate_cards)
        self.system_log.show_message(message)
        self.system_log.save_logs(message)

        info = Info(self)
        if self.cursor is None:
            if info.key not in self.root:
                self.root[info.key] = ActionNode(None, None)
                self.root[info.key].info = info
            self.cursor = self.root[info.key]
            logger.debug("root contains: {}".format(",".join(
                self.root.keys())))
            logger.debug("cursor move to root: {}\n{}".format(
                info.key, str(self.cursor)))

        obs_hash = self.cursor.add_observation(self.observation_buffer,
                                               info=info)

        # take simulation result of from previous round's next step)
        self.cursor = self.cursor.observations[obs_hash]
        logger.debug("cursor move to observation: {}\n{}".format(
            obs_hash, str(self.cursor)))
        self.observation_buffer = []

        action = self.search(candidate_cards)
        picked_card = str(action.card)

        message = "Pick Card:{}".format(picked_card)
        self.system_log.show_message(message)
        self.system_log.save_logs(message)
        return picked_card
Example #12
0
 def init_actions(self):
     me = self.info.players[self.info.self_index]
     if len(self.info.cur_board) > 0:
         suit = self.info.cur_board[0].suit
         suit_cards = [c for c in me["hasCards"] if c.suit == suit]
         if len(suit_cards) > 0:
             actions = suit_cards
         else:
             actions = me["hasCards"]
     elif len(me["hasCards"]) == 13:
         actions = [Card("2C")]
     elif self.info.heart_broken:
         actions = me["hasCards"]
     else:
         non_heart_cards = [c for c in me["hasCards"] if c.suit != "H"]
         if len(non_heart_cards) > 0:
             actions = non_heart_cards
         else:
             actions = me["hasCards"]
     self.actions = [ActionNode(self, a) for a in actions]
Example #13
0
    def run(self, state):
        self.init_with_state(state)

        self._rollout()

        moon_shooter_id = -1
        for i in range(4):
            if len(self.score_cards[i]) == 15:
                moon_shooter_id = i
            elif len(self.score_cards[i]) == 14 and Card(
                    "TC") not in self.score_cards[i]:
                moon_shooter_id = i

        if moon_shooter_id != -1:
            if moon_shooter_id == self.my_id:
                return (self.scores[moon_shooter_id] * -1) * 4
            else:
                return (self.scores[moon_shooter_id]) * 4

        return self.scores[self.my_id]
Example #14
0
 def turn_end(self, data):
     super(PomDPBot, self).turn_end(data)
     turnCard = data['turnCard']
     turnPlayer = data['turnPlayer']
     self.observation_buffer.append((turnPlayer, Card(turnCard)))
Example #15
0
 def test_player_to_act(self):
     self.deal.deal_preflop({
         'John': [Card('Ad'), Card('Kd')],
         'Phil': [Card('As'), Card('5c')]
     })
     assert self.deal.player_to_act == self.john
Example #16
0
 def test_raises_exception_if_all_players_havent_acted(self):
     with raises(PlayersNotActed):
         self.deal.deal_flop([Card('6s'), Card('7s'), Card('Ks')])
Example #17
0
 def setup_method(self, method):
     DealTestCase.setup_method(self, method)
     self.deal.deal_preflop({
         'John': [Card('Ad'), Card('Kd')],
         'Phil': [Card('As'), Card('5c')]
     })
Example #18
0
    def step(self, state, card_to_play):
        assert isinstance(card_to_play,
                          Card), "step, card_to_play is not type Card"
        self.init_with_state(state)
        ori_score = self.scores[self.my_id]

        self._play(self.cur_player_id, card_to_play)
        self.cur_player_id = (self.cur_player_id + 1) % 4

        while len(self.cur_board) < 4:
            card_to_play = self._decide(self.cur_player_id)
            self._play(self.cur_player_id, card_to_play)
            self.cur_player_id = (self.cur_player_id + 1) % 4
        self._end_round()

        self.cur_player_id = self.next_first
        while len(self.hand_cards_of_players[
                self.cur_player_id]) > 0 and self.cur_player_id != self.my_id:
            card_to_play = self._decide(self.cur_player_id)
            self._play(self.cur_player_id, card_to_play)
            self.cur_player_id = (self.cur_player_id + 1) % 4

        output_score = ori_score - self.scores[self.my_id]
        moon_shooter_id = -1
        for i in range(4):
            if len(self.score_cards[i]) == 15:
                moon_shooter_id = i
            elif len(self.score_cards[i]) == 14 and Card(
                    "TC") not in self.score_cards[i]:
                moon_shooter_id = i

        if moon_shooter_id != -1:
            if moon_shooter_id == self.my_id:
                output_score += (self.scores[moon_shooter_id] * -1) * 4
            else:
                output_score += (self.scores[moon_shooter_id]) * 4

        return_state = copy.deepcopy(state)
        return_state.cur_board = []
        return_state.cur_board.extend(self.cur_board)
        return_state.heart_broken = self.heart_broken
        for i in range(4):
            return_state.players[i]["hasCards"] = self.hand_cards_of_players[
                i].copy()
            return_state.players[i]["scoreCards"] = []
            return_state.players[i]["scoreCards"].extend(self.score_cards[i])
            return_state.players[i]["score"] = self.scores[i]

        return_state.picked_cards_this_round = []
        for i in range(len(self.turn_end_player_id)):
            for player_id in range(4):
                if self.turn_end_player_id[i] == player_id:
                    return_state.picked_cards_this_round.append(
                        (return_state.players[player_id]["playerName"],
                         self.cur_board[i]))
        # list of tuple(playerName, Card)
        return_observation_buffer = [
            (state.players[player_id]["playerName"], card)
            for player_id, card in self.observation_buffer
        ]

        return return_state, return_observation_buffer, output_score