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

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(); //...
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
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 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);                 /**         ...
A. Create a Dollar currency class with two integer attributes and one string attribute, all of...
A. Create a Dollar currency class with two integer attributes and one string attribute, all of which are non-public. The int attributes will represent whole part (or currency note value) and fractional part (or currency coin value) such that 100 fractional parts equals 1 whole part. The string attribute will represent the currency name. B. Create a CIS22C Dollar derived/inherited class with one additional non-public double attribute to represent the conversion factor from/to US Dollar. The value of the conversion...
Python please A string is one of most powerful data types in programming. A string object...
Python please A string is one of most powerful data types in programming. A string object is a sequence of characters and because it is a sequence, it is indexable, using index numbers starting with 0. Similar to a list object, a string object allows for the use of negative index with -1 representing the index of the last character in the sequence. Accessing a string object with an invalid index will result in IndexError exception. In Python a string...
Please provide Python code that does the following: 3) Write a program that takes a string...
Please provide Python code that does the following: 3) Write a program that takes a string as input, checks to see if it is comprised entirely of letters, and if all those letters are lower case. The output should be one of three possible messages: Your string is comprised entirely of lower case letters. Your string is comprised entirely of letters but some or all are upper case. Your string is not comprised entirely of letters. Your program may NOT:...
Challenge: Documents Description: Create a class in Python 3 named Document that has specified attributes and...
Challenge: Documents Description: Create a class in Python 3 named Document that has specified attributes and methods for holding the information for a document and write a program to test the class. Purpose: The purpose of this challenge is to provide experience creating a class and working with OO concepts in Python 3. Requirements: Write a class in Python 3 named Document that has the following attributes and methods and is saved in the file Document.py. Attributes __title is a...
Write the following Python code: A string X is an anagram of string Y if X...
Write the following Python code: A string X is an anagram of string Y if X can be obtained by arranging all characters of Y in some order, without removing any characters and without adding new characters. For example, each of the strings "baba", "abab", "aabb" and "abba" is an anagram of "aabb", and strings "aaab", "aab" and "aabc" are not anagrams of "aabb". A set of strings is anagram-free if it contains no pair of strings which are anagrams...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT