Tenir compte de cette pseudo-code pour des suggestions (certaines parties doivent être mises en œuvre):
# a Player class. it should have a Name and a Score
class Player():
def __init__(name):
self.name = name
self.score = 0
# displays a prompt and reads in the players name returning a string
def getPlayerName():
# needs code here, see next function for idea of what
pass
# ask the player to make a choice, takes a Player object
# and returns a string
def getPlayerAttack(player):
print "%s, what do you choose?" % player.name
return raw_input("> ")
# determines who wins and updates score accordingly
# takes in the player objects and their attack choices
def attack(player1, choice1, player2, choice2):
if choice1 == choice2:
print "Its's a tie."
elif choice1 == "1" and choice2 == "2":
print "%s wins." % player2
player2.score = player2.score + 1
elif ... # other attacks
# display the scores of the two players
def displayScores(player1, player2):
print "%s vs %s" % (player1.score, player2.score)
player1 = Player(getPlayerName())
player2 = Player(getPlayerName())
while true:
choice1 = getPlayerAttack(player1)
choice2 = getPlayerAttack(player2)
attack(player1, choice1, player2, choice2)
displayScores(player1, player2)
Cela nécessitera un travail , et c'est pas super-optimal, mais il devrait être un début et devrait montrer quelques autres concepts. Utilisez Ctrl-C pour arrêter ou ajouter une condition d'arrêt (par exemple, chaque joueur entre "0") - bugs inclus gratuitement. :)
Bonne codification.
"Plus d'objets". :) Considérez que chaque joueur est un objet, avec un nom et un score. "Plus de fonctions". Considérez qu'il y a une fonction qui, une fois donnée l'entrée, montrera qui gagne et met à jour le score de joueur approprié. –