Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
*pyc
*.DS_Store
2 changes: 2 additions & 0 deletions axelrod/strategies/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
from titfortat import *
from gobymajority import *
from alternator import *
from averagecopier import *


strategies = [
Expand All @@ -15,4 +16,5 @@
GoByMajority,
Random,
Alternator,
AverageCopier,
]
24 changes: 24 additions & 0 deletions axelrod/strategies/averagecopier.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
from axelrod import Player
import random

class AverageCopier(Player):
"""
The player will cooperate with probability p if the opponent's cooperation ratio is p
"""
def strategy(self, opponent):
"""
Randomly picks a strategy (not affected by history)
"""
if len(opponent.history) == 0:
return random.choice(['C', 'D'])
p = sum([s == 'C' for s in opponent.history]) / len(opponent.history)
rnd_num = random.random()
if rnd_num < p:
return 'C'
return 'D'

def __repr__(self):
"""
The string method for the strategy:
"""
return 'Average Copier'
54 changes: 54 additions & 0 deletions axelrod/tests/test_averagecopier.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
"""
Test for the average_copier strategy
"""
import unittest
import axelrod
import random

class TestAverageCopier(unittest.TestCase):

def test_initial_strategy(self):
"""
Test that the first strategy is picked randomly
"""
random.seed(1)
P1 = axelrod.AverageCopier()
P2 = axelrod.Player()
self.assertEqual(P1.strategy(P2), 'C')
self.assertEqual(P1.strategy(P2), 'D')
self.assertEqual(P1.strategy(P2), 'D')
self.assertEqual(P1.strategy(P2), 'C')
self.assertEqual(P1.strategy(P2), 'C')
self.assertEqual(P1.strategy(P2), 'C')
self.assertEqual(P1.strategy(P2), 'D')
self.assertEqual(P1.strategy(P2), 'D')
self.assertEqual(P1.strategy(P2), 'C')
self.assertEqual(P1.strategy(P2), 'C')

def test_when_oppenent_all_Cs(self):
"""
Tests that if opponent has played all C then player chooses C
"""
random.seed(5)
P1 = axelrod.AverageCopier()
P2 = axelrod.Player()
P2.history = ['C', 'C', 'C', 'C']
self.assertEqual(P1.strategy(P2), 'C')
self.assertEqual(P1.strategy(P2), 'C')
self.assertEqual(P1.strategy(P2), 'C')

def test_when_opponent_all_Ds(self):
"""
Tests that if opponent has played all D then player chooses D
"""
random.seed(5)
P1 = axelrod.AverageCopier()
P2 = axelrod.Player()
P2.history = ['D', 'D', 'D', 'D']
self.assertEqual(P1.strategy(P2), 'D')
self.assertEqual(P1.strategy(P2), 'D')
self.assertEqual(P1.strategy(P2), 'D')

def test_representation(self):
P1 = axelrod.AverageCopier()
self.assertEqual(str(P1), 'Average Copier')
Binary file modified results.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.