There is a lot of code below but you dont have to really read any of it, you just need to know that the functions exist and the function names. I will describe my problem first.
I have created a procedural program based entirely on functions and a few global variables as you can see below. I want to change the program into an object-oriented-program but I am having trouble with it since I have never done anything like this before.
The procedure that needs to be followed is that:
– the function attack() needs to be placed into a file named attacker.py
-the functions defence(),updateVars(), and smartDefender() need to be placed into a file defender.py
-the main() function and the rest of the code(most of the code) will be placed in a file named manager.py which will be the main file and will bring everything together.
-I have to use classes.
I have tried a range of different things including changing the names of the functions to __init__ and then importing and attempting to use them in manager.py. I have also tried to keep the function names the same and just put the functions inside of classes and import attacker.py and defender.py into manager.py but nothing seems to work… Any and all help would be appreciated.
Although I don’t think you really need a description of what the program does, if you really need one, I can make a brief one, or you can view it here.
Any and all help will be appreciated.
import random
HIGH= 3
MED= 2
LOW= 1
def attack(attackList):
x= random.uniform(0,1)
for attackLevel,probability in attackList:
if x<probability:
break
x=x-probability
return attackLevel
def defence(attackLevel,defendList):
x= random.uniform(0,1)
for defendLevel,probability in defendList:
if x<probability:
break
x=x-probability
return defendLevel
def updateVars(attackLevel,defendLevel,block,hit,run):
if attackLevel==1:
printAttackLevel='Low'
if attackLevel==2:
printAttackLevel='Medium'
if attackLevel==3:
printAttackLevel='High'
if defendLevel==1:
printDefendLevel='Low'
if defendLevel==2:
printDefendLevel='Medium'
if defendLevel==3:
printDefendLevel='High'
if attackLevel==defendLevel:
block=block+1
hit=hit
run=run+1
else:
block=block
hit=hit+1
run=run+1
return block,hit,run,printAttackLevel,printDefendLevel
def smartDefender(defendLevel,attackLevel,smartList):
for i in smartList:
if (i==(i+1)==(i+2)):
defendLevel= attackLevel
return defendLevel
else:
return
def main():
DEFAULT_PROBABILITY= 0.33
run=0
hit=0
block=0
smartList=[]
rounds= int(input("\nPlease enter the number of rounds between 1 and 100:"))
if rounds<=0 or rounds>100:
print("\n")
print("Invalid range. The number of rounds has been set to 10 by DEFAULT_PROBABILITY.")
rounds=10
lowAttackProb= float(input("\nPercentage of attacks aimed low(0-100):"))/100
medAttackProb= float(input("Percentage of attacks aimed medium(0-100):"))/100
highAttackProb= float(input("Percentage of attacks aimed high(0-100):"))/100
if lowAttackProb+medAttackProb+highAttackProb !=1.00:
print("\n")
print("Invalid entry. The sum of the pecentages must equal 100%. The probability of each level has been set to 33.0% by DEFAULT_PROBABILITY.")
lowAttackProb=DEFAULT_PROBABILITY
medAttackProb=DEFAULT_PROBABILITY
highAttackProb=DEFAULT_PROBABILITY
print('\nLet The Fighting Begin')
print('-'*22)
while run < rounds:
lowDefProb= DEFAULT_PROBABILITY
medDefProb= DEFAULT_PROBABILITY
highDefProb= DEFAULT_PROBABILITY
attackList= [(LOW,lowAttackProb),(MED,medAttackProb),(HIGH,highAttackProb)]
attackLevel= attack(attackList)
smartList.append(attackLevel)
defendList=[(LOW,lowDefProb),(MED,medDefProb),(HIGH,highDefProb)]
defendLevel=defence(attackLevel,defendList)
block,hit,run,printAttackLevel,printDefendLevel= updateVars(attackLevel,defendLevel,block,hit,run)
if run>(rounds/2):
defendLevel=smartDefender(defendLevel,attackLevel,smartList)
#implement smart mode
print('%s%2s%s%3s%s%5s%s%3s'% ('\nRound',run,':\t','Attacker:',printAttackLevel,'\t','Defender:',printDefendLevel))
print("%2s%2d%s%s%2d"% ('\nTotal Hits:',hit,'\t','Total Blocks:',block))
print('Attacker Proportions:','','','Low:','','',lowAttackProb*100,'%','','','Medium:','','',medAttackProb*100,'%','','','High:','','',highAttackProb*100,'%')
print('Defender Proportions:','','','Low:','','',lowDefProb*100,'%','','','Medium:','','',medDefProb*100,'%','','','High:','','',highDefProb*100,'%')
print("\nThank you for using this program, Goodbye!")
main()
My question is, How can I very easily (not necessarily efficiently) convert these procedural program into a object-oriented one that uses classes and multiple files.
I think problem areas would include where the functions are being called in main(), if that helps to solve the problem at all..
In case it helps … I just made this class-based script as an example.