可以与人类玩家和计算机一起运行猪游戏的Python程序

时间:2018-10-03 23:36:28

标签: 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 个答案:

答案 0 :(得分: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