Exemplo n.º 1
0
    def calculate_deal_score(self):
        for agent_position in self._seat_position:
            agent = self._agent_list[agent_position]
            deal_score = 0
            for card in agent.deal_state[self._current_deal_num -
                                         1].score_cards:
                if card.suit.upper() == 'H':
                    deal_score += -1

            # double the hearts score if heart exposed
            if self._is_heart_exposed:
                deal_score *= 2
            if Card('Qs') in agent.deal_state[self._current_deal_num -
                                              1].score_cards:
                deal_score += -13

            # check shooting the moon
            if deal_score == (-13 + -13) or deal_score == (
                    -13 * 2 + -13):  # heart exposed or not
                agent.deal_state[self._current_deal_num -
                                 1].is_shooting_the_moon = True
                # make score positive
                deal_score *= -1
                # multiply by 4
                deal_score *= 4

            if Card('Tc') in agent.deal_state[self._current_deal_num -
                                              1].score_cards:
                deal_score *= 2
            agent.deal_state[self._current_deal_num -
                             1].deal_score = deal_score
Exemplo n.º 2
0
    def resolve_round_winner(self):
        round_winner_position = None
        round_winner = None
        largest_card = None
        for seat, agent_position in enumerate(self._seat_position):
            agent = self._agent_list[agent_position]
            # match suit only
            if agent.deal_state[
                    self._current_deal_num -
                    1].round_card.suit.upper() == self._current_suit:
                if largest_card is None or agent.deal_state[
                        self._current_deal_num - 1].round_card > largest_card:
                    largest_card = agent.deal_state[self._current_deal_num -
                                                    1].round_card
                    round_winner_position = seat
                    round_winner = agent

        for agent_position in self._seat_position:
            agent = self._agent_list[agent_position]
            card = agent.deal_state[self._current_deal_num - 1].round_card
            if card.suit.upper() == 'H' or card == Card('Qs') or card == Card(
                    'Tc'):
                round_winner.deal_state[self._current_deal_num -
                                        1].score_cards.add(card)
        self._current_round_winner = round_winner_position
Exemplo n.º 3
0
 def find_round_one_player_seat_position(self):
     for seat, agent_position in enumerate(self._seat_position):
         agent = self._agent_list[agent_position]
         if Card('2c') in agent.deal_state[self._current_deal_num -
                                           1].cards:
             self._current_round_winner = seat
             break
Exemplo n.º 4
0
 def get_ah_owner(self):
     for agent_position in self._seat_position:
         agent = self._agent_list[agent_position]
         if Card('Ah') in agent.deal_state[self._current_deal_num -
                                           1].cards:
             agent.deal_state[self._current_deal_num - 1].has_AH = True
             return agent
Exemplo n.º 5
0
    def draw(self, n=1):
        if n == 1:
            return Card(self.cards.pop(0))

        cards = []
        for i in range(n):
            cards.append(self.draw())
        return cards
Exemplo n.º 6
0
    def pass_cards(self):
        message = {'eventName': 'pass_cards', 'data': {}}
        message['data']['dealNumber'] = self._current_deal_num

        players = []
        info_list = ['gameScore', 'dealScore', 'cards', 'cardsCount']
        for agent_position in self._seat_position:
            agent = self._agent_list[agent_position]
            players.append(
                self.build_agent_info(agent, self._current_deal_num - 1,
                                      info_list))

        for seat, agent_position in enumerate(self._seat_position):
            agent = self._agent_list[agent_position]

            # add self information
            self_info = self.build_agent_info(agent,
                                              self._current_deal_num - 1,
                                              info_list)

            message['data']['self'] = self_info
            message['data']['players'] = players

            # add receiver information
            receiver_seat = (
                seat +
                self._passing_card_order[self._current_deal_num - 1]) % 4
            if receiver_seat < 0:
                receiver_seat = 3
            receiver = self._agent_list[self._seat_position[receiver_seat]]
            message['data']['receiver'] = receiver.player_name

            # expected to receive valid pass my card event from agent
            response = agent.agent.take_action(json.dumps(message))

            # record pass cards
            response = json.loads(response)
            receiver.deal_state[self._current_deal_num -
                                1].received_from = agent.player_name
            for card_str in response['data']['cards']:
                rank, suit = card_str
                suit = suit.lower()
                rank = rank.upper()
                rank_suit = rank + suit
                card = Card(rank_suit)
                agent.deal_state[self._current_deal_num -
                                 1].passing_cards.add(card)
                receiver.deal_state[self._current_deal_num -
                                    1].receive_opponent_cards.add(card)
Exemplo n.º 7
0
    def your_turn(self):
        message = {'eventName': 'your_turn', 'data': {}}
        message['data']['dealNumber'] = self._current_deal_num
        message['data']['roundNumber'] = self._current_round_num

        players = []
        info_list = [
            'gameScore', 'dealScore', 'cards', 'cardsCount', 'scoreCards',
            'roundCard', 'serverRandom', 'exposedCards'
        ]
        for agent_position in self._seat_position:
            agent = self._agent_list[agent_position]
            players.append(
                self.build_agent_info(agent, self._current_deal_num - 1,
                                      info_list))

        # add self information
        info_list = [
            'gameScore', 'dealScore', 'cards', 'cardsCount', 'exposedCards'
        ]
        seat = self._round_player_order[self._current_turn_num - 1]
        agent_position = self._seat_position[seat]
        agent = self._agent_list[agent_position]
        self_info = self.build_agent_info(agent, self._current_deal_num - 1,
                                          info_list)

        # build candidate cards
        candidate_cards = []

        # for round 1
        if self._current_round_num == 1:
            # first turn, only 2c can be played
            if self._current_turn_num == 1:
                candidate_cards.append('2C')
            else:
                # must follow the suit
                for card in agent.deal_state[self._current_deal_num - 1].cards:
                    if card.suit.upper() == self._current_suit:
                        candidate_cards.append(card.to_string())
                # cannot follow the suit
                if len(candidate_cards) == 0:
                    # for other turns, can't play hearts and QS for first round
                    for card in agent.deal_state[self._current_deal_num -
                                                 1].cards:
                        if card.suit.upper() != 'H' and card != Card('Qs'):
                            candidate_cards.append(card.to_string())
                    if len(candidate_cards) == 0:
                        # all penalty cards in hands, can't play Qs only
                        for card in agent.deal_state[self._current_deal_num -
                                                     1].cards:
                            if card != Card('Qs'):
                                candidate_cards.append(card.to_string())
        # not first round
        else:
            # for the round winner
            if self._current_turn_num == 1:
                if self._is_heart_broken is True:
                    # can play any cards
                    for card in agent.deal_state[self._current_deal_num -
                                                 1].cards:
                        candidate_cards.append(card.to_string())
                else:
                    # can play any cards but the not the heart
                    for card in agent.deal_state[self._current_deal_num -
                                                 1].cards:
                        if card.suit.upper() != 'H':
                            candidate_cards.append(card.to_string())

                    # if only hearts left, all cards can be played
                    if len(candidate_cards) == 0:
                        for card in agent.deal_state[self._current_deal_num -
                                                     1].cards:
                            candidate_cards.append(card.to_string())
            else:
                # must follow the suit
                for card in agent.deal_state[self._current_deal_num - 1].cards:
                    if card.suit.upper() == self._current_suit:
                        candidate_cards.append(card.to_string())

                    if len(candidate_cards) == 0:
                        # cannot follow the suit
                        if self._is_heart_broken is True:
                            # hearts has broken, can play any cards
                            for card in agent.deal_state[self._current_deal_num
                                                         - 1].cards:
                                candidate_cards.append(card.to_string())
                        else:
                            # hearts hasn't broken, should play other cards
                            for card in agent.deal_state[self._current_deal_num
                                                         - 1].cards:
                                if card.suit.upper() != 'H':
                                    candidate_cards.append(card.to_string())

                            # has only heart cards
                            if len(candidate_cards) == 0:
                                for card in agent.deal_state[
                                        self._current_deal_num - 1].cards:
                                    candidate_cards.append(card.to_string())

        self_info['candidateCards'] = candidate_cards
        message['data']['self'] = self_info
        message['data']['players'] = players
        message['data']['roundPlayers'] = self.build_round_player_message()

        # expected to get pick card event
        response = agent.agent.take_action(json.dumps(message))
        response = json.loads(response)
        card_str = response['data']['turnCard']

        if card_str not in candidate_cards:
            card_str = random.choice(candidate_cards)
            agent.server_random = True
            agent.error_count += 1
        else:
            agent.server_random = False

        rank, suit = card_str
        suit = suit.lower()
        rank = rank.upper()
        rank_suit = rank + suit
        card = Card(rank_suit)

        # remove cards from agent hand
        agent.deal_state[self._current_deal_num - 1].cards.remove(card)

        # add cards to agent round cards
        agent.deal_state[self._current_deal_num - 1].round_card = card

        # set suit if is first turn
        if self._current_turn_num == 1:
            self._current_suit = card.suit.upper()

        # broken hearts
        if card.suit.upper() == 'H':
            self._is_heart_broken = True