Question

In: Computer Science

# Mini-Exercise 4: Classes practice # Complete the three methods that say 'your code here' in...

# Mini-Exercise 4: Classes practice
# Complete the three methods that say 'your code here' in the classes below

class Character:
    '''
    This class represents a character in a game.
    '''

    def __init__(self, name, health=100):
        '''(str, int) -> Character
        Construct a character with the name and health provided.
        If health is not provided, a default health of 100 is used.
        '''

        self.name = name
        self.health = health

    def __str__(self):
        '''() -> str
        Return a string representation of this character.
        '''

        return "{}, health = {}".format(self.name, self.health)

    def is_alive(self):
        '''() -> bool
        Return True iff the character has health greater than 0.
        '''

        # YOUR CODE HERE
        pass

    def punch(self, opponent):
        '''(Character) -> None
        Given an opponent Character, punch that opponent (decrease
        that opponent's health by 10). If decreasing by 10 would
        make the opponent's health less than 0, just set the health
        to 0 instead. (That is, the health should never be a negative
        number.
        '''
        
        # YOUR CODE HERE
        pass

class Game:

    def __init__(self, characters):
        '''(Game, list of Characters) -> None
        Construct a new Game with the given list of Characters.
        '''

        self.characters = characters

    def add_character(self, c):
        '''(Game, Character) -> None
        Given a Character c, add c to this game's list of characters.
        '''

        self.characters.append(c)

    def revive_characters(self):
        '''(Game) -> None
        Revive any character in the Game's list of characters whose health
        is 0 by increasing their health to 100.
        '''

        # YOUR CODE HERE
        pass

    
# Some lines to try out your code;feel free to add more
if __name__ == "__main__":
    character1 = Character("Wizard", 500)
    character2 = Character("Monster")
    print(character1)
    print(character2)
    character1.punch(character2)
    print(character1.health)
    print(character2.health)

Solutions

Expert Solution

CODE

class Character:

'''

This class represents a character in a game.

'''

def __init__(self, name, health=100):

'''(str, int) -> Character

Construct a character with the name and health provided.

If health is not provided, a default health of 100 is used.

'''

self.name = name

self.health = health

def __str__(self):

'''() -> str

Return a string representation of this character.

'''

return "{}, health = {}".format(self.name, self.health)

def is_alive(self):

'''() -> bool

Return True iff the character has health greater than 0.

'''

return self.health > 0

def punch(self, opponent):

'''(Character) -> None

Given an opponent Character, punch that opponent (decrease

that opponent's health by 10). If decreasing by 10 would

make the opponent's health less than 0, just set the health

to 0 instead. (That is, the health should never be a negative

number.

'''

opponent.health -= 10;

if opponent.health < 0:

opponent.health = 0

class Game:

def __init__(self, characters):

'''(Game, list of Characters) -> None

Construct a new Game with the given list of Characters.

'''

self.characters = characters

def add_character(self, c):

'''(Game, Character) -> None

Given a Character c, add c to this game's list of characters.

'''

self.characters.append(c)

def revive_characters(self):

'''(Game) -> None

Revive any character in the Game's list of characters whose health

is 0 by increasing their health to 100.

'''

for character in self.characters:

if character.health == 0:

character.health += 100

# Some lines to try out your code;feel free to add more

if __name__ == "__main__":

character1 = Character("Wizard", 500)

character2 = Character("Monster")

print(character1)

print(character2)

character1.punch(character2)

print(character1.health)

print(character2.health)


Related Solutions

4.9 Branching Practice - Python Exercise #Write the code for the following # Exercise 1 #...
4.9 Branching Practice - Python Exercise #Write the code for the following # Exercise 1 # Ask user for a number using an input statement (no prompt) & assign the input to a variable # Convert the user entry to an integer and assign it to a variable # Condition: user entry is greater than zero # Action: display the following word on screen: positive # Exercise 2 # Ask user for a number using an input statement (no prompt)...
1. Agile Methods Practice Problems. Complete these problems, showing your work. 1a. At the end of...
1. Agile Methods Practice Problems. Complete these problems, showing your work. 1a. At the end of Day 1 of a sprint there are 90 story points worth of work remaining, and at the end of Day 6 there are 40 story points of work remaining. What is the project’s current velocity, in units of story points per day? (Show your work) 1b. A project whose sprint velocity is 12 story points per day at the end of Day 4, and...
Your primary task for this exercise is to complete header file by writing three functions with...
Your primary task for this exercise is to complete header file by writing three functions with its description below: removeAt function – to remove the item from the list at the position specified by location. Because the list elements are in no particular order (unsorted list), you could simple remove the element by swapping the last element of the list with the item to be removed and reducing the length of the list. insertAt function - to insert an item...
4.10 Branching Practice 2 - Python Exercise #Write the code for the following #Exercise 1 #Ask...
4.10 Branching Practice 2 - Python Exercise #Write the code for the following #Exercise 1 #Ask the user to input a name using an input statement (no prompt) and assign it to a variable #Condition: user input is equal to 'Joseph' #Action: display the user input on screen #Exercise 2 #Ask the user to input his/her first name using an input statement (no prompt) and assign it to a variable #Condition: user input is equal to 'Joseph' #Action: display the...
Briefly explain the three classes of creditors specified in the Bankruptcy Code.
Briefly explain the three classes of creditors specified in the Bankruptcy Code.
i have attached my code here and we are supposed to create two classes. one is...
i have attached my code here and we are supposed to create two classes. one is date the other switches accounts for bank and then displays the bank account,type,name,date(pulled from first class and then prints out. i am having issues getting my code to pull from the first class and dont know how to make it read the data from date class if someone can look at my code and tell me how to fix this i would greatly appreciate...
I'm very confused on how to complete this code. There are 5 total classes. I have...
I'm very confused on how to complete this code. There are 5 total classes. I have some written but it isn't quite working and the Band class especially is confusing me. I posted the entire thing since the band and manager class refer to the other classes' constructors and thought it would be helpful to post those as well. Person class is a base class: Private variables String firstname, String lastname, int age Constructor has the parameters String firstname, String...
//Complete the incomplete methods in the java code //You will need to create a driver to...
//Complete the incomplete methods in the java code //You will need to create a driver to test this. public class ManagedArray { private int[] managedIntegerArray; //this is the array that we are managing private int maximumSize; //this will hold the size of the array private int currentSize = 0; //this will keep track of what positions in the array have been used private final int DEFAULT_SIZE = 10; //the default size of the array public ManagedArray()//default constructor initializes array to...
Machine Learning - multivariate methods Let us say in two dimensions, we have two classes with...
Machine Learning - multivariate methods Let us say in two dimensions, we have two classes with exactly the same mean. What type of boundaries can be defined? show a picture of the options
1. Before completing this exercise, review Chapters 3 and 4 of your SPSS textbook and complete...
1. Before completing this exercise, review Chapters 3 and 4 of your SPSS textbook and complete the example in each chapter (the example starts at the beginning of the chapter) to learn how to create a frequency distribution and a histogram. Using these new skills: Use SPSS to create a frequency distribution of the Body Mass Index (BMI) of a random sample of 30 adults diagnosed with the depression (Table 1). Take a screenshot of the output (make sure the...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT