用于Python的Craps模拟器

时间:2014-02-28 23:58:07

标签: python probability

问题在于模拟一些掷骰子游戏。你滚动两个6面骰子。如果掷骰加起来为2,3或12,则该玩家将失去该游戏。如果掷骰加起来为7或11,则玩家获胜。如果滚动累加到任何其他数字,则播放器重新滚动,直到再次滚动前一个滚动量或滚动7。如果首先掷出7,则游戏算作胜利。如果先前滚动先前的掷骰数量,则游戏计为输掉。我们应该显示胜利的数量和总数的百分比。

我不太确定我在这里遇到了什么错误 - 获胜的百分比应该在50%左右,但是当我运行该程序时,我通常会获得1-3%的胜利。编辑:我实际上不确定概率是否应该是50%,但我很确定它不应该是1-3%......

from random import *

def craps():
printIntro()
n=getInput()
winCount=simRoll(n)
printResults(n, winCount)


def printIntro():

print('This program simulates the popular casino game "craps." A player rolls a pair of normal six-sided dice.')
print('If the initial roll is a 2, 3, or 12, the player loses. If the initial roll is a 7 or 11, the player wins.')
print('Any other initial roll causes the player to "roll for point." The player keeps rolling the dice until either rolling')
print('a 7 or re-rerolling the value of the initial roll. If the player re-rolls the initial value before rolling a 7, it is a win.')
print('If the player rolls a 7 first, it is a loss.')
print('\nThis program simulates n games of craps and calculates the percent of games won.')


def getInput():

n=eval(input("Input the amount of games of craps to simulate: "))
return n


def simRoll(n):
rollCount=0
winCount=0
PointForRoll=0

while rollCount < n:

        rollCount=rollCount+1

        randomRoll=randrange(1,7) + randrange (1,7)

        if randomRoll == 2 or randomRoll == 3 or randomRoll == 12:
            winCount = winCount + 0

        if randomRoll == 7 or randomRoll == 11:
            winCount = winCount + 1

        else:

            while PointForRoll != 7 or PointForRoll != randomRoll:

                PointForRoll = randrange(1,7) + randrange(1,7)

                if PointForRoll == randomRoll:
                    winCount=winCount

                if PointForRoll == 7:
                    winCount=winCount+1

                return PointForRoll

return winCount


def printResults(n, winCount):

print('\nFor', n, 'games of craps simulated,', winCount, 'games were wins, giving a success rate of '+ str(100*(winCount/n)) + '%.')


if __name__ == '__main__': craps()

谢谢!

2 个答案:

答案 0 :(得分:0)

if randomRoll == 2 or randomRoll == 3 or randomRoll == 12:
    winCount = winCount + 0
if randomRoll == 7 or randomRoll == 11:
    winCount = winCount + 1
else:

第二个if应为elif。如果滚动为2,3或12,则不希望输入else

while PointForRoll != 7 or PointForRoll != randomRoll:

or应为and。虽然滚动不是7 ,但滚动不是重点。

if PointForRoll == randomRoll:
    winCount=winCount
if PointForRoll == 7:
    winCount=winCount+1

这是倒退的。滚动7是一种损失。击中这一点是一场胜利。您应该在第一个if中增加获胜次数。

return PointForRoll

删除此行。你不应该从这个循环中返回。

while PointForRoll != 7 and PointForRoll != randomRoll:
    ...

最后,在此循环结束后,您永远不会重置PointForRoll。在循环之前或之后添加PointForRoll = 0

通过所有这些改变,我获得了大约50%的胜率。

答案 1 :(得分:0)

现在,如果你滚动2,3或11,你的程序仍然试图说明问题。而不是:

if randomRoll == 2 or randomRoll == 3 or randomRoll == 12:
   ....

if randomRoll == 7 or randomRoll == 11:
  ...

else:
  ...

你想尝试

if randomRoll == 2 or randomRoll == 3 or randomRoll == 12:
   ....

elif randomRoll == 7 or randomRoll == 11:
  ...

else:
 ....

这样滚动2,3和12将不会使用最终'else'中的代码