PYTHON! Need help fixing errors in roll playing game import random class DiceRol
ID: 3778310 • Letter: P
Question
PYTHON! Need help fixing errors in roll playing game
import random
class DiceRoller:
def roll(self, times, sides): # selects the number of the attack of the other player
total = 0
for i in range(times):
roll = random.randint(1, sides)
total += roll
return total
r=DiceRoller()
class Attack:
def __init__(self, name, number_of_die, sides_of_die, damage_type):
self._name = name # initializes name in the class
self._sides = sides_of_die # initializes sides of the dice
self._number = number_of_die # initializes the number of the dice
self._type = damage_type # initializes the name damage_type with damage
def get_attack_type(self): # the rest are the getters and setters of the class
return self._type
def get_damage(self):
damage_value = r
return damage_value
def get_name(self):
return self._name
def get_side(self):
return self._sides
def get_number(self):
return self._number
def set_name(self, newname):
self._name = newname
def set_side(self, newsides):
self._sides = newsides
def set_num_of_die(self, new_num_of_die):
self._number = new_num_of_die
def set_damage_type(self, newdamage):
self._type = newdamage
class Adventurer:
def __init__(self,name, hit_points, defense, magic_defense, initiative):
self._hit_points= hit_points # initializes the _hitpoints with a variable
self._name = name # initializes the the private namme to name
self._defense= defense # initializes the private dense with defense
self._magic_defense = magic_defense # initializes the private magic with the name
self._initiative= initiative # initializes the private initiative witht the name
def is_alive(self):
return self._hit_points > 0 # checking if the hit points is greater the 0
# if the hit points is greater the 0 then they are still alive
def roll_initiative(self):
return random.randint(0,self._initiative) # randomly picks a number from 0 to the initiative
def take_damage(self, amount, damage_type):
damage= Attack.get_damage() # calls the function(get_damage) from the attack class
if damage_type == 'PHYSICAL': # checks the status of the damage type
total_damage= self._hit_points-(damage - self._defense) # reduce the hit points by the damage
else: # if not physical then magic hit
total_damage =self._hit_points -(damage - self._magic_defense) # reduces the hit points by the magic damage
class Fighter(Adventurer):
_HP = 40
_DEF = 10
_MAG_DEF = 4
def __init__(self, name, initiative):
super().__init__(name, Fighter._HP, Fighter._DEF, Fighter._MAG_DEF, initiative) # calls from Adventurer(super)
self.melee = Attack("SLASH", 1, 8, "PHYSICAL") # creates a new object called melee
def strike(self):
getdamage=Attack.get_damage(self) # calls the object .get_damage from the attack class
attacktype = Attack.get_attack_type(self) # calls the object get_attack_type from the attack class
print(self._name + ' attacks with Slash for ' + getdamage + ''+ attacktype)
return (getdamage,attacktype) # returns the tuple with the called objects
def __str__(self): # prints an update of the hit
print(self._name + 'with' + str(self._hit_points) + 'hit points and a Slash attack'+ str( Attack._number ) + str( Attack._sides ))
class Wizard(Adventurer):
_HP = 20
_DEF = 4
_MAG_DEF = 10
def __init__(self, name, initiative):
super().init(name, Wizard.HP, Wizard.DEF, Wizard.MAG_DEF, initiative) # calls from Adventurer class
self.melee = Attack("FIREBALL", 3, 6, "MAGIC") # creates an object
def cast(self):
getdamage = Attack.get_damage(self) # gets the object get_damage from the attack class
attacktype = Attack.get_attack_type(self) # gets the object get_attack from thr attack class
print(self._name + ' attacks with Fireball for ' + str(getdamage) + '' + str(attacktype))
return (getdamage, attacktype) # returns the tuple with the called objects
def __str__(self): # prints an update of the hit from the wizard to the fighter
print(self._name + 'with' + self._hit_points + 'hit points and a Fireball attack' + Attack._number + Attack._sides)
if __name__ == "__main__":
a = Fighter("Aragorn", 20)
print("Created: ", a)
b = Wizard("Humphrey", 50)
print("Created: ", b)
rounds = 0
while a.is_alive() and b.is_alive():
while a.roll_initiative() == b.roll_initiative():
a.roll_initiative()
b.roll_initiative()
if a.roll_initiative() > b.roll_initiative():
b.take_damage(a.strike())
round += 1
else:
a.take_damage(b.cast())
round += 1
for something in somethingelse:
rounds += 1
print("rounds: " , rounds)
if a.is_alive():
print('Aragon Won')
else:
print('Humphrey Won') # add the remaining hit points
Explanation / Answer
from random import randint
class Character:
def __init__(self):
self.name = ""
self.health = 1
self.health_max = 1
def do_damage(self, enemy):
damage = min(
max(randint(0, self.health) - randint(0, enemy.health), 0),
enemy.health)
enemy.health = enemy.health - damage
if damage == 0: print "%s evades %s's attack." % (enemy.name, self.name)
else: print "%s hurts %s!" % (self.name, enemy.name)
return enemy.health <= 0
class Enemy(Character):
def __init__(self, player):
Character.__init__(self)
self.name = 'a goblin'
self.health = randint(1, player.health)
class Player(Character):
def __init__(self):
Character.__init__(self)
self.state = 'normal'
self.health = 10
self.health_max = 10
def quit(self):
print "%s can't find the way back home, and dies of starvation. R.I.P." % self.name
self.health = 0
def help(self): print Commands.keys()
def status(self): print "%s's health: %d/%d" % (self.name, self.health, self.health_max)
def tired(self):
print "%s feels tired." % self.name
self.health = max(1, self.health - 1)
def rest(self):
if self.state != 'normal': print "%s can't rest now!" % self.name; self.enemy_attacks()
else:
print "%s rests." % self.name
if randint(0, 1):
self.enemy = Enemy(self)
print "%s is rudely awakened by %s!" % (self.name, self.enemy.name)
self.state = 'fight'
self.enemy_attacks()
else:
if self.health < self.health_max:
self.health = self.health + 1
else: print "%s slept too much." % self.name; self.health = self.health - 1
def explore(self):
if self.state != 'normal':
print "%s is too busy right now!" % self.name
self.enemy_attacks()
else:
print "%s explores a twisty passage." % self.name
if randint(0, 1):
self.enemy = Enemy(self)
print "%s encounters %s!" % (self.name, self.enemy.name)
self.state = 'fight'
else:
if randint(0, 1): self.tired()
def flee(self):
if self.state != 'fight': print "%s runs in circles for a while." % self.name; self.tired()
else:
if randint(1, self.health + 5) > randint(1, self.enemy.health):
print "%s flees from %s." % (self.name, self.enemy.name)
self.enemy = None
self.state = 'normal'
else: print "%s couldn't escape from %s!" % (self.name, self.enemy.name); self.enemy_attacks()
def attack(self):
if self.state != 'fight': print "%s swats the air, without notable results." % self.name; self.tired()
else:
if self.do_damage(self.enemy):
print "%s executes %s!" % (self.name, self.enemy.name)
self.enemy = None
self.state = 'normal'
if randint(0, self.health) < 10:
self.health = self.health + 1
self.health_max = self.health_max + 1
print "%s feels stronger!" % self.name
else: self.enemy_attacks()
def enemy_attacks(self):
if self.enemy.do_damage(self): print "%s was slaughtered by %s!!! R.I.P." %(self.name, self.enemy.name)
Commands = {
'quit': Player.quit,
'help': Player.help,
'status': Player.status,
'rest': Player.rest,
'explore': Player.explore,
'flee': Player.flee,
'attack': Player.attack,
}
p = Player()
p.name = raw_input("What is your character's name? ")
print "(type help to get a list of actions) "
print "%s enters a dark cave, searching for adventure." % p.name
while(p.health > 0):
line = raw_input("> ")
args = line.split()
if len(args) > 0:
commandFound = False
for c in Commands.keys():
if args[0] == c[:len(args[0])]:
Commands[c](p)
commandFound = True
break
if not commandFound:
print "%s doesn't understand the suggestion." % p.name
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.