def simulate_play(P1, P2, h1=None, h2=None): """ Simulates play with or without forced history. If h1 and h2 are given, these moves are enforced in the players strategy. This generally should not be necessary, but various tests may force impossible or unlikely histories. """ if h1 and h2: # Simulate Plays s1 = P1.strategy(MockPlayer(P2, h2)) s2 = P2.strategy(MockPlayer(P1, h1)) # Record intended history # Update Cooperation / Defection counts update_histories(P1, P2, h1, h2) return (h1, h2) else: s1 = P1.strategy(P2) s2 = P2.strategy(P1) # If P1 or P2 is axelrod.Player, they will return None, change to # s1 or s2 to 'C' if that case. if not s1: s1 = C if not s2: s2 = C # Record history # Update Cooperation / Defection counts update_histories(P1, P2, s1, s2) return (s1, s2)
def test_various(self): p1 = TestOpponent() p2 = TestOpponent() update_histories(p1, p2, C, C) self.assertEqual(p1.history, [C]) self.assertEqual(p2.history, [C]) self.assertEqual(p1.cooperations, 1) self.assertEqual(p2.cooperations, 1) self.assertEqual(p1.defections, 0) self.assertEqual(p2.defections, 0) update_histories(p1, p2, D, D) self.assertEqual(p1.history, [C, D]) self.assertEqual(p2.history, [C, D]) self.assertEqual(p1.cooperations, 1) self.assertEqual(p2.cooperations, 1) self.assertEqual(p1.defections, 1) self.assertEqual(p2.defections, 1)
def test_various(self): p1 = axelrod.Player() p2 = axelrod.Player() update_histories(p1, p2, C, C) self.assertEqual(p1.history, [C]) self.assertEqual(p2.history, [C]) self.assertEqual(p1.cooperations, 1) self.assertEqual(p2.cooperations, 1) self.assertEqual(p1.defections, 0) self.assertEqual(p2.defections, 0) update_histories(p1, p2, D, D) self.assertEqual(p1.history, [C, D]) self.assertEqual(p2.history, [C, D]) self.assertEqual(p1.cooperations, 1) self.assertEqual(p2.cooperations, 1) self.assertEqual(p1.defections, 1) self.assertEqual(p2.defections, 1)
def simulate_play(P1, P2, h1=None, h2=None): """ Simulates play with or without forced history. If h1 and h2 are given, these moves are enforced in the players strategy. This generally should not be necessary, but various tests may force impossible or unlikely histories. """ if h1 and h2: # Simulate Plays s1 = P1.strategy(MockPlayer(P2, h2)) s2 = P2.strategy(MockPlayer(P1, h1)) # Record intended history # Update Cooperation / Defection counts update_histories(P1, P2, h1, h2) return (h1, h2) else: s1 = P1.strategy(P2) s2 = P2.strategy(P1) # Record history update_histories(P1, P2, s1, s2) return (s1, s2)
def simulate_match(player_1, player_2, strategy, rounds=10): """Simulates a number of matches.""" for match in range(rounds): play_1, play_2 = strategy, player_2.strategy(player_1) # Update histories and counts update_histories(player_1, player_2, play_1, play_2)