Question

Imagine you are creating a game in which robots on two teams battle each other. A robot has an energy attribute which determiWrite a class CompoundRobot that is a subclass of Robot which conforms to thefollowing specifications: 1. It has an init() me

0 0
Add a comment Improve this question Transcribed image text
Answer #1

The code is as below

class Robot:
def __init__(self, name=None):
self.name = name
self.energy = 20
self.compound = None

def __add__(self, otherRobot):
return CompoundRobot(self,otherRobot)
  
def attack(self, otherRobot, extraEnergy):
if self.energy > otherRobot.energy:
winner = self
looser = otherRobot
elif self.energy < otherRobot.energy:
winner = otherRobot
looser = self
else: # energy are equal
if extraEnergy >= self.energy:
self.energy = 1
else:
self.energy = self.energy - extraEnergy
return

if looser.energy <= extraEnergy:
winner.energy = winner.energy + (looser.energy - 1)
looser.energy = 1
else:
winner.energy = winner.energy + extraEnergy
looser.energy = looser.energy - extraEnergy

class CompoundRobot(Robot):
def __init__(self, robot1, robot2):
super().__init__(self)
self.components =[robot1,robot2]
self.energy = robot1.energy + robot2.energy
robot1.compound = self
robot2.compound = self

def iadd(self, robot):
self.components.append(robot)
self.energy = self.energy + robot.energy
self.compound = self
  
#ShouLd print:
#Robot 0 has name of None
# Robot 1 energy: 20
# Robot 2 energy: 20
robot0 = Robot()
robot1 = Robot('robotl')
robot2 = Robot('robot2')
robot3 = Robot('robot3')
print('Robot 0 has name of',robot0.name)
print('Robot 1 energy:', robot1.energy)
print('Robot 2 energy:', robot2.energy)

#Should print:
# Combined robot 1 and robot 2 into a combo robot named Brutus
# Brutus energy 40
# Brutus composed of robot1 and robot2
# Robot 1 component of Brutus
print("Combined robot 1 and robot 2 into a combo robot named Brutus")
compound_robot = robot1 + robot2
setattr(compound_robot,'name','Brutus')
print(compound_robot.name,'energy',compound_robot.energy)
print(compound_robot.name,'composed of',compound_robot.components[0].name,'and',compound_robot.components[1].name)
print('Robot 1 component of',getattr(robot1,'compound').name)

class Robot: 1 def_init_ (self, name=None): self.name = name 2 3 self.energy = 20 self.compound None 4 5 6 _add__(self, other

43 #ShouLd print: 44 #Robot 0 has name of None 45 # Robot 1 energy: 20 # Robot 2 energy: 20 robot0 = Robot() robot1 Robot(ro

73 input Robot 0 has name of None Robot 1 energy: 20 е& Robot 2 energy: 20 Combined robot 1 and robot 2 into a combo robot na

Add a comment
Know the answer?
Add Answer to:
Imagine you are creating a game in which robots on two teams battle each other. A...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Lab 10C - Creating a new class This assignment assumes that you have read and understood...

    Lab 10C - Creating a new class This assignment assumes that you have read and understood the following documents: http://www.annedawson.net/Python3_Intro_OOP.pdf http://www.annedawson.net/Python3_Prog_OOP.pdf and that you're familiar with the following example programs: http://www.annedawson.net/python3programs.html 13-01.py, 13-02.py, 13-03.py, 13-04.py Instructions: Complete as much as you can in the time allowed. Write a Python class named "Car" that has the following data attributes (please create your own variable names for these attributes using the recommended naming conventions): - year (for the car's year of manufacture)...

  • Use Java and please try and show how to do each section. You are creating a 'virtual pet' program...

    Use Java and please try and show how to do each section. You are creating a 'virtual pet' program. The pet object will have a number of attributes, representing the state of the pet. You will need to create some entity to represent attributes in general, and you will also need to create some specific attributes. You will then create a generic pet class (or interface) which has these specific attributes. Finally you will make at least one subclass of...

  • in python. automatic upvote for correct answer. This is my first exposure to OOP and I'm...

    in python. automatic upvote for correct answer. This is my first exposure to OOP and I'm really trying to grasp these concepts but they aren't coming fast. For Programming Assignment 11, it is required to include properly_formatted docstrings for Classes, as well as include properly formatted docstrings with purpose statements and signatures for all functions you define. If you don't include properly formatted docstrings with purpose statements and signatures for all functions you define for Programming Assignment 10, your Programming...

  • [python3] Start by using UML and planning the functionality of each method (including the constructor, that...

    [python3] Start by using UML and planning the functionality of each method (including the constructor, that is, the __init__() method) Initialization of your objects must require at least one parameter (in addition to self) All of your objects attributes/fields should be 'private' and you should include getters and setters for each attribute/field You must be able to print your object in a meaningful way and you should define what it means for two objects to be equal Additionally, your object...

  • 1. Assume you have a Car class that declares two private instance variables, make and model....

    1. Assume you have a Car class that declares two private instance variables, make and model. Write Java code that implements a two-parameter constructor that instantiates a Car object and initializes both of its instance variables. 2. Logically, the make and model attributes of each Car object should not change in the life of that object. a. Write Java code that declares constant make and model attributes that cannot be changed after they are initialized by a constructor. Configure your...

  • Python Classes - Write a class to determine the price of a salad and its ingredients....

    Python Classes - Write a class to determine the price of a salad and its ingredients. Write a Salad class. Your class will have two attributes: • name, which should be a string containing the name of the person who ordered the salad. • ingredients, which should be a set of strings where each string is an ingredient in the salad. __init__() The Salad class should have an __init__() method with four parameters, in the following order: • self •...

  • The class Engineer Test below is used to test two other class named Project & Engineer....

    The class Engineer Test below is used to test two other class named Project & Engineer. A project has three attributes: projNo (int), projName (string), revenue (double). Add a parameterized constructor, and setters & getters for all attributes. An engineer has four attributes: jobld (int), name (string), rate (double), and a list of projects. For each project, the engineer has a specific amount as profit (project revenue rate). Add a constructor that initializes the attributes: jobild, name and rate. Implement...

  • please answer "class playerexception(exception):" in python Notes Two players will face each other. They each decide...

    please answer "class playerexception(exception):" in python Notes Two players will face each other. They each decide independently to "cooperate" or "cheat". If they both cooperated, they each win two points. If they both cheated, nobody wins anything. one cheats, the cheater gets +3 and the cooperator loses a point. That wasn't very kind! One turn is defined as each player making a choice, and winning or losing some points as a result. Shared history against this player is available to...

  • Game Development: Uno For this assignment you will be creating the game of Uno (See the...

    Game Development: Uno For this assignment you will be creating the game of Uno (See the accompanying pdf for the instructions of the game). Your version will adhere to all the rules except that only the next player can issue a challenge against the previous player in regards to penalties, and your games must have at least three (3) players and at most nine (9) players. To begin, you must create the class Card which contains: Private string field named...

  • Program must be written in C++: Create a parent class called CompSciProfessor. Each professor has name...

    Program must be written in C++: Create a parent class called CompSciProfessor. Each professor has name (type string), email (type string), and facultyId (type long). Specialize the CompSciProfessor class into two more classes: AdjunctProf, and TenureTrackProf classes. The specialized class AdjunctProf has three attributes of its own: degree (type char), NoOfTA (type int), and NoOfCourses (type int). The attribute ‘degree’ refers to the degree of the adjunct professor. You assign ‘B’ to represent bachelor degree, ‘M’ for Master degree, and...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT