Answered step by step
Verified Expert Solution
Question
1 Approved Answer
Python 2.7 #!/usr/bin/env python In this assignment you have been given a very simple framework that pits an army of Clones against an army
Python 2.7
#!/usr/bin/env python """ In this assignment you have been given a very simple framework that pits an army of Clones against an army of Robots. It is your job to create Clone and Robot classes that are compatible with the framework. The army of Robots will attack first. Each Robot will shoot randomly at Clones (up to 5 times) until it hits one for damage. Once all Robots have attempted to attack, it will be the Clones' turn. The army of Clones will attack second. Each Clone will shoot randomly at Robots (up to 5 times) until it hits one for damage. After a Clone hits a Robot, there is a 0.3% chance it will issue Order 66. This will cause all Clones to shoot themselves instead of their randomly chosen targets when they pull the trigger on their blasters. Once all Clones have attempted to attack, it will again be the Robots' turn. Once either all Clones are dead or all Robots are dead, the simulation will end. """ import os import time from random import random as R from random import choice as getRandom ###################################################################### # # [ YOUR CODE GOES HERE ] # ###################################################################### def printBattleStatus(cloneArmy, robotArmy): """ ACCEPTS: cloneArmy - a list of Clone objects robotArmy - a list of Robot objects RETURNS: Nothing Clears the screen and prints the hitpoints of all the clone soldiers on the left and all the robot soldiers on the right. Example output: Clones Robots ----------------------- [ 40 ] [ DEAD ] [ 25 ] [ DEAD ] [ 20 ] [ 20 ] [ DEAD ] [ 50 ] [ 35 ] [ DEAD ] [ 20 ] [ 80 ] [ 5 ] [ 40 ] [ 45 ] [ DEAD ] [ 40 ] [ DEAD ] [ 15 ] [ 20 ] """ os.system('clear') if Clone._order66 is True: print ' [ !! ORDER 66 !! ]' print ' Clones Robots' print '-----------------------' for clone, robot in zip(cloneArmy, robotArmy): print '[ %s ] [ %s ]' % (clone.hitpoints(), robot.hitpoints()) time.sleep(1) def allDead(army): """ ACCEPTS: army - a list of either Robot or Clone objects RETURNS: True False This function simply accepts a list of either Robot or Clone objects and returns True if all the soldiers are dead; otherwise it returns False """ for soldier in army: if soldier.dead() is False: return False return True def launchAttack(attackers, victims): """ ACCEPTS: attackers - a list of either Robot or Clone objects victims - a list of either Robot or Clone objects RETURNS: Nothing This function iterates through attackers and invokes the shoot(victim) method for each attacker. The victim is a randomly selected object from the victims list. If a victim is already dead, the attacker will attempt randomly choose another victim. If the attacker can't find an alive victim after 5 tries, it gives up and doesn't attack anybody. """ for attacker in attackers: # dead attackers can't attack if attacker.dead(): continue # target a victim that is not dead # try up to 5 times tries = 0 victim = getRandom(victims) while victim.dead(): victim = getRandom(victims) tries += 1 if tries > 4: return attacker.shoot(victim) if __name__ == '__main__': cloneArmy = [] robotArmy = [] # Create two armies, each with 10 soldiers # Clones have 100 HP and do 10 damage # Robots have 180 HP and do 5 damage for _ in xrange(10): cloneArmy.append(Clone(100, 10)) robotArmy.append(Robot(180, 5)) # Kick off the main event loop printBattleStatus(cloneArmy, robotArmy) while (not allDead(cloneArmy) and (not allDead(robotArmy))): # robot army attacks clone army 1st launchAttack(robotArmy, cloneArmy) # clone army attacks robot army 2nd launchAttack(cloneArmy, robotArmy) printBattleStatus(cloneArmy, robotArmy)(50 Points) In this assignment you have been given a very simple framework that pits an army of Clones against an army of Robots. It is your job to create Clone and Robot classes that are compatible with the framework. In this lab, you will be working with the attached file clone wars.py The army of Robots will attack first. Each Robot will shoot randomly at Clones (up to 5 times) until it hits one for damage. Once all Robots have attempted to attack, it will be the Clones' turin The army of Clones will attack second. Each Clone will shoot randomly at Robots (up to 5 times) until it hits one for damage. After a Clone hits a Robot, there is a 0.3% chance it will issue Order 66, This will cause all Clones to shoot themselves instead of their randomly chosen targets when they pull the trigger on their blasters. Once all Clones have attempted to attack, it will again be the Robots' turn Once either all Clones are dead or all Robots are dead, the simulation will end Robot is the simpler of the two classes you must implement, so we will discuss it first: Robots should have the following private attributes - hitpoints - the "life" of the robot solder. When this reaches zero, the robot dies -damage _ the damage dealt by the robot when it shoots and enemy - dead - boolean that tracks if the robot is dead Robots should have the following public methods takeDamage - This method should accept an integer representing an amount of damage inflicted upon the Robot. This amount of damage should be subtracted from the Robot's hitpoints. If this results in the Robots hitpoints being non-positive, then the Robot's dead attribute should be set. shoot - This method should accept a target, which could be either a Robot or Clone object. If the Robot is not dead, the target should take damage. (In other words, dead robots can't shoot) dead - This is a simple accessor method for returning the value of the Robot's private dead attribute - hitpoints - This method accepts no parameters and returns a string. If the Robot is not dead, the string should contain the Robot's current hitpoints formatted to 4 digits. If the Robot is dead, the returned string should simply contain DEAD
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started