猪的游戏 - 无法弄清楚如何循环所有玩家

时间:2017-06-21 01:31:12

标签: python while-loop pygame

我已经找出了大部分问题,但似乎无法弄清楚如何循环每个玩家让他们玩游戏。这是我到目前为止的代码。我认为main()函数中的while循环是错误的,但我不知道该怎么做才能修复它。如果你能指出我正确的方向,请告诉我。如果你掷1,我还需要弄清楚如何结束转弯。

import random


def instructions():
    print ("==================================================")
    print ("\nWelcome to the Game of Pig.  To win, be the")
    print ("player with the most points at the end of the")
    print ("game.  The game ends at the end of a round where")
    print ("at least one player has 100 or more points.\n")
    print ("On each turn, you may roll the die as many times")
    print ("as you like to obtain more points.  However, if")
    print ("you roll a 1, your turn is over, and you do not")
    print ("obtain any points that turn.\n")


def num_players():
    while True:
        players = raw_input("How many players will be playing? ")

        if players.isdigit():
            return int(players)
        else:
            print "\nPlease enter a valid number of players.\n"


def name_players(players):
    count = 1
    list_of_players = []
    for i in range(players):
        name = raw_input("Enter the name for Player {}: ".format(count))
        list_of_players.append(name)
        count += 1
    print ""
    return list_of_players


def start_game(list_of_players):
    points = 0
    for player in list_of_players:
        print "{0} has {1} points.".format(player, points)
    print ("==================================================\n")
    s = input("How many sides of the dice do you want? ")
        for player in list_of_players:
        print ("\n{0}'s turn:").format(player)
        answer = raw_input("Press y to roll the dice?")
        while answer == 'y' and points <= 100:
            roll = random.randrange(1, s)
            if roll > 1:
                points += roll
                print "{0} has {1} points.".format(player, points)
                answer = raw_input("Press y to roll the dice?")


def main():
    instructions()
    players = num_players()
    list_of_players = name_players(players)
    start_game(list_of_players)


if __name__ == "__main__":
    main()

1 个答案:

答案 0 :(得分:0)

您遇到的问题是由函数start_game中的局部变量 points 引起的。所有玩家都在共享相同的变量以跟踪他们的分数。因此,一旦你的第一个玩家达到/超过100分,游戏逻辑将移动到第二个玩家。但是,积分变量仍保持前一个玩家的得分(大于或等于100)。因此,以下玩家永远不会有机会进入循环。

每当点变量保持值大于或等于100时,你应为每个玩家声明点可变重置点变量

def start_game(list_of_players):
    points = 0 #<----------------- points is local variable to the function
    for player in list_of_players:
        print("{0} has {1} points.".format(player, points))
    print ("==================================================\n")
    s = int(input("How many sides of the dice do you want? ")) ###
    # validate value of s?
    for player in list_of_players:
        ### the variables declared in for loop is unique to each player
        print ("\n{0}'s turn:".format(player))
        answer = input("Press y to roll the dice?")
        while answer == 'y' and points <= 100:
            roll = random.randrange(1, s + 1, 1) ### randrange 
            if roll > 1:
                points += roll
                print("{0} has {1} points.".format(player, points))
                answer = input("Press y to roll the dice?")

PS。我用Python 3.6.1运行你的代码。

此外,您需要注意的另一件事是random.randrange(start, stop, step)。输出不包括 stop的值。例如random.randrange(0, 10, 1)将生成0到9之间的整数。因此,如果玩家选择了骰子的6个边,这意味着随机数范围应该是1到6.这样,你需要增加玩家输入的数量。
例如骰子的6面
random.randrange(0, 6+1, 1)random.randrange(6+1)应该有效。