我试图让这个循环起作用,但它只是拒绝工作。非常感谢任何帮助。
while player1_score < 100 and player2_score < 100 and player3_score < 100 and player4_score < 100:
while player_turn != playerholder:
dice_rolling()
scoring()
score_awarding()
player_turn = player_turn + 1
if player_turn == playerholder:
player_turn == 1
我想要做的是获得在场球员的数量(球员),然后让球员在其范围内转弯,即重复直到有人获得100分或更高的分数。 只要问你是否需要更多代码:) 提前谢谢。
编辑:每个人似乎都对我的要求感到困惑所以我会尝试更精细地解释它。
所以游戏中有2到4名玩家,而他们想要做的就是获得100分或更高分。由于有多个玩家,我想尝试重复结构,以有效地使用我一遍又一遍创建的功能。使用当前代码,它将为玩家提供类似的评分,直到最后一个玩家,代码完全停止并且不显示任何内容。我想做的是让代码无限重复,直到获得所需的分数,因为它将在没有错误的情况下继续骑行。我尝试了多次不同的循环,但我无法让它工作。希望这对每个人来说都更清楚一些。抱歉,无组织/不清楚的问题,对StackOverflow来说相对较新。
编辑:问题已经解决
答案 0 :(得分:2)
顺便说一句,你应该让你的玩家保持某种列表并迭代它们,允许他们每个人在他们的迭代中转向。类似的东西:
players = [player1,player2,player3,player4]
for player in players:
if player.score >= 100: win(player)
dice_rolling()
player.score += scoring()
但是,当然,只有在代码中有某种class Player
时才能使用它(你应该这样做)。
class Player(object):
def __init__(self,name):
self.name = name
self.score = 0
# what else does a Player do?
实际上你也可以成为一个Game
课程。
class Game(object):
def __init__(self,players):
self.players = players # a list of players
self.running = False
def dice_rolling(self):
diceroll = sum(random.randint(1,6) for _ in range(2))
def scoring(self):
return score # I don't know how you're doing this so...
def win(self,player):
print("{} wins!".format(player.name))
def run(self):
self.running = True
while self.running:
for player in self.players:
roll = dice_rolling()
player.score += scoring()
if player.score >= 100:
self.running = False
win(player)
Game().run()
答案 1 :(得分:0)
您的代码可能无法正常工作,因为相反
为此1
分配player_turn
:
player_turn == 1
应该是这样的:
player_turn = 1
答案 2 :(得分:0)
你可以尝试使用列表结构。
如果有4名球员,那将会是这样的:
turns = [0,1,2,3,4,0,1,2,3,4]
#this would add another turn for player 0, to remove you just use del
turns.append(0)
#This would remove a winner from your turn stack while preserving the order
for i in turns:
if i ==4:
del i
#You could also use a dictionary if you don't like list indicies or numbers:
Players = {"blue":3, red":4, "green":2, "orange":1}
turn = [4,2,1,3,4,2,1,3]
#If there is also a maximum number of turns, you can have that hard coded also:
for i in range(max):
turn.append(1)
turn.append(2)
turn.append(3)
turn.append(4)
说实话,我并不真正理解你的问题,但希望这可能完全或部分地回答了这个问题。