Il y a beaucoup de code ci-dessous mais vous n'avez pas besoin de vraiment en lire, il suffit de savoir que les fonctions existent et les noms des fonctions. Je vais décrire mon problème en premier.python, changer les fonctions en classes et en objets
J'ai créé un programme procédural entièrement basé sur des fonctions et quelques variables globales comme vous pouvez le voir ci-dessous. Je veux changer le programme en un programme orienté objet mais j'ai des problèmes avec ça puisque je n'ai jamais rien fait de tel auparavant.
La procédure qui doit être suivie est que: - la fonction attack()
doit être placé dans un fichier nommé attacker.py -les fonctions defence()
, updateVars()
et smartDefender()
doivent être placés dans un fichier defender.py -la fonction main()
et le reste du code (la majeure partie du code) seront placés dans un fichier nommé manager.py qui sera le fichier principal et rassemblera tout. -Je dois utiliser des classes.
J'ai essayé une gamme de choses différentes, y compris en changeant les noms des fonctions à __init__
puis en les important et en essayant de les utiliser dans manager.py. J'ai aussi essayé de garder les mêmes noms de fonction et de simplement mettre les fonctions dans les classes et d'importer attacker.py et defender.py dans manager.py mais rien ne semble fonctionner ... Toute aide serait appréciée.
Bien que je ne pense pas que vous avez vraiment besoin d'une description de ce que le programme fait, si vous avez vraiment besoin, je peux faire une brève, ou vous pouvez voir here.
Toute aide sera apprécié.
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()
Ma question est, Comment puis-je très facilement (pas nécessairement efficace) convertir ces programmes de procédure en un orienté objet qui utilise des classes et des fichiers multiples.
Je pense que les problèmes incluraient où les fonctions sont appelées à main()
, si cela aide à résoudre le problème du tout ..
OK, vous avez spec tech bien. Alors, quelle est votre question? – Denis
Ma question est, Comment puis-je très facilement (pas nécessairement efficacement) convertir ce programme procédural dans un programme orienté objet qui utilise des classes et des fichiers multiples ... si cela a du sens ..? la question est dans le PO plus en détail. – SeesSound
On dirait que vous confondez les classes et les modules. Renommer des fonctions à '__init__' ne fait pas un programme OO. –