骰子游戏模拟

Dice Game Simulation

我有一个作业的问题:

Your friend has devised a game with two players. The two players, called A and B, take turns rolling an ordinary six-sided die, with A being the first to roll.

The first player who rolls a six wins the game. You and your friend do not agree about what the probability of A winning the game is, and you therefore decide to simulate the game with a computer.

Thus: write a Python program that performs 10 trials, each consisting of 10000 games, and for each trial prints the fraction of the games won by player A.

这是我到目前为止得到的代码,它每次都只返回一个 1667 左右的数字。我主要想知道如何区分 A 或 B 赢得比赛。

如有任何帮助,我们将不胜感激!

编辑代码

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

实现干净代码的最佳方法是将手头的每个任务分开 functions,这意味着:
1. 运行 一场游戏 -> 每个骰子滚动
2. 运行 一场游戏 -> A 和 B 交替进行,直到第一个骰子点到 6(这里考虑如果 A 点到 6,B 甚至不需要玩,因为 A赢了)
3. 运行 一次试用 -> 由特定次数的播放组成
4.运行主程序->由播放所有需要的试听次数组成

因此,下面是一种可能的解决方案(在这里您可以看到我的 play 函数已经 returns 结果,这意味着玩家是否获胜):

import random

def play():
    won = True
    keepPlaying = False
    rollDice = random.choice([1,2,3,4,5,6])
    if rollDice == 6: 
        return won
    return keepPlaying

def run_game(winsA, winsB):
    while True:
        playA = play()
        playB = play()
        if playA:
            winsA += 1
            return winsA, winsB
        elif playB:
            winsB += 1
            return winsA, winsB

def run_trial(numGames):
    winsA = 0
    winsB = 0
    for i in range(numGames):
        wins = run_game(winsA, winsB)
        winsA = wins[0]
        winsB = wins[1]
    print("winsA:", winsA, "| winsB:", winsB, "| Fraction of A wins:",  "{} %".format(winsA / ( winsA + winsB ) * 100))

numTrials = 10
numGames = 10000

for i in range(numTrials):
    run_trial(numGames)

你真的只需要计算玩家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