I just started learning python so I'm not familiar with the various tricks or tools, or the proper way to word my question. Because of that, I was unable to find previous questions that do what I am looking for.
I have a working code outlined here:
import random
class UserGroup:
def __init__(self, users):
self.user_list = users
def random_users(self):
self.random_1 = random.choice(self.user_list)
self.random_2 = self.random_1
while self.random_2 == self.random_1:
self.random_2 = random.choice(self.user_list)
return self.random_1, self.random_2
class User:
def __init__(self, nickname, stats):
self.nickname = nickname
self.strength = stats['strength']
self.constitution = stats['constitution']
self.dexterity = stats['dexterity']
self.intelligence = stats['intelligence']
self.wisdom = stats['wisdom']
self.charisma = stats['charisma']
def __repr__(self):
return self.nickname
class Jared(User):
def fight_stat(self):
self.attack = self.strength + self.intelligence
self.defense = self.constitution * 2
self.speed = self.dexterity / 2
class Poptart(User):
def fight_stat(self):
self.attack = self.strength + self.dexterity
self.defense = self.dexterity
self.speed = self.dexterity + self.charisma
class Kaos(User):
def fight_stat(self):
self.attack = self.dexterity + self.wisdom
self.defense = self.wisdom * 2
self.speed = self.dexterity
class Magda(User):
def fight_stat(self):
self.attack = self.intelligence + self.charisma
self.defense = self.dexterity + self.charisma
self.speed = self.dexterity + self.constitution / 2
class Battle:
def __init__(self, user1, user2):
self.user1 = user1
self.user2 = user2
print(user1, "and", user2, "have entered the fight!")
def fight(self):
self.user1.fight_stat()
self.user2.fight_stat()
if self.user1.speed > self.user2.speed:
self.attacker = self.user1
self.defender = self.user2
elif self.user2.speed > self.user1.speed:
self.attacker = self.user2
self.defender = self.user1
elif self.user1.dexterity > self.user2.dexterity:
self.attacker = self.user1
self.defender = self.user2
else:
self.attacker = self.user2
self.defender = self.user1
if self.attacker.attack > self.defender.defense:
return self.attacker
elif self.defender.attack > self.attacker.defense:
return self.defender
else:
return "Draw"
# HERE STARTS BATTLE CODE
jared = Jared('Jarebear', {'strength': 7, 'constitution': 6, 'dexterity': 6, 'intelligence': 8, 'wisdom': 5, 'charisma': 5})
poptart = Poptart('Yung SLizzy', {'strength': 4, 'constitution': 5, 'dexterity': 10, 'intelligence': 7, 'wisdom': 5, 'charisma': 7})
kaos = Kaos('Kung Cows', {'strength': 8, 'constitution': 7, 'dexterity': 6, 'intelligence': 4, 'wisdom': 7, 'charisma': 4})
magda = Magda('Meghan M', {'strength': 7, 'constitution': 5, 'dexterity': 7, 'intelligence': 8, 'wisdom': 5, 'charisma': 5})
users = UserGroup([jared, poptart, kaos, magda])
for i in range(1,4):
print("Battle number", i)
battle = Battle(*users.random_users())
print("The winner is: ", battle.fight())
The example output is shown below:
Battle number 1
Jarebear and Kung Cows have entered the fight!
The winner is: Kung Cows
Battle number 2
Jarebear and Kung Cows have entered the fight!
The winner is: Kung Cows
Battle number 3
As I have written it, the code performs as expected. However, I am concerned about the way that I've implemented the fight()
method inside the Battle
class. I don't think the large sequence of if
statements is the proper way to say "user with higher speed attacks first". Logically, I just need a statement that is like self.attacker = max(self.user1.speed, self.user2.speed)
but the attacker is set to the user, not the user's speed. However, I don't know how to accomplish this in one or two lines of code in python.