Ejemplo n.º 1
0
def simulate_play(player1: Player, player2: Player, action1: Action =None, action2: Action =None) -> Tuple[Action, Action]:
    """
    Simulates play with or without forced history. If action1 and action2 are given, these
    actions are enforced in the players strategy. This generally should not be
    necessary, but various tests may force impossible or unlikely histories.
    """

    if action1 and action2:
        mock_player1 = MockPlayer(actions=[action1], history=player1.history)
        mock_player2 = MockPlayer(actions=[action2], history=player2.history)
        # Force plays
        s1 = player1.strategy(mock_player2)
        s2 = player2.strategy(mock_player1)
        if (s1 != action1) or (s2 != action2):
            warnings.warn(
            "Simulated play mismatch with expected history: Round was "
            "({}, {}) but ({}, {}) was expected for player: {}".format(
                s1, s2, action1, action2, str(player1))
            )
        # Record intended history
        # Update Cooperation / Defection counts
        update_history(player1, action1)
        update_history(player2, action2)
        update_state_distribution(player1, action1, action2)
        update_state_distribution(player2, action2, action1)
        return (s1, s2)
    else:
        s1 = player1.strategy(player2)
        s2 = player2.strategy(player1)
        # Record history
        update_history(player1, s1)
        update_history(player2, s2)
        update_state_distribution(player1, s1, s2)
        update_state_distribution(player2, s2, s1)
        return (s1, s2)
Ejemplo n.º 2
0
    def strategy(opponent: Player) -> Action:
        """
        Alters the opponents strategy method to be a lambda function which
        always returns C. This player will then always return D to take
        advantage of this
        """

        opponent.strategy = lambda opponent: C
        return D
Ejemplo n.º 3
0
    def strategy(opponent: Player) -> Action:
        """
        Alters the opponents strategy method to be a lambda function which
        always returns C. This player will then always return D to take
        advantage of this
        """

        opponent.strategy = lambda opponent: C
        return D
Ejemplo n.º 4
0
 def strategy(self, opponent: Player) -> Action:
     """
     Look at what the opponent will play in the next round and choose a strategy
     that gives the least jail time, which is is equivalent to playing the same
     strategy as that which the opponent will play.
     """
     curframe = inspect.currentframe()
     calframe = inspect.getouterframes(curframe, 2)
     calname = calframe[1][3]
     if calname == 'strategy':
         return self.default()
     else:
         return opponent.strategy(self)
Ejemplo n.º 5
0
    def strategy(self, opponent: Player) -> Action:
        """Will read the mind of the opponent and play the opponent's strategy.

        Also avoid infinite recursion when called by itself or another mind
        reader or bender by cooperating.
        """

        curframe = inspect.currentframe()
        calframe = inspect.getouterframes(curframe, 2)
        calname = calframe[1][3]

        if calname in ('strategy', 'simulate_match'):
            return C

        return opponent.strategy(self)
Ejemplo n.º 6
0
 def strategy(opponent: Player) -> Action:
     opponent.strategy = lambda opponent: C
     return D
Ejemplo n.º 7
0
 def strategy(opponent: Player) -> Action:
     opponent.strategy = lambda opponent: C
     return D