Created
April 13, 2017 16:18
-
-
Save ghabs/34a4815d9d0cfea222cc07c0ccf734d6 to your computer and use it in GitHub Desktop.
Command Line Prisoner
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import getpass | |
import sys | |
class Player(object): | |
"""Player class that holds name and score""" | |
def __init__(self, name): | |
super(Player, self).__init__() | |
self.name = name | |
self.points = 0 | |
def add_points(self, points): | |
self.points += points | |
def lost_points(self, points): | |
self.points -= points | |
def get_score(self): | |
return self.points | |
def get_name(self): | |
return self.name | |
class Dilemma(object): | |
"""Dilemma contains rules, points, and inputs for multiple games""" | |
def __init__(self, player_one, player_two): | |
super(Dilemma, self).__init__() | |
self.player_one = player_one | |
self.player_two = player_two | |
self.games = {'P': 'prisoner', 'Q': 'Quit'} | |
def get_input(self, player_name): | |
dec = getpass.getpass(prompt="Does {0} Cooperate or Defect? (C or D)".format(player_name)) | |
print dec | |
if dec != 'C' and dec != 'D': | |
print('Please Input C (cooperate) or D (defect)') | |
dec = self.get_input(player_name) | |
return dec | |
def game_options(self): | |
type = raw_input('(P) Prisoner Dilemma \n(Q) Quit \n') | |
if type not in self.games: | |
print('Please select a defined game. \n') | |
self.game_options() | |
return | |
return self.games[type] | |
def prisoner(self): | |
dec_one = self.get_input(self.player_one.get_name()) | |
dec_two = self.get_input(self.player_two.get_name()) | |
if dec_one == 'C' and dec_two == 'C': | |
print('Dual Cooperation!') | |
self.player_one.add_points(1) | |
self.player_two.add_points(1) | |
elif dec_one == 'C' and dec_two == 'D': | |
print('Cooperation and Defection!') | |
self.player_one.add_points(0) | |
self.player_two.add_points(2) | |
elif dec_one == 'D' and dec_two == 'C': | |
print('Cooperation and Defection!') | |
self.player_one.add_points(2) | |
self.player_two.add_points(0) | |
else: | |
print('Dual Defection!') | |
self.player_one.lose_points(3) | |
self.player_two.lose_points(3) | |
print('{0} Score: {1}'.format(self.player_one.get_name(), self.player_one.get_score())) | |
print('{0} Two Score: {1}'.format(self.player_two.get_name(), self.player_two.get_score())) | |
def main(): | |
player_one = Player(sys.argv[1]) | |
player_two = Player(sys.argv[2]) | |
dilemma = Dilemma(player_one, player_two) | |
game = dilemma.game_options() | |
while game != 'Quit': | |
attr = getattr(dilemma, game) | |
attr() | |
game = dilemma.game_options() | |
print('Final Score \n {0}: {1} \n {2}: {3}'.format(player_one.get_name(), player_one.get_score(), player_two.get_name(), player_two.get_score())) | |
return | |
if __name__ == "__main__": | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment