QuestionQuestion

See Question.pdf

Solution PreviewSolution Preview

This material may consist of step-by-step explanations on how to solve a problem or examples of proper writing, including the use of citations, references, bibliographies, and formatting. This material is made available for the sole purpose of studying and learning - misuse is strictly forbidden.

"""
Represents one move, indicating a choice to cooperate or not
"""
class Move:

    """
    constructor of a move: create/initialize the cooperate field.
    """
    def __init__(self, cooperate=True):
       self.cooperate = cooperate

    """
    return "." for a cooperating move, "x" for a non-cooperating move
    """
    def __str__(self):
       if self.cooperate:
            return "."
       else:
            return "x"

    """
    create/return a string as in this example: "Move(True)"
    """
    def __repr__(self):
       return "Move(" + str(self.cooperate) + ")"

    """
    return boolean: whether this move has same cooperate value as the other
    """
    def __eq__(self, other):
       if isinstance(other, Move) and self.cooperate == other.cooperate:
            return True
       else:
            return False

    """
    change the cooperation status of this move
    """
    def change(self):
       self.cooperate = not self.cooperate

    """
    create/return a new Move object that has the same cooperation status.
    """
    def copy(self):
       return Move(self.cooperate)


"""
Represents any exceptional situation for players, with a message
stored inside of it.
"""
class PlayerException(Exception):

    """
    create/initialize a msg field based on the parameter
    """
    def __init__(self, msg):
       self.msg = msg

    """
    Returns the error message
    """
    def __str__(self):
       return self.msg

    """
    return a string like this: "PlayerException('message contents')"
    """
    def __repr__(self):
       return "PlayerException('" + self.msg + "')"


"""
Represents one player, whose style string will resolve their behavior.
A Player object keeps track of the needed internal state:
previous moves of the opponent, their style, and their points.
"""
class Player:

    """
    create/initialize fields for each parameter.
    If history is None, then re-assign an empty list to it.
    """
    def __init__(self, style, points=0, history=None):
       self.style = style
       self.points = points
       if history is None:
            self.history = []
       else:
            self.history = history

       if style not in ["previous", "friend", "cheater", "grudger", "detective"]:
            raise PlayerException("no style '" + str(style) + "'.")

    """
    create/return a string (utilizing Move's str) like: "friend(12)+.+.+..."
    """
    def __str__(self):
       msg = self.style + "(" + str(self.points) + ")"
       for hist in self.history:
            msg += str(hist)
       return msg

    """
    create/return a string like: "Player('grudge', 12, [Move(True), Move(False)])"
    """
    def __repr__(self):
       msg = "Player('" + self.style + "', " + str(self.points)
       msg += ", ["
       idx = 0
       for hist in self.history:
            msg += repr(hist)
            if idx < len(self.history)-1:
                msg += ", "
            idx += 1
       msg += "])"
       return msg

    """
    reset history to an empty list. Don't change points
    """
    def reset_history(self):
       self.history = []

    """
    reset history, and also reset points to zero
    """
    def reset(self):
       self.reset_history()
       self.points = 0

    """
    increase points by the parameter. Might go negative
    """
    def update_points(self, amount):
       self.points += amount

    """
    return whether an opponent ever cheated, based on the history
    """
    def ever_betrayed(self):
       result = False
       for hist in self.history...
$40.00 for this solution

PayPal, G Pay, ApplePay, Amazon Pay, and all major credit cards accepted.

Find A Tutor

View available Python Programming Tutors

Get College Homework Help.

Are you sure you don't want to upload any files?

Fast tutor response requires as much info as possible.

Decision:
Upload a file
Continue without uploading

SUBMIT YOUR HOMEWORK
We couldn't find that subject.
Please select the best match from the list below.

We'll send you an email right away. If it's not in your inbox, check your spam folder.

  • 1
  • 2
  • 3
Live Chats