Skip to content

Commit f1a45c0

Browse files
authored
Merge pull request #750 from Axelrod-Python/tidytitfortat
Fix PEP8 errors in Tit For Tat
2 parents 020bdd6 + ce1a574 commit f1a45c0

File tree

2 files changed

+140
-64
lines changed

2 files changed

+140
-64
lines changed

axelrod/strategies/titfortat.py

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,7 @@ class SuspiciousTitForTat(Player):
160160

161161
name = "Suspicious Tit For Tat"
162162
classifier = {
163-
'memory_depth': 1, # Four-Vector = (1.,0.,1.,0.)
163+
'memory_depth': 1, # Four-Vector = (1.,0.,1.,0.)
164164
'stochastic': False,
165165
'makes_use_of': set(),
166166
'long_run_time': False,
@@ -267,7 +267,7 @@ class OmegaTFT(Player):
267267
"""OmegaTFT modifies Tit For Tat in two ways:
268268
- checks for deadlock loops of alternating rounds of (C, D) and (D, C),
269269
and attempting to break them
270-
- uses a more sophisticated retaliation mechanism that is noise tolerant.
270+
- uses a more sophisticated retaliation mechanism that is noise tolerant
271271
272272
Names:
273273
@@ -315,9 +315,10 @@ def strategy(self, opponent):
315315
# If the opponent's move changed, increase the counter
316316
if opponent.history[-2] != opponent.history[-1]:
317317
self.randomness_counter += 1
318-
# If the opponent's last move differed from mine, increase the counter
318+
# If the opponent's last move differed from mine,
319+
# increase the counter
319320
if self.history[-1] == opponent.history[-1]:
320-
self.randomness_counter+= 1
321+
self.randomness_counter += 1
321322
# Compare counts to thresholds
322323
# If randomness_counter exceeds Y, Defect for the remainder
323324
if self.randomness_counter >= 8:
@@ -479,6 +480,7 @@ def strategy(self, opponent):
479480
# Otherwise cooperate
480481
return C
481482

483+
482484
class AdaptiveTitForTat(Player):
483485
"""ATFT - Adaptive Tit For Tat (Basic Model)
484486
@@ -559,7 +561,8 @@ def __repr__(self):
559561
class SpitefulTitForTat(Player):
560562
"""
561563
A player starts by cooperating and then mimics the previous action of the
562-
opponent until opponent defects twice in a row, at which point player always defects
564+
opponent until opponent defects twice in a row, at which point player
565+
always defects
563566
564567
Names:
565568

axelrod/tests/unit/test_titfortat.py

Lines changed: 132 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,8 @@ def test_strategy(self):
129129
self.first_play_test(C)
130130

131131
def test_effect_of_strategy(self):
132-
"""Will try defecting after two turns of cooperation, but will stop if punished."""
132+
"""Will try defecting after two turns of cooperation, but will stop
133+
if punished."""
133134
self.responses_test([C, C], [C, C], [D])
134135
self.responses_test([C, C, D, D], [C, C, C, D], [C])
135136

@@ -139,7 +140,7 @@ class TestSuspiciousTitForTat(TestPlayer):
139140
name = 'Suspicious Tit For Tat'
140141
player = axelrod.SuspiciousTitForTat
141142
expected_classifier = {
142-
'memory_depth': 1, # Four-Vector = (1.,0.,1.,0.)
143+
'memory_depth': 1, # Four-Vector = (1.,0.,1.,0.)
143144
'stochastic': False,
144145
'makes_use_of': set(),
145146
'inspects_source': False,
@@ -152,7 +153,8 @@ def test_strategy(self):
152153
self.first_play_test(D)
153154

154155
def test_effect_of_strategy(self):
155-
"""Plays like TFT after the first move, repeating the opponents last move."""
156+
"""Plays like TFT after the first move, repeating the opponents last
157+
move."""
156158
self.markov_test([C, D, C, D])
157159

158160

@@ -161,7 +163,7 @@ class TestAntiTitForTat(TestPlayer):
161163
name = 'Anti Tit For Tat'
162164
player = axelrod.AntiTitForTat
163165
expected_classifier = {
164-
'memory_depth': 1, # Four-Vector = (1.,0.,1.,0.)
166+
'memory_depth': 1, # Four-Vector = (1.,0.,1.,0.)
165167
'stochastic': False,
166168
'makes_use_of': set(),
167169
'inspects_source': False,
@@ -183,7 +185,7 @@ class TestHardTitForTat(TestPlayer):
183185
name = "Hard Tit For Tat"
184186
player = axelrod.HardTitForTat
185187
expected_classifier = {
186-
'memory_depth': 3, # Four-Vector = (1.,0.,1.,0.)
188+
'memory_depth': 3, # Four-Vector = (1.,0.,1.,0.)
187189
'stochastic': False,
188190
'makes_use_of': set(),
189191
'inspects_source': False,
@@ -268,16 +270,20 @@ def test_reset(self):
268270

269271
class TestOmegaTFTvsSTFT(TestHeadsUp):
270272
def test_rounds(self):
271-
outcomes = zip()
272-
self.versus_test(axelrod.OmegaTFT(), axelrod.SuspiciousTitForTat(),
273-
[C, D, C, D, C, C, C, C, C],
274-
[D, C, D, C, D, C, C, C, C])
273+
self.versus_test(
274+
axelrod.OmegaTFT(), axelrod.SuspiciousTitForTat(),
275+
[C, D, C, D, C, C, C, C, C],
276+
[D, C, D, C, D, C, C, C, C]
277+
)
278+
275279

276280
class TestOmegaTFTvsAlternator(TestHeadsUp):
277281
def test_rounds(self):
278-
self.versus_test(axelrod.OmegaTFT(), axelrod.Alternator(),
279-
[C, C, D, C, D, C, C, C, D, C, C, C, D, D, D, D, D, D],
280-
[C, D, C, D, C, D, C, D, C, D, C, D, C, D, C, D, C, D])
282+
self.versus_test(
283+
axelrod.OmegaTFT(), axelrod.Alternator(),
284+
[C, C, D, C, D, C, C, C, D, C, C, C, D, D, D, D, D, D],
285+
[C, D, C, D, C, D, C, D, C, D, C, D, C, D, C, D, C, D]
286+
)
281287

282288

283289
class TestGradual(TestPlayer):
@@ -300,38 +306,96 @@ def test_strategy(self):
300306
def test_effect_of_strategy(self):
301307
"""Punishes defection with a growing number of defections and calms
302308
the opponent with two Cooperations in a row"""
303-
self.responses_test([C], [C], [C], attrs={"calming": False,
304-
"punishing": False, "punishment_count": 0,
305-
"punishment_limit": 0})
306-
self.responses_test([C], [D], [D], attrs={"calming": False,
307-
"punishing": True, "punishment_count": 1,
308-
"punishment_limit": 1})
309-
self.responses_test([C, D], [D, C], [C], attrs={"calming": True,
310-
"punishing": False, "punishment_count": 0,
311-
"punishment_limit": 1})
312-
self.responses_test([C, D, C], [D, C, D], [C], attrs={"calming": False,
313-
"punishing": False, "punishment_count": 0,
314-
"punishment_limit": 1})
315-
self.responses_test([C, D, C, C], [D, C, D, C], [C],
316-
attrs={"calming": False, "punishing": False,
317-
"punishment_count": 0, "punishment_limit": 1})
318-
self.responses_test([C, D, C, D, C], [D, C, D, C, C], [C],
319-
attrs={"calming": False, "punishing": False,
320-
"punishment_count": 0, "punishment_limit": 1})
321-
self.responses_test([C, D, C, D, C, C], [D, C, D, C, C, D], [D],
322-
attrs={"calming": False, "punishing": True,
323-
"punishment_count": 1, "punishment_limit": 2})
324-
self.responses_test([C, D, C, D, D, C, D], [D, C, D, C, C, D, C], [D],
325-
attrs={"calming": False, "punishing": True,
326-
"punishment_count": 2, "punishment_limit": 2})
327-
self.responses_test([C, D, C, D, D, C, D, D],
328-
[D, C, D, C, C, D, C, C], [C],
329-
attrs={"calming": True, "punishing": False,
330-
"punishment_count": 0, "punishment_limit": 2})
331-
self.responses_test([C, D, C, D, D, C, D, D, C],
332-
[D, C, D, C, C, D, C, C, C], [C],
333-
attrs={"calming": False, "punishing": False,
334-
"punishment_count": 0, "punishment_limit": 2})
309+
self.responses_test(
310+
[C], [C], [C],
311+
attrs={
312+
"calming": False,
313+
"punishing": False,
314+
"punishment_count": 0,
315+
"punishment_limit": 0
316+
}
317+
)
318+
self.responses_test(
319+
[C], [D], [D],
320+
attrs={
321+
"calming": False,
322+
"punishing": True,
323+
"punishment_count": 1,
324+
"punishment_limit": 1
325+
}
326+
)
327+
self.responses_test(
328+
[C, D], [D, C], [C],
329+
attrs={
330+
"calming": True,
331+
"punishing": False,
332+
"punishment_count": 0,
333+
"punishment_limit": 1
334+
}
335+
)
336+
self.responses_test(
337+
[C, D, C], [D, C, D], [C],
338+
attrs={
339+
"calming": False,
340+
"punishing": False,
341+
"punishment_count": 0,
342+
"punishment_limit": 1
343+
}
344+
)
345+
self.responses_test(
346+
[C, D, C, C], [D, C, D, C], [C],
347+
attrs={
348+
"calming": False,
349+
"punishing": False,
350+
"punishment_count": 0,
351+
"punishment_limit": 1
352+
}
353+
)
354+
self.responses_test(
355+
[C, D, C, D, C], [D, C, D, C, C], [C],
356+
attrs={
357+
"calming": False,
358+
"punishing": False,
359+
"punishment_count": 0,
360+
"punishment_limit": 1
361+
}
362+
)
363+
self.responses_test(
364+
[C, D, C, D, C, C], [D, C, D, C, C, D], [D],
365+
attrs={
366+
"calming": False,
367+
"punishing": True,
368+
"punishment_count": 1,
369+
"punishment_limit": 2
370+
}
371+
)
372+
self.responses_test(
373+
[C, D, C, D, D, C, D], [D, C, D, C, C, D, C], [D],
374+
attrs={
375+
"calming": False,
376+
"punishing": True,
377+
"punishment_count": 2,
378+
"punishment_limit": 2
379+
}
380+
)
381+
self.responses_test(
382+
[C, D, C, D, D, C, D, D], [D, C, D, C, C, D, C, C], [C],
383+
attrs={
384+
"calming": True,
385+
"punishing": False,
386+
"punishment_count": 0,
387+
"punishment_limit": 2
388+
}
389+
)
390+
self.responses_test(
391+
[C, D, C, D, D, C, D, D, C], [D, C, D, C, C, D, C, C, C], [C],
392+
attrs={
393+
"calming": False,
394+
"punishing": False,
395+
"punishment_count": 0,
396+
"punishment_limit": 2
397+
}
398+
)
335399

336400
def test_reset_cleans_all(self):
337401
p = axelrod.Gradual()
@@ -432,13 +496,13 @@ def test_strategy_with_noise(self):
432496
self.assertEqual(opponent.history, [C, D, D, D])
433497
self.assertFalse(ctft.contrite)
434498

435-
436499
def test_reset_cleans_all(self):
437500
p = self.player()
438501
p.contrite = True
439502
p.reset()
440503
self.assertFalse(p.contrite)
441504

505+
442506
class TestSlowTitForTwoTats(TestPlayer):
443507

444508
name = "Slow Tit For Two Tats"
@@ -457,10 +521,12 @@ def test_strategy(self):
457521
self.first_play_test(C)
458522

459523
def test_effect_of_strategy(self):
460-
"""If opponent plays the same move twice, repeats last action of opponent history."""
461-
self.responses_test([C]*2, [C, C], [C])
462-
self.responses_test([C]*3, [C, D, C], [C])
463-
self.responses_test([C]*3, [C, D, D], [D])
524+
"""If opponent plays the same move twice, repeats last action of
525+
opponent history."""
526+
self.responses_test([C] * 2, [C, C], [C])
527+
self.responses_test([C] * 3, [C, D, C], [C])
528+
self.responses_test([C] * 3, [C, D, D], [D])
529+
464530

465531
class TestAdaptiveTitForTat(TestPlayer):
466532

@@ -474,20 +540,20 @@ class TestAdaptiveTitForTat(TestPlayer):
474540
'manipulates_source': False,
475541
'manipulates_state': False
476542
}
477-
543+
478544
def test_strategy(self):
479545
"""Start by cooperating."""
480546
self.first_play_test(C)
481-
547+
482548
def test_effect_of_strategy(self):
483-
549+
484550
self.markov_test(['C', 'D', 'C', 'D'])
485-
551+
486552
p1, p2 = self.player(), self.player()
487553
p1.play(p2)
488554
p1.play(p2)
489555
self.assertEqual(p2.world, 0.75)
490-
556+
491557
def test_world_rate_reset(self):
492558
p1, p2 = self.player(), self.player()
493559
p1.play(p2)
@@ -496,7 +562,7 @@ def test_world_rate_reset(self):
496562
self.assertEqual(p2.world, 0.5)
497563
self.assertEqual(p2.rate, 0.5)
498564

499-
565+
500566
class TestSpitefulTitForTat(TestPlayer):
501567
name = "Spiteful Tit For Tat"
502568
player = axelrod.SpitefulTitForTat
@@ -514,11 +580,18 @@ def test_strategy(self):
514580
self.first_play_test(C)
515581

516582
def test_effect_of_strategy(self):
517-
"""Repeats last action of opponent history until 2 consecutive defections, then always defects"""
583+
"""Repeats last action of opponent history until 2 consecutive
584+
defections, then always defects"""
518585
self.markov_test([C, D, C, D])
519-
self.responses_test([C] * 4, [C, C, C, C], [C], attrs={"retaliating": False})
520-
self.responses_test([C] * 5, [C, C, C, C, D], [D], attrs={"retaliating": False})
521-
self.responses_test([C] * 5, [C, C, D, D, C], [D], attrs={"retaliating": True})
586+
self.responses_test(
587+
[C] * 4, [C, C, C, C], [C], attrs={"retaliating": False}
588+
)
589+
self.responses_test(
590+
[C] * 5, [C, C, C, C, D], [D], attrs={"retaliating": False}
591+
)
592+
self.responses_test(
593+
[C] * 5, [C, C, D, D, C], [D], attrs={"retaliating": True}
594+
)
522595

523596
def test_reset_retaliating(self):
524597
player = self.player()

0 commit comments

Comments
 (0)