简体   繁体   English

Python编程骰子游戏?

[英]Python Programming Dice Game?

I am trying to come up with a simulation for the Pig dice game. 我正在尝试模拟猪骰子游戏。 I want it to simulate for the number of games the user wants(each game to 100 points) and report the average points and percent wins for each player. 我希望它模拟用户想要的游戏数量(每个游戏为100分),并报告每个玩家的平均得分和获胜百分比。 My program is running but it is only running for one game. 我的程序正在运行,但仅运行一个游戏。 I think there is something wrong with my loop but i cannot tell. 我认为我的循环存在问题,但我无法分辨。 Thank you for your help and time here is my program: 谢谢您的帮助,时间是我的计划:

from random import randrange    # Use "randrange(1, 7)" to get a random
                                # number between 1 and 6.

# Takes one turn in the game of pig.  Keeps rolling until either
# the holdAmount is reached or a pig (1) is rolled.  Returns the
# score accumulated during the turn.
def takeTurn(holdAmount):
    totalScore = 0
    while totalScore < holdAmount:
        rollValue = randrange(1,7)
        if rollValue == 1:
            totalScore = 0
            break
        else:
            totalScore = totalScore + rollValue
    return totalScore 

    # Start with turn score equal to 0.  Repeatedly roll die, adding
    # roll value to score each time, until score reaches holdAmount.
    # If at any time a pig (1) is rolled, set score to 0 and end the
    # turn early.


# Tests the takeTurn function.
def main():
    first = eval(input("How many points should the first player try for in each turn?"))
    second =  eval(input("How many points should the second player try for in each turn?"))
    games = eval(input("How many games should be simulated?"))
    firstScore = 0
    secondScore = 0
    turnCount = 0
    score = 0
    score1 = 0
    won = 0
    won1 = 0
    for i in range(games):
        while firstScore < 100 and secondScore < 100:
            firstScore = takeTurn(first) + firstScore
            secondScore = takeTurn(second) + secondScore
            turnCount = turnCount + 1
        if firstScore >= 100:
            won = won + 1
        elif secondScore >= 100:
            won1 = won1 + 1
        score = score + firstScore
        score1 = score1 + secondScore
    percent = won / games
    percent1 = won1 / games
    points = score / games
    points2 = score1 / games
    print("The average points for player one is",points)
    print("The percent of games won for player one is",percent)
    print("The average points for player two is",points2)
    print("The percent of games won for player two is",percent1)



if __name__ == "__main__":
    main()

I was confused for a while when I first looked at this. 当我第一次看这个时,我很困惑。 The reason is that each game ends with the same score since you do not reset the firstScore, etc. values each time. 原因是每个游戏都以相同的分数结束,因为您不必每次都重置firstScore等值。 If you set each of those to 0 at the beginning of your for loop, you won't have any problems. 如果在for循环开始时将每个参数都设置为0,则不会有任何问题。

To be more specific, if you move firstScore, secondScore, and turnCount inside your for loop at the very top of it, the code runs properly. 更具体地说,如果将firstScore,secondScore和turnCount移到for循环的最顶部,则代码可以正常运行。

在for循环中需要firstScoresecondScore

The traditional way to gain a better understanding of what your program is doing is to add some print statements at looping and branching points. 更好地了解程序正在执行的操作的传统方式是在循环和分支点处添加一些打印语句。

A more advanced technique is to trace through the program using pdb . 一种更高级的技术是使用pdb跟踪程序。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM