Question

In: Computer Science

PYTHON PLEASE: Construct a class “Monster” with the following attributes: self.name (a string) self.type (a string,...

PYTHON PLEASE:

Construct a class “Monster” with the following attributes:

  1. self.name (a string)
  2. self.type (a string, default is ‘Normal’)
  3. self.current_hp (int, starts out equal to max_hp)
  4. self.max_hp (int, is given as input when the class instance is created, default is 20)
  5. self.exp (int, starts at 0, is increased by fighting)
  6. self.attacks (a dict of all known attacks)
  7. self.possible_attacks (a dictionary of all possible attacks)

The dictionary of possible_attacks will map the name of an attack (the key) to how many points of damage the attack does. These are all the possible attacks with their corresponding damage points.

  1. sneak_attack: 1
  2. slash: 2
  3. ice_storm: 3
  4. fire_storm: 3
  5. whirlwind: 3
  6. earthquake: 2
  7. double_hit: 4
  8. wait: 0

Every monster will start out with only the “wait” attack within self.attacks, but you will need to construct the method add_attacks and method remove_attacks. Both methods will take in an attack_name as a parameter.

A monster can only to have a max of four attacks at a time. If you add an attack when the monster already has four, the weakest one should be dropped automatically. If there is a tie for weakest attack, drop the attack that comes first alphabetically. If adding the attack ended successfully return True if you try to add an invalid attack return False.

If all of a monster’s attacks are removed, “wait” should automatically be added again, so that every monster always has at least 1 attack. If removing an attack ended successfully return True if you try to remove an invalid or an attack which has not been learned return False.

Solutions

Expert Solution

Monster with attacks

-----------------------------------------------------code start-------------------------------------------------------------------------------------------

class Monster:
def __init__(self, name, typee = 'Normal', max_hpm = 20, exp = 0):
self.name = name
self.type = typee
self.current_hp = max_hpm
self.max_hp = max_hpm
self.exp = 0
self.attacks = {"wait":0}
self.possible_attacks = {"sneak_attack": 1,"slash": 2,"ice_storm": 3,"fire_storm": 3,"whirlwind": 3,"earthquake": 2,"double_hit": 4,"wait": 0}
  
def __repr__(self):
return f" monster {self.name} has attacks {list(self.attacks.keys())}"
  
def add_attacks(self,attack):
if "wait" in self.attacks.keys():
del self.attacks["wait"]
if len(self.attacks) < 4 and (attack not in self.attacks.keys()):
self.attacks.update({attack:self.possible_attacks[attack]})
return True
elif attack not in self.attacks.keys():
# len(self.attacks) > 4:
# print("delete and insert")
min_val = sorted(list(self.attacks.values()))
mini = min_val[0]
min_val_count = min_val.count(mini)
# print(mini, min_val_count)
if min_val_count == 1:
delete_key = list(self.attacks.keys())[list(self.attacks.values()).index(mini)]
# print(delete_key)
del self.attacks[delete_key]
self.attacks.update({attack:self.possible_attacks[attack]})
elif min_val_count > 1:
keys = []
for k,v in self.attacks.items():
if v == mini:
keys.append(k)
# print("unsorted",keys)
keys = sorted(keys)
print(keys)
# print("deleting alpha",keys[0])
del self.attacks[keys[0]]
self.attacks.update({attack:self.possible_attacks[attack]})
return True
else:
return False
  
def remove_attack(self,attack):
if len(self.attacks) != 0 and (attack in self.attacks.keys()):
del self.attacks[attack]
if len(self.attacks) == 0:
self.attacks.update({"wait":0})
return True
else:
return False

--------------------------------------------------------end-----------------------------------------------------------------------------------------

readme usage:

1) creating an object: cj = Monster("cj")

2) Adding attack: (it follows the guidelines and constraints you provided)

  • adding the same attack returns false
  • to check the status of monster:
  • Adding after the attack dict is full. (it sorts alpha and deletes the attack with min power) <here min is 3 and fire_storm comes first>
    • and the attacks become {'whirlwind': 3, 'ice_storm': 3, 'double_hit': 4, 'slash': 2}
    • DELETES the attack with least power
  • Removing: removes if the attack is owned in attack dict else returns False
    • if the attack dict is empty, it is updated with {wait:0}
    • after removing all
    • ,

Please rate my answer, if you found it useful and If you still need the jupyter notebook.


Related Solutions

Please, how to construct a class template in Python? Class name is animal: give it a...
Please, how to construct a class template in Python? Class name is animal: give it a set of class variables called: gender, status give it a set of class private variables: __heart, __lungs, __brain_size construct methods to write and read the private class variables in addition initiate in the constructor the following instance variables: name, location as well as initiate the variables status and gender all from constructor parameters The variables meaning is as follows: name is name status is...
Implement a class Student, including the following attributes and methods: Two public attributes name(String) and score...
Implement a class Student, including the following attributes and methods: Two public attributes name(String) and score (int). A constructor expects a name as a parameter. A method getLevel to get the level(char) of the student. score level table: A: score >= 90 B: score >= 80 and < 90 C: score >= 60 and < 80 D: score < 60 Example:          Student student = new Student("Zack"); student.score = 10; student.getLevel(); // should be 'D'. student.score = 60; student.getLevel(); //...
Given: class Monster {     private:     string name;     int dangerLevel;     public:     Monster(sting, int);     virtual void hunt()
Given: class Monster {     private:     string name;     int dangerLevel;     public:     Monster(sting, int);     virtual void hunt() = 0;     virtual void fight(Monster&);     string getName() const; }; class GiantMonster : public Monster {     protected:         int height;          public:         GiantMonster(string, int, int);         virtual void trample(); }; class Dinosaur : public GiantMonster {     public:     Dinosaur(string, int, int);     void hunt();     void roar(); }; class Kraken : protected GiantMonster {     public:     Kraken(string, int, int);     virtual void hunt();     void sinkShip(); }; Indicate if the code snippets below are...
In C++ Demonstrate inheritance. Create an Airplane class with the following attributes: • manufacturer : string...
In C++ Demonstrate inheritance. Create an Airplane class with the following attributes: • manufacturer : string • speed : float Create a FigherPlane class that inherits from the Airplane class and adds the following attributes: • numberOfMissiles : short
Create a class Employee. Your Employee class should include the following attributes: First name (string) Last...
Create a class Employee. Your Employee class should include the following attributes: First name (string) Last name (string) Employee id (string) Employee home street address (string) Employee home city (string) Employee home state (string) Write a constructor to initialize the above Employee attributes. Create another class HourlyEmployee that inherits from the Employee class.   HourEmployee must use the inherited parent class variables and add in HourlyRate and HoursWorked. Your HourEmployee class should contain a constructor that calls the constructor from the...
Create a class Client. Your Client class should include the following attributes: Company Name (string) Company...
Create a class Client. Your Client class should include the following attributes: Company Name (string) Company id (string) Billing address (string) Billing city (string) Billing state (string) Write a constructor to initialize the above Employee attributes. Create another class HourlyClient that inherits from the Client class.   HourClient must use the inherited parent class variables and add in hourlyRate and hoursBilled. Your Hourly Client class should contain a constructor that calls the constructor from the Client class to initialize the common...
In python- Create a class defined for Regression. Class attributes are data points for x, y,...
In python- Create a class defined for Regression. Class attributes are data points for x, y, the slope and the intercept for the regression line. Define an instance method to find the regression line parameters (slope and intercept). Plot all data points on the graph. Plot the regression line on the same plot.
Using Python Define a Student class. A Student class needs to have two attributes, the student_name...
Using Python Define a Student class. A Student class needs to have two attributes, the student_name and strudent_grade . It also has three methods as follows: set_grade(grade) that sets the grade of the student. get_grade returns the grade of the student. print_student_info that print the name and grade of student in a formatted string. In the math_utils.py file define a function called average_grade(roster) . This method accepts as input a list of Student Objects and returns the average of the...
Using python class: Design a class called Account CLASS NAME:    Account ATTRIBUTES: -nextAcctID: int    #NOTE-...
Using python class: Design a class called Account CLASS NAME:    Account ATTRIBUTES: -nextAcctID: int    #NOTE- class-level attribute initialized to 1000 -acctID: int -bank: String -acctType: String (ex: checking, savings) -balance: Float METHODS: <<Constructor>>Account(id:int, bank: String, type:String, bal:float) +getAcctID(void): int                        NOTE: retrieving a CLASS-LEVEL attribute! -setAcctID(newID: int): void           NOTE: PRIVATE method +getBank(void): String +setBank(newBank: String): void +getBalance(void): float +setBalance(newBal: float): void +str(void): String NOTE: Description: prints the information for the account one item per line. For example: Account #:        ...
Could you do it with python please? public class BSTTraversal {         public static void main(String[]...
Could you do it with python please? public class BSTTraversal {         public static void main(String[] args) {                 /**                  * Creating a BST and adding nodes as its children                  */                 BSTTraversal binarySearchTree = new BSTTraversal();                 Node node = new Node(53);                 binarySearchTree.addChild(node, node, 65);                 binarySearchTree.addChild(node, node, 30);                 binarySearchTree.addChild(node, node, 82);                 binarySearchTree.addChild(node, node, 70);                 binarySearchTree.addChild(node, node, 21);                 binarySearchTree.addChild(node, node, 25);                 binarySearchTree.addChild(node, node, 15);                 binarySearchTree.addChild(node, node, 94);                 /**         ...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT