Python 程序与人类玩家和计算机一起运行 pig 游戏

标签 python python-3.x probability dice

简单来说,谁先走是随机决定的。当人类玩家轮到时,他/她可以选择保持或滚动。如果他选择掷骰子,则掷骰子并添加值(到turn_score),直到> 20,然后计算机开始转动。

每回合结束后,回合分数都会添加到最终分数中。如果掷出 1,则回合分数变为 0(最终结果不变)。

关于我在这个问题上哪里出了问题,有什么建议吗?

import random
def roll_dice():
    return random.randint(1,6)

players = ['Player One', 'Player Two']
scores = {'Player One': 0, 'Player Two': 0}
random.shuffle(players)

while True:
    for i in players:
        if i == 'Player One':
            choice = input("Roll(r) or Hold(h)?: ")
            turn_score = 0
            final_score = 0
            if (choice=='r'):
                while(turn_score<=20):
                    dice_value = roll_dice()
                    turn_score += dice_value
                    if (dice_value==1):
                        turn_score = 0
                        print("-rolled a ",dice_value)
                        print("Pigged out!")
                        break
                    print("Turn score is: ",turn_score)
                    final_score += turn_score
                    print("Your final score is: ",final_score)
                    if (final_score>100):
                        break
        else:
            turn_score=0
            print("It is " +  str(i) + "'s turn.")
            while(turn_score<20):
                dice_value = roll_dice()
                if (dice_value==1):
                    turn_score = 0
                    scores[i] +=0
                    print("- rolled a ",dice_value)
                    print("Pigged out!")
                    break
                else:
                    turn_score+=dice_value
                    print("-rolled a ",dice_value)
        scores[i] += turn_score
        print("Turn score is: ",turn_score)
        print('{} score: {} {} score: {}'.format(players[0], scores[players[0]], players[1], scores[players[1]]))
        if scores[i]>100:
            break
    if scores[i]>100:
        break

winner = [winner for winner in scores if scores[winner] == max(scores.values())]
print(str(winner) + " is the winner!")`

这是我得到的输出:

It is Player Twos turn.
-rolled a  3
-rolled a  5
-rolled a  5
-rolled a  5
-rolled a  4
Turn score is:  22
Player Two score: 22 Player One score: 0
Roll(r) or Hold(h)?: h
It is Player Twos turn.
- rolled a  1
Pigged out!
Turn score is:  0
Player Two score: 22 Player One score: 0
Roll(r) or Hold(h)?: r
Turn score is:  3
Your final score is:  3
Turn score is:  5
Your final score is:  8
Turn score is:  9
Your final score is:  17
Turn score is:  13
Your final score is:  30
Turn score is:  16
Your final score is:  46
Turn score is:  19
Your final score is:  65
-rolled a  1
Pigged out!
It is Player Twos turn.
- rolled a  1
Pigged out!
Turn score is:  0
Player Two score: 22 Player One score: 0
Roll(r) or Hold(h)?:

最佳答案

好吧,除非我误解了游戏,否则我认为您的问题是您将回合得分添加到每轮的最终得分中,而不是添加回合得分和回合得分之间的差值。每轮的原始最终得分(即,即使玩家在后续掷骰中掷出 1,您仍会为该回合之前掷骰的玩家提供分数)。

而不是:

        turn_score = 0
        final_score = 0
        if (choice=='r'):
            while(turn_score<=20):
                dice_value = roll_dice()
                turn_score += dice_value
                if (dice_value==1):
                    turn_score = 0
                    print("-rolled a ",dice_value)
                    print("Pigged out!")
                    break
                print("Turn score is: ",turn_score)
                final_score += turn_score
                print("Your final score is: ",final_score)
                if (final_score>100):
                    break

我想你想要:

        turn_score = 0
        final_score = 0
        if (choice=='r'):
            while(turn_score<=20):
                dice_value = roll_dice()
                turn_score += dice_value
                if dice_value == 1:
                    turn_score = 0
                    print("-rolled a ",dice_value)
                    print("Pigged out!")
                    break
                print("Turn score is: ", turn_score)
                print("Your final score is: ", final_score + turn_score)
                if final_score + turn_score > 100:
                    final_score += turn_score
                    break

关于Python 程序与人类玩家和计算机一起运行 pig 游戏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52637031/

相关文章:

algorithm - 加权圆图的总路径概率

python - numpy.zeros_like() 中 subok 选项的用途和用途是什么?

python - 是否可以优先锁定锁?

python - 为什么快速调用 Python 类的 id 不是唯一的?

node.js - 从 Node.js 到 Python 3.6 的 hmac 512 迁移结果不匹配

python - 在有偏差的骰子上求概率

python - 令人困惑的任务

python - 如何在 Python 中旋转向量

python - 为什么我们需要在bigquery中构建数据集引用?

python - 比较两列,一列通过 float ,另一列通过字符串来获得匹配值