Ejemplo n.º 1
0
    def to_state(self, order_deck=True):
        state = State(prepare_state=False)

        state.active_player_id = self.state_as_dict['active_player_id']
        state.list_of_players_hands[state.active_player_id].from_dict(
            self.state_as_dict['active_player_hand'])
        state.list_of_players_hands[(state.active_player_id - 1) % len(
            state.list_of_players_hands)].from_dict(
                self.state_as_dict['other_player_hand'])
        state.board.from_dict(self.state_as_dict)

        # Adding nobles
        for i in self.state_as_dict['active_player_hand'][
                'noble_possessed_ids']:
            state.list_of_players_hands[
                state.active_player_id].nobles_possessed.add(
                    state.board.deck.pop_noble_by_id(i))

        for i in self.state_as_dict['other_player_hand'][
                'noble_possessed_ids']:
            state.list_of_players_hands[(state.active_player_id - 1) % len(
                state.list_of_players_hands)].nobles_possessed.add(
                    state.board.deck.pop_noble_by_id(i))

        for i in self.state_as_dict['board']['nobles_on_board']:
            state.board.nobles_on_board.add(
                state.board.deck.pop_noble_by_id(i))

        # Adding cards
        for i in self.state_as_dict['active_player_hand'][
                'cards_possessed_ids']:
            state.list_of_players_hands[
                state.active_player_id].cards_possessed.add(
                    state.board.deck.pop_card_by_id(i))

        for i in self.state_as_dict['active_player_hand'][
                'cards_reserved_ids']:
            state.list_of_players_hands[
                state.active_player_id].cards_reserved.add(
                    state.board.deck.pop_card_by_id(i))

        for i in self.state_as_dict['other_player_hand'][
                'cards_possessed_ids']:
            state.list_of_players_hands[(state.active_player_id - 1) % len(
                state.list_of_players_hands)].cards_possessed.add(
                    state.board.deck.pop_card_by_id(i))

        for i in self.state_as_dict['other_player_hand']['cards_reserved_ids']:
            state.list_of_players_hands[(state.active_player_id - 1) % len(
                state.list_of_players_hands)].cards_reserved.add(
                    state.board.deck.pop_card_by_id(i))

        if order_deck:
            state.board.deck.order_deck(self.state_as_dict)
        else:
            state.board.deck.shuffle()

        return state
    def recreate_state(self):
        """Loads observation and return a current_state that agrees with the observation. Warning: this method is ambiguous,
        that is, many states can have the same observation (they may differ in the order of hidden cards)."""
        state = State(all_cards=StochasticObservation.all_cards,
                      all_nobles=StochasticObservation.all_nobles,
                      prepare_state=False)
        cards_on_board_names = self.observation_dict['cards_on_board_names']
        nobles_on_board_names = self.observation_dict['nobles_on_board_names']
        for card_name in cards_on_board_names:
            card = name_to_card_dict[card_name]
            state.board.cards_on_board.add(card)
            state.board.deck.decks_dict[card.row].remove(card)

        for noble_name in nobles_on_board_names:
            noble = name_to_noble_dict[noble_name]
            state.board.nobles_on_board.add(noble)
            state.board.deck.deck_of_nobles.remove(noble)

        state.board.gems_on_board = self.observation_dict['gems_on_board']

        players_hands = []
        for player_observation in self.observation_dict['players_hands']:
            players_hand = PlayersHand()
            players_hand.gems_possessed = player_observation['gems_possessed']
            for card_name in player_observation['cards_possessed_names']:
                card = name_to_card_dict[card_name]
                players_hand.cards_possessed.add(card)
                state.board.deck.decks_dict[card.row].remove(card)
            for card_name in player_observation['cards_reserved_names']:
                card = name_to_card_dict[card_name]
                players_hand.cards_reserved.add(card)
                state.board.deck.decks_dict[card.row].remove(card)
            for noble_name in player_observation['nobles_possessed_names']:
                noble = name_to_noble_dict[noble_name]
                players_hand.nobles_possessed.add(noble)
                state.board.deck.deck_of_nobles.remove(noble)
            players_hands.append(players_hand)

        state.active_player_id = self.observation_dict['active_player_id']
        state.list_of_players_hands = players_hands
        return state
Ejemplo n.º 3
0
 def change_active_player(self, state: State) -> None:
     """Changes active player to the next one."""
     state.active_player_id = (state.active_player_id + 1)%len(state.list_of_players_hands)