我正在尝试创建游戏Snakeeyes的版本,该版本将在程序开始时使用n,即玩家人数。
到目前为止,我已经做到了这一点:
import random
def rollDice():
dice = random.randint(1, 6)
print "You rolled a", dice
return dice
def addUser(name):
name = player()
print name, "is a player"
class player():
score = 0
players = []
def __init__(self):
score = 0
player.score = score
def addScore(self, dice1, dice2):
if dice1 == 1 or dice2 == 1:
player.score = 0
if dice1 == 1 and dice2 == 1:
print "SNAKE EYES"
else:
player.score += dice1
player.score += dice2
return player.score
def dispScore(self):
return player.score
numbp = int(input("Please enter number of players \n"))
plyarr = dict()
for x in range(numbp):
plyarr[x] = player()
plyarr[x].addScore(rollDice(),rollDice())
for x in range(numbp):
print plyarr[x].score
但是,由于我对python的工作方式以及如何使用类(等)来加快这种编程速度的幼稚工作,我无法使其正常工作。主要问题是它经常会覆盖字典中的同一位置(如果我使用字典)。
重新写球员课:
class player():
def __init__(self):
self.score = 0
self.players = []
def addScore(self, dice1, dice2):
if dice1 == 1 or dice2 == 1:
player.score = 0
if dice1 == 1 and dice2 == 1:
print "SNAKE EYES"
else:
self.score += dice1
self.score += dice2
return self.score
def dispScore(self):
return self.score
def __str__(self):
return '<a description of the current object>'
根据您的评论,我认为您对将玩家存储在字典中的方式感到怀疑;您可以按照以下步骤进行操作(对原始代码进行了少量更改)
numbp = int(input("Please enter number of players \n"))
plyarr = dict()
for x in range(numbp):
current_player = player()
current_player.addScore(rollDice(),rollDice())
playarr[x] = current_player
最后,显示您的球员得分:
for player_id, player in plyarr.items():
print player.dispScore()
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句