Esempio n. 1
0
    def apply_moves(self, moves):
        """A new game state representing the game after a move is applied.

        Returns a new state, this state is unchanged.

        Args:
            moves (dict) : Dict of `Role` => `Action`, one entry per role.

        Returns:
            GeneralGameState: The new game state.
        """
        with Frame() as f:
            game_id_term = self._game_id_term()

            moves_term = Term.from_list_terms([
                self._does_functor(Term.from_atom(role._atom),
                                   action._term_record.get())
                for (role, action) in moves.items()])

            # prepare_moves(game_id, moves_term, PreparedMoves)
            prepared_moves = f.term()
            try:
                self._prepare_moves_predicate(
                    game_id_term, moves_term, prepared_moves, check=True)
            except PrologCallFailed:
                raise ValueError(
                    'Invalid move set. Possibly not 1 move per role.')

            # NewMoveHistory = [PreparedMoves | old_move_history]
            new_move_history = Term.from_cons_list(prepared_moves,
                                                   self.move_history.get())

            # truth_history(game_id, NewMoveHistory, old_truth_history,
            #               NewTruthHistory)
            new_truth_history = f.term()
            try:
                self._truth_history_4_predicate(
                    game_id_term, new_move_history, self.truth_history.get(),
                    new_truth_history, check=True)
            except PrologCallFailed:
                raise ValueError('Invalid moves: {}'.format(
                    {str(role): str(action)
                     for role, action in moves.items()}))

            # final_truth_state(NewTruthHistory, NewTruthState)
            new_truth_state = f.term()
            self._final_truth_state_predicate(
                new_truth_history, new_truth_state, check=True)

            return GeneralGameState(
                game=self.game,
                move_history_term=new_move_history,
                truth_history_term=new_truth_history,
                truth_state_term=new_truth_state,
            )
Esempio n. 2
0
    def legal_actions(self, role):
        """An iterator of legal actions for role in the current state."""
        with Frame() as f:
            action = f.term()
            action_query = self._legal_functor(
                Term.from_atom(role._atom), action)

            query = self._query(action_query)
            for action_assignment in query.term_assignments(action,
                                                            persistent=True):
                yield Action._from_term_record(action_assignment)
Esempio n. 3
0
    def utility(self, role):
        """The utility of the current state for the given role."""
        with Frame():
            utility = Term()
            utility_query = self._goal_functor(
                Term.from_atom(role._atom), utility)
            self._query_term(utility_query)(check=True)

            if utility.is_atom():
                return int(utility.get_atom_name())
            else:
                return int(utility)
Esempio n. 4
0
    def all_actions(self, role):
        """Iterator over all possible actions for `role` in this game.

        This does not represent the legal actions in some state.
        It is an iterator of all actions which may be available to role at some
        time in the game.

        Args:
            role (Role) : Get actions for this role.

        Yields:
            Action: A possible action for `role` in this game.
        """
        with Frame() as f:
            action_variable = f.term()
            input_query_term = self._input_functor(
                Term.from_atom(role._atom), action_variable)

            query = self._stateless_query(input_query_term)
            for action in query.term_assignments(action_variable,
                                                 persistent=True):
                yield Action._from_term_record(action)
Esempio n. 5
0
 def is_terminal(self):
     """True if the current game state is terminal."""
     with Frame():
         return self._query_term(Term.from_atom(self._terminal_atom))()
Esempio n. 6
0
 def _stateless_query(self, *queries):
     return self._stateful_query(
         Term.from_atom(self._empty_game_state), *queries)