Python 骰子游戏模拟
我有一个任务问题: 你的朋友设计了一个两人玩的游戏。两位选手,, 称为A和B,轮流滚动一个普通的六面模具,其中一个是 第一个滚 第一个掷六分的玩家获胜。 你和你的朋友对获胜的可能性并不一致 游戏是,因此您决定使用 电脑 因此:编写一个Python程序,执行10次试验,每次试验包括 10000场比赛,每一次试玩打印出 球员A 这是我到目前为止得到的代码,它只是返回一个1667左右的数字。我主要想知道如何区分A和B赢得比赛 任何帮助都将不胜感激 编辑的代码Python 骰子游戏模拟,python,python-3.x,montecarlo,dice,stochastic,Python,Python 3.x,Montecarlo,Dice,Stochastic,我有一个任务问题: 你的朋友设计了一个两人玩的游戏。两位选手,, 称为A和B,轮流滚动一个普通的六面模具,其中一个是 第一个滚 第一个掷六分的玩家获胜。 你和你的朋友对获胜的可能性并不一致 游戏是,因此您决定使用 电脑 因此:编写一个Python程序,执行10次试验,每次试验包括 10000场比赛,每一次试玩打印出 球员A 这是我到目前为止得到的代码,它只是返回一个1667左右的数字。我主要想知道如何区分A和B赢得比赛 任何帮助都将不胜感激 编辑的代码 import random def ro
import random
def rollDie():
return random.choice([1,2,3,4,5,6])
def roll_first():
if random.choice([0,1]) == 0:
return 'A'
else:
return 'B'
def rollSim():
while True:
turn = roll_first()
numTrials = 10
numThrows = 10000
totalThrows = numTrials*numThrows
game_on = True
winsA = 0
winsB = 0
while game_on:
for n in range(numTrials):
games = 0
for i in range(numThrows):
throw = rollDie()
if turn == 'A':
if throw == 6:
winsA += 1
games += 1
break
else:
turn = 'B'
else:
if throw == 6:
winsB += 1
games += 1
break
else:
turn = 'A'
return winsA/totalThrows
实现干净代码的最佳方法是在
函数中分离手头的每个任务,意思是:
1.为每个掷骰子运行游戏->
2.运行一个游戏->在a和B之间交替进行游戏,直到第一个玩家在骰子上得到6(这里考虑到如果a得到6,B甚至不需要玩,因为a赢了)
3.运行试用->包含特定数量的播放
4.运行主程序->包括播放所需的所有试验次数
下面是一个可能的解决方案(在这里您可以看到我的play
函数已经返回了结果,这意味着玩家是否赢了):
你真的只需要计算玩家A的赢数。因为你每次试玩10000场游戏,如果你知道A赢了多少场游戏,你就知道其他游戏肯定是B赢的,A+B=10000
您似乎随机决定谁拥有第一个回合,但任务指出,应该始终是玩家A进行第一个回合
您可以使用布尔isPlayerA
来知道该轮到谁了。从isPlayerA=True
开始,然后用isPlayerA=not isPlayerA
进行切换
以下是如何编写代码:
import random
def rollDie():
return random.choice([1,2,3,4,5,6])
def winFraction(): # Perform one trial of 10000 games and return fraction of wins for A
winsA = 0 # only count wins for A
for numTrow in range(10000):
isPlayerA = True # Player A always takes the first turn
throw = rollDie()
while throw != 6: # While the game is not over yet:
isPlayerA = not isPlayerA # Switch to the other player
throw = rollDie()
if isPlayerA:
winsA += 1 # Only count the wins for player A
return winsA/10000.0 # This is the fraction: we know the number of played games
def sim():
for trial in range(10): # Perform 10 trials
print(winFraction()) # Print the result of the trial
sim() # Start the simulation
我相信你误解了你的问题。您只为a创建了一个函数,但这是一个a对B的游戏。因此,您应该为diceRow创建一个函数,在循环了交互次数之后,每一行都属于一个玩家,并检查分数。经过所有的测试,然后计算A赢得的比赛分数。谢谢你!我如何创建一个知道掷球是a还是B的函数?你应该为a的分数和B的分数创建变量,因此为
循环添加一个,例如,为每个游戏。每次玩两次,一次将结果分配给玩家A,另一次分配给玩家B,并检查结果(如果有),因为骰子结果是6,所以赢了。清楚吗?好的,如果A先抛出,我如何检查抛出是否是A的,我可以使用if numThrows%2!=0? 由于所有As-throw都很奇怪,所以@user10551611只需创建两个变量:aThrow
和bThrow
import random
def rollDie():
return random.choice([1,2,3,4,5,6])
def winFraction(): # Perform one trial of 10000 games and return fraction of wins for A
winsA = 0 # only count wins for A
for numTrow in range(10000):
isPlayerA = True # Player A always takes the first turn
throw = rollDie()
while throw != 6: # While the game is not over yet:
isPlayerA = not isPlayerA # Switch to the other player
throw = rollDie()
if isPlayerA:
winsA += 1 # Only count the wins for player A
return winsA/10000.0 # This is the fraction: we know the number of played games
def sim():
for trial in range(10): # Perform 10 trials
print(winFraction()) # Print the result of the trial
sim() # Start the simulation