You are on page 1of 8

from models.

unit import unit


import random

class main:
def __init__(self):
self.gameName = 'BattleGame'
self.playerUnits = [
]
self.aiUnits = [

]
self.no_of_units = 3

def play_game(self):
try:
print(self.gameName + " - Started")

#Next action - Game Setup


self.setup_game(teamName="PlayerTeam")
self.setup_game(teamName="AITeam")

# Attack
round = 0
NextAttacker = "PlayerTeam" #default
cont = True
while cont==True:
if NextAttacker == "PlayerTeam":
round += 1
print("\n")
print("Round-" + str(round))
self.player_attack()
self.remove_ai_unit() #after attach, check for remove of a unit
from the target team
NextAttacker = "AITeam"
else:
print("\n")
self.ai_attack()
self.remove_player_unit() #after attach, check for remove of a
unit from the target team
NextAttacker = "PlayerTeam"

self.print_teams_current_score()

if self.get_playerteam_active_unit_count() == 0:
print("\n")
print("The winner of the battle game is AI-Team")
print("Game Over")
cont = False

elif self.get_aiteam_active_unit_count() == 0:
print("\n")
print("The winner of the battle game is Player-Team")
print("Game Over")
cont = False

except Exception as ex:


#print(colored("ERROR:" + str(ex), "red"))
print(str(ex))
def print_teams_current_score(self):
try:
print("PlayerTeam's Current Score:")
print("__________________________")
for i in range(len(self.playerUnits)):
tmpUnit = self.playerUnits[i]
myvalue = str(i + 1) + ". Name: " + tmpUnit.name + "; HealthPoint:
" + str(tmpUnit.healthPoint) + "; Status: " + str(tmpUnit.status)
print(myvalue)

print("AITeam's Current Score:")


print("_______________________")
for i in range(len(self.aiUnits)):
tmpUnit = self.aiUnits[i]
myvalue = str(i + 1) + ". Name: " + tmpUnit.name + "; HealthPoint:
" + str(tmpUnit.healthPoint) + "; Status: " + str(tmpUnit.status)
print(myvalue)

except Exception as ex:


#print(coloured("ERROR:" + str(ex), "red"))
print(str(ex))
def get_playerteam_active_unit_count(self):
active_units = 0
try:
for unit in self.playerUnits:
if str(unit.status).lower() == "active":
active_units +=1
except Exception as ex:
#print(colored(str(ex), "red"))
print(str(ex))
finally:
return active_units

def get_aiteam_active_unit_count(self):
active_units = 0
try:
for unit in self.aiUnits:
if str(unit.status).lower() == "active":
active_units +=1
except Exception as ex:
#print(colored(str(ex), "red"))
print(str(ex))
finally:
return active_units

def setup_game(self, teamName):


try:
if teamName == "PlayerTeam":
self.no_of_units = int(input("Enter the Number of Units
(Players)"))

for i in range(self.no_of_units):
unitClass = unit()

if teamName == "PlayerTeam":
unitClass.name = input("Enter the name of the unit-" + str(i+1)
+ " of the PlayerTeam")
else:
random_number = random.randint(1,99)
unitClass.name = "AI" + str(random_number).zfill(2)

unitClass.teamName = teamName
unitClass.profession = self.get_profession(teamName)

if unitClass.profession == "Warrior":
unitClass.healthPoint = 25 #100
unitClass.attackPoint = random.randint(5,20)
unitClass.defencePoint = random.randint(1,10)
unitClass.experience = 0
unitClass.rank = 1

else:
unitClass.healthPoint = 25 #100
unitClass.attackPoint = random.randint(1,10)
unitClass.defencePoint = random.randint(5,15)
unitClass.experience = 0
unitClass.rank = 1

#Generate a list container


if teamName == "PlayerTeam":
self.playerUnits.append(unitClass)
else:
self.aiUnits.append(unitClass)
except Exception as ex:
# print(colored(str(ex), "red"))
print(str(ex))
def player_attack(self):
try:
player_unit_name = input("Enter a UnitName from PlayerTeam [" +
self.get_playerteam_players() + "] to attack: ")
ai_unit_name = input("Enter a UnitName from AITeam [" +
self.get_aiteam_players() + "] for defence: ")

#get attacker unit


attacker_unit = self.get_specific_player_unit(player_unit_name)
attacker_ATK = attacker_unit.attackPoint
attacker_EXP = attacker_unit.experience
attacker_Rank = attacker_unit.rank

# get target unit


target_unit = self.get_specific_ai_unit(ai_unit_name)
target_DEF = target_unit.defencePoint
target_HP = target_unit.healthPoint
target_EXP = target_unit.experience
target_Rank = target_unit.rank

damage = 0 #initialization/No effect


target_damage_severity = input("How badly damaged for target team unit
(Severe/Mild)")

if str(target_damage_severity).lower() == "severe":
damage = (attacker_ATK - target_DEF) + random.randint(1, 10)
target_HP -= damage
print("\n")
print("Damage for AITeam member:" + str(damage))

attacker_EXP += damage
target_EXP += target_DEF

#extra experience
if damage>10:
target_EXP += (target_DEF * 20)/100
elif damage<0:
target_EXP += (target_DEF * 50) / 100

#level up
if attacker_EXP >= 100:
attacker_Rank +=1
attacker_EXP -= 100

if target_EXP >= 100:


target_Rank +=1
target_EXP -= 100

# set the new value of attacker unit


attacker_unit.rank = attacker_Rank
attacker_unit.experience = attacker_EXP

# set the new value of target unit


target_unit.rank = target_Rank
target_unit.experience = target_EXP
target_unit.healthPoint = target_HP
except Exception as ex:
#print(colored(str(ex), "red"))
print(str(ex))
def ai_attack(self):
try:
ai_unit_name = input("Enter a UnitName from AITeam [" +
self.get_aiteam_players() + "] to attack: ")
player_unit_name = input("Enter a UnitName from PlayerTeam [" +
self.get_playerteam_players() + "] for defence: ")

#get attacker unit


attacker_unit = self.get_specific_ai_unit(ai_unit_name)
target_unit = self.get_specific_player_unit(player_unit_name)

if attacker_unit != {} and target_unit != {}: #Ensure, there is a


active member to play on both teams
ai_unit_name = attacker_unit.name
player_unit_name = target_unit.name

attacker_ATK = attacker_unit.attackPoint
attacker_EXP = attacker_unit.experience
attacker_Rank = attacker_unit.rank

target_DEF = target_unit.defencePoint
target_HP = target_unit.healthPoint
target_EXP = target_unit.experience
target_Rank = target_unit.rank

target_damage_severity = input("How badly damaged for target team


unit (Severe/Normal)")

damage = 0 #initialization/No effect


if str(target_damage_severity).lower() == "severe":
damage = (attacker_ATK - target_DEF) + random.randint(1, 10)
target_HP -= damage
print("\n")
print("Damage for PlayerTeam member:" + str(damage))

attacker_EXP += damage
target_EXP += target_DEF

#extra experience
if damage>10:
target_EXP += (target_DEF * 20)/100
elif damage<0:
target_EXP += (target_DEF * 50) / 100

#level up
if attacker_EXP >= 100:
attacker_Rank +=1
attacker_EXP -= 100

if target_EXP >= 100:


target_Rank +=1
target_EXP -= 100

# set the new value of attacker unit


attacker_unit.rank = attacker_Rank
attacker_unit.experience = attacker_EXP

# set the new value of target unit


target_unit.rank = target_Rank
target_unit.experience = target_EXP
target_unit.healthPoint = target_HP
except Exception as ex:
#print(colored(str(ex), "red"))
print(str(ex))
def get_random_player_unit(self):
unit = {}
try:
tmp_playerUnits = []
for unit in self.playerUnits:
if str(unit.status).lower() == "active":
tmp_playerUnits.append(unit)

if len(tmp_playerUnits) > 0:
random_number = random.randint(1, len(tmp_playerUnits))
unit = self.playerUnits[random_number - 1]
except Exception as ex:
#print(colored(str(ex), "red"))
print(str(ex))
finally:
return unit

def get_specific_player_unit(self, playerName):


unitValue = {}
try:
for unit in self.playerUnits:
if unit.status.lower() == "active" and unit.name.lower() ==
playerName.lower():
unitValue = unit
break
except Exception as ex:
#print(colored(str(ex), "red"))
print(str(ex))
finally:
return unitValue

def set_player_unit(self, name, revisedUnit):


try:
for unit in self.playerUnits:
if str(unit.name).lower() == str(name).lower():
unit = revisedUnit
break
except Exception as ex:
# print(colored(str(ex), "red"))
print(str(ex))
def remove_player_unit(self):
try:
for unit in self.playerUnits:
if unit.healthPoint <= 0 and str(unit.status) == "active":
unit.status = "dead"
print(colored("PlayerUnit - " + unit.name + " has been removed
due to lower HealthPoint","red"))
break
except Exception as ex:
#print(colored(str(ex), "red"))
print(str(ex))
def get_random_ai_unit(self):
unit = {}

tmp_aiUnits = []
for unit in self.aiUnits:
if str(unit.status).lower() == "active":
tmp_aiUnits.append(unit)

if len(tmp_aiUnits)>0:
random_number = random.randint(1, len(tmp_aiUnits))
unit = self.aiUnits[random_number-1]
return unit
def get_specific_ai_unit(self, playerName):
unitValue = {}
try:
for unit in self.aiUnits:
if unit.status.lower() == "active" and unit.name.lower() ==
playerName.lower():
unitValue = unit
break
except Exception as ex:
#print(colored(str(ex), "red"))
print(str(ex))
finally:
return unitValue

def set_ai_unit(self, name, revisedUnit):


try:
for unit in self.aiUnits:
if str(unit.name).lower() == str(name).lower():
unit = revisedUnit
break
except Exception as ex:
#print(colored(str(ex), "red"))
print(str(ex))

def remove_ai_unit(self):
try:
for unit in self.aiUnits:
if unit.healthPoint <= 0 and str(unit.status) == "active":
unit.status = "dead"
print("AIUnit - " + unit.name + " has been removed due to lower
HealthPoint")
break
except Exception as ex:
#print(colored(str(ex), "red"))
print(str(ex))
def get_profession(self, teamName):
profession = ""
try:
if teamName == "PlayerTeam":
total_units = len(self.playerUnits)
if total_units == 0:
profession = "Warrior"
else:
lastest_unit = self.playerUnits[total_units-1]
if lastest_unit.profession == "Warrior":
profession = "Tanker"
else:
profession = "Warrior"
else:
total_units = len(self.aiUnits)
if total_units == 0:
profession = "Warrior"
else:
lastest_unit = self.aiUnits[total_units-1]
if lastest_unit.profession == "Warrior":
profession = "Tanker"
else:
profession = "Warrior"
except Exception as ex:
#print(colored(str(ex), "red"))
print(str(ex))
finally:
return profession

def get_playerteam_players(self):
playerteam_unit_names = ""
try:
for unit in self.playerUnits:
if str(unit.status).lower() == "active":
playerteam_unit_names += unit.name + " | "
playerteam_unit_names =
playerteam_unit_names[:len(playerteam_unit_names)-2]
except Exception as ex:
#print(colored(str(ex), "red"))
print(str(ex))
finally:
return playerteam_unit_names.strip()

def get_aiteam_players(self):
aiteam_unit_names = ""
try:
for unit in self.aiUnits:
if str(unit.status).lower() == "active":
aiteam_unit_names += unit.name + " | "
aiteam_unit_names = aiteam_unit_names[:len(aiteam_unit_names)-2]
except Exception as ex:
#print(colored(str(ex), "red"))
print(str(ex))
finally:
return aiteam_unit_names.strip()

# Press the green button in the gutter to run the script.


if __name__ == '__main__':
main = main()
main.play_game()

You might also like