【问题标题】:Python program that runs a game of pig with a human player and the computer与人类玩家和计算机一起运行猪游戏的 Python 程序
【发布时间】:2022-04-22 19:54:25
【问题描述】:

所以简而言之,谁先走是随机决定的。当人类玩家轮到时,他/她可以选择保持或滚动。如果他选择掷骰子,则掷骰子并将值相加(到 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)?:

【问题讨论】:

  • 您能否详细说明当前输出有什么问题?我不知道 pig 的一般规则,从你的问题中也不完全清楚预期的输出应该是什么。
  • @JennerFelton 道歉。问题在于回合分数和最终分数的相加部分。说轮到我了,我决定滚动,即按 r。它应该准确地显示它是如何显示计算机转动的。每当玩家滚动时,这很奇怪(5+3=8,但显示为 9)。

标签: python python-3.x probability dice


【解决方案1】:

好的,除非我误解了游戏,否则我认为您的问题是您将回合得分添加到每一轮的最终得分中,而不是添加回合得分和每轮的原始最终分数(即,即使玩家在随后的掷骰中掷出 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

【讨论】:

  • 谢谢!我会试着理解你的代码!到目前为止有效!但最终分数有时会超过 110...
  • @ggezpython3 对不起,如果我没有在我的代码中包含足够的解释。我所做的和你所做的唯一真正的区别是,直到 final_score 超过 100 之后,我才将 turn_score 添加到 final_score。再看一遍,我认为我的代码实际上不能正常工作(因为 while 循环可能会在不增加最终分数的情况下结束)。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-03-06
  • 1970-01-01
  • 1970-01-01
  • 2021-11-03
  • 1970-01-01
相关资源
最近更新 更多