Create a gist now

Instantly share code, notes, and snippets.

from __future__ import division
import matplotlib.pyplot as plt
def dictplot(D, f, title):
plt.figure()
values = [v / sum(list(D.values())) for v in list(D.values())]
plt.bar(range(len(D)), values, align='center')
plt.xticks(range(len(D)), D.keys())
plt.ylabel('Probability')
plt.title(title)
plt.savefig(f)
class Player():
"""
A player
"""
def __init__(self, name):
self.name = name
def __str__(self):
return self.name
class Match():
"""
A match corresponds to 3 duels
"""
def __init__(self, players, duels):
self.players = players
self.duels = duels
self.winner = sorted(self.players, key=lambda x: len([d for d in duels if d.winner == x]))[1]
def __str__(self):
return str(self.winner)
class Duel():
"""
A dual of Rock, Paper, Scissors, Lizard, Spock
"""
def __init__(self, players, strategies):
self.players = players
self.strategies = strategies
if strategies[0] == strategies[1]:
self.winner = 'Tie'
self.winningstrategy = False
self.losingstrategy = False
if sorted(strategies) == sorted(['R','P']):
self.winner = self.players[strategies.index('P')]
self.winningstrategy = 'P'
self.losingstrategy = 'R'
if sorted(strategies) == sorted(['R','Sc']):
self.winner = self.players[strategies.index('R')]
self.winningstrategy = 'R'
self.losingstrategy = 'Sc'
if sorted(strategies) == sorted(['R','Sp']):
self.winner = self.players[strategies.index('Sp')]
self.winningstrategy = 'Sp'
self.losingstrategy = 'R'
if sorted(strategies) == sorted(['R','L']):
self.winner = self.players[strategies.index('R')]
self.winningstrategy = 'R'
self.losingstrategy = 'L'
if sorted(strategies) == sorted(['L','P']):
self.winner = self.players[strategies.index('L')]
self.winningstrategy = 'L'
self.losingstrategy = 'P'
if sorted(strategies) == sorted(['L','Sc']):
self.winner = self.players[strategies.index('Sc')]
self.winningstrategy = 'Sc'
self.losingstrategy = 'L'
if sorted(strategies) == sorted(['L','Sp']):
self.winner = self.players[strategies.index('L')]
self.winningstrategy = 'L'
self.losingstrategy = 'Sp'
if sorted(strategies) == sorted(['Sp','P']):
self.winner = self.players[strategies.index('P')]
self.winningstrategy = 'P'
self.losingstrategy = 'Sp'
if sorted(strategies) == sorted(['Sp','Sc']):
self.winner = self.players[strategies.index('Sp')]
self.winningstrategy = 'Sp'
self.losingstrategy = 'Sc'
if sorted(strategies) == sorted(['Sc','P']):
self.winner = self.players[strategies.index('Sc')]
self.winningstrategy = 'Sc'
self.losingstrategy = 'P'
def __str__(self):
return str(self.winner)
players = [Player('Matt'),
Player('Hannah'),
Player('Shawn'),
Player('Andrea'),
Player('Jade'),
Player('Kate'),
Player('Danny'),
Player('Claire'),
Player('Cara'),
Player('Annie'),
Player('Manraj'),
Player('Pri'),
Player('Asher'),
Player('Marion'),
Player('Simon'),
Player('Ryan')
]
round1 = [Match([players[0], players[1]], [Duel([players[0], players[1]], ['L', 'R']),
Duel([players[0], players[1]], ['Sp', 'R']),
Duel([players[0], players[1]], ['Sp', 'Sc'])
]),
Match([players[2], players[3]], [Duel([players[2], players[3]], ['P', 'P']),
Duel([players[2], players[3]], ['R', 'Sc']),
Duel([players[2], players[3]], ['R', 'P']),
Duel([players[2], players[3]], ['L', 'L']),
Duel([players[2], players[3]], ['Sc', 'Sc']),
Duel([players[2], players[3]], ['Sc', 'Sc']),
Duel([players[2], players[3]], ['R', 'Sp'])
]),
Match([players[4], players[5]], [Duel([players[4], players[5]], ['Sc', 'Sp']),
Duel([players[4], players[5]], ['P', 'R']),
Duel([players[4], players[5]], ['P', 'Sp']),
]),
Match([players[6], players[7]], [Duel([players[6], players[7]], ['R', 'L']),
Duel([players[6], players[7]], ['L', 'Sp']),
]),
Match([players[8], players[9]], [Duel([players[8], players[9]], ['P', 'Sc']),
Duel([players[8], players[9]], ['P', 'L']),
Duel([players[8], players[9]], ['R', 'Sp']),
]),
Match([players[10], players[11]], [Duel([players[10], players[11]], ['Sp', 'L']),
Duel([players[10], players[11]], ['R', 'R']),
Duel([players[10], players[11]], ['P', 'P']),
]),
Match([players[12], players[13]], [Duel([players[12], players[13]], ['Sp', 'P']),
Duel([players[12], players[13]], ['R', 'Sc']),
Duel([players[12], players[13]], ['L', 'R']),
]),
Match([players[14], players[15]], [Duel([players[14], players[15]], ['P', 'L']),
Duel([players[14], players[15]], ['Sp', 'Sc']),
Duel([players[14], players[15]], ['Sc', 'P']),
]),
]
strategies = [[s for m in round1 for d in m.duels for s in d.strategies]]
winningstrategies = [[d.winningstrategy for m in round1 for d in m.duels]]
losingstrategies = [[d.losingstrategy for m in round1 for d in m.duels]]
round2 = [Match([players[0], players[3]], [Duel([players[0], players[3]], ['Sp', 'L']),
Duel([players[0], players[3]], ['Sc', 'Sp']),
]),
Match([players[4], players[6]], [Duel([players[4], players[6]], ['R', 'Sc']),
Duel([players[4], players[6]], ['Sp', 'R']),
]),
Match([players[9], players[11]], [Duel([players[9], players[11]], ['P', 'P']),
Duel([players[9], players[11]], ['Sp', 'R']),
]),
Match([players[13], players[14]], [Duel([players[13], players[14]], ['L', 'L']),
Duel([players[13], players[14]], ['Sc', 'P']),
Duel([players[13], players[14]], ['R', 'R']),
]),
]
strategies.append([s for m in round2 for d in m.duels for s in d.strategies])
winningstrategies.append([d.winningstrategy for m in round2 for d in m.duels])
losingstrategies.append([d.losingstrategy for m in round2 for d in m.duels])
round3 = [Match([players[0], players[9]], [Duel([players[0], players[9]], ['R', 'Sc']),
Duel([players[0], players[9]], ['L', 'P']),
]),
Match([players[4], players[13]], [Duel([players[4], players[13]], ['L', 'P']),
Duel([players[4], players[13]], ['Sc', 'Sc']),
Duel([players[4], players[13]], ['R', 'P']),
Duel([players[4], players[13]], ['Sp', 'L']),
]),
]
strategies.append([s for m in round3 for d in m.duels for s in d.strategies])
winningstrategies.append([d.winningstrategy for m in round3 for d in m.duels])
losingstrategies.append([d.losingstrategy for m in round2 for d in m.duels])
round4 = [Match([players[0], players[13]], [Duel([players[0], players[9]], ['R', 'Sc']),
Duel([players[0], players[9]], ['L', 'P']),
])
]
strategies.append([s for m in round4 for d in m.duels for s in d.strategies ])
winningstrategies.append([d.winningstrategy for m in round4 for d in m.duels])
losingstrategies.append([d.losingstrategy for m in round3 for d in m.duels])
data = [s for l in strategies for s in l]
strategydict = dict((x,data.count(x)) for x in data)
dictplot(strategydict, 'allstrategies.png', 'All strategies')
data = strategies[0]
strategydict = dict((x,data.count(x)) for x in data)
dictplot(strategydict, 'round1.png', 'Strategies played in round 1')
data = strategies[1]
strategydict = dict((x,data.count(x)) for x in data)
dictplot(strategydict, 'round2.png', 'Strategies played in round 2')
data = strategies[2]
strategydict = dict((x,data.count(x)) for x in data)
dictplot(strategydict, 'round3.png', 'Strategies played in round 3')
data = [s for l in winningstrategies for s in l if s]
strategydict = dict((x,data.count(x)) for x in data)
dictplot(strategydict, 'winningstrategies.png', 'Winning strategies')
data = [s for l in losingstrategies for s in l if s]
strategydict = dict((x,data.count(x)) for x in data)
dictplot(strategydict, 'losingstrategies.png', 'Losing strategies')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment