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
2 changes: 2 additions & 0 deletions axelrod/strategies/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
from gobymajority import *
from alternator import *
from averagecopier import *
from grumpy import *


strategies = [
Expand All @@ -17,4 +18,5 @@
Random,
Alternator,
AverageCopier,
Grumpy,
]
55 changes: 55 additions & 0 deletions axelrod/strategies/grumpy.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
from axelrod import Player

class Grumpy(Player):
"""
A player that defects after a ceratin level of grumpiness. Grumpiness increases when the opponent defects and decreases when the opponent co-operates.
"""

def __init__(self, starting_state = 'Nice', grumpy_threshold = 10, nice_threshold = -10):
"""
Player starts of nice be default with set thresholds
"""

self.history = []
self.score = 0
self.state = starting_state
self.starting_state = starting_state
self.grumpy_threshold = grumpy_threshold
self.nice_threshold = nice_threshold

def strategy(self, opponent):
"""
A player that gets grumpier the more the opposition defects, and nicer the more they cooperate.
Starts off Nice, but becomes grumpy once the grumpiness threshold is hit.
Won't become nice once that grumpy threshold is hit, but must reach a much lower threshold before it becomes nice again.
"""

self.grumpiness = sum(play=='D' for play in opponent.history) - sum(play=='C' for play in opponent.history)

if self.state == 'Nice':
if self.grumpiness > self.grumpy_threshold:
self.state = 'Grumpy'
return 'D'
return 'C'

if self.state == 'Grumpy':
if self.grumpiness < self.nice_threshold:
self.state = 'Nice'
return 'C'
return 'D'

def reset(self):
"""
Resets score, history and state for the next round of the tournement
"""

self.history = []
self.score = 0
self.state = self.starting_state

def __repr__(self):
"""
The string method for the strategy:
"""

return 'Grumpy'
64 changes: 64 additions & 0 deletions axelrod/tests/test_grumpy.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
"""
Test for the grudger strategy
"""
import unittest
import axelrod

class TestGrumpy(unittest.TestCase):

def test_initial_nice_strategy(self):
"""
Starts by cooperating
"""
P1 = axelrod.Grumpy()
P2 = axelrod.Player()
self.assertEqual(P1.strategy(P2), 'C')

def test_initial_grumpy_strategy(self):
"""
Starts by defecting if grumpy
"""
P1 = axelrod.Grumpy(starting_state = 'Grumpy')
P2 = axelrod.Player()
self.assertEqual(P1.strategy(P2), 'D')

def test_effect_of_strategy(self):
"""
Tests that grumpy will play c until threshold is ht at which point it will become grumpy.
Player will then not become nice until lower nice threshold is hit.
"""
P1 = axelrod.Grumpy(grumpy_threshold = 3, nice_threshold=0)
P2 = axelrod.Player()
P1.history = ['C', 'D', 'D', 'D']
P2.history = ['C', 'C', 'C', 'C']
self.assertEqual(P1.strategy(P2), 'C')
P1.history = ['C', 'C', 'D', 'D', 'D']
P2.history = ['D', 'D', 'D', 'D', 'D']
self.assertEqual(P1.strategy(P2), 'D')
P1.history = ['C', 'C', 'D', 'D', 'D', 'D', 'D', 'D']
P2.history = ['D', 'D', 'D', 'D', 'D', 'C', 'C', 'C']
self.assertEqual(P1.strategy(P2), 'D')
P1.history = ['C', 'C', 'D', 'D', 'D', 'D', 'D', 'D', 'D', 'D', 'D']
P2.history = ['D', 'D', 'D', 'D', 'D', 'C', 'C', 'C', 'C', 'C', 'C']
self.assertEqual(P1.strategy(P2), 'C')

def test_representation(self):
"""
Tests string representation of class
"""
P1 = axelrod.Grumpy()
self.assertEqual(str(P1), 'Grumpy')

def test_reset_method(self):
"""
tests the reset method
"""
P1 = axelrod.Grumpy(starting_state = 'Grumpy')
P1.history = ['C', 'D', 'D', 'D']
P1.score = 5
P1.state = 'Nice'
P1.reset()
self.assertEqual(P1.history, [])
self.assertEqual(P1.score, 0)
self.assertEqual(P1.state, 'Grumpy')