【问题标题】:Python program that simulates a game of dice with both players controlled by the computer模拟骰子游戏的 Python 程序,两个玩家都由计算机控制
【发布时间】:2019-03-06 01:57:18
【问题描述】:

程序应该随机选择哪个玩家先走。它 应保持每个玩家的总分,并交替轮流 电脑玩家,直到一名玩家以分数结束其回合 100 或更高。

这是我尝试解决问题的方法:

import random
print("Well, hello there")
def roll_the_dice():
    dice_value = random.randint(1,6)
    return dice_value
def roll_AI(player):
    point = 0
   checker = 1
   while (checker==1):
        dice_value = roll_the_dice()
        print("- rolled a :" , dice_value)
        if dice_value==1:
            print("Pigged out, mate!")
            point=0
            checker=0
        else:
            point += dice_value
            print("Your total be: ",point)
    print("Turns over, mate!")
    return point
playerOne = 0 #Score of P1
playerTwo = 0 #Score of P2
while (playerOne<100 and playerTwo<100):
    whos_turn = random.randint(1,2)
    if (whos_turn==1):
        print("Initial point of both of these bots be 0.")
        dice_value = roll_AI(1)
        playerOne+=dice_value
        print("Now, player one score: ",playerOne)
    else:
        dice_value = roll_AI(2)
        playerTwo += dice_value
        print("Now, player two score: ",playerTwo)
    print("GAMES OVER!")
    print("Player One:",playerOne)
    print("Player Two:",playerTwo)
if playerOne>playerTwo:
    print("Player one wins!")
elif playerTwo>playerOne:
    print("Player two wins!")
else:
    print("ITS A DRAW!.")

这是程序应该运行的示例输出:

Player One’s score: 0
Player Two’s score: 0
It’s Player One’s turn
- rolled a 1
Pigged out!
Total turn score = 0
Player One’s score: 0
Player Two’s score: 0
It’s Player Two’s turn
- rolled a 6
- rolled a 2
...
Player One’s score: 85
Player Two’s score: 88
It’s Player One’s turn
- rolled a 2
- rolled a 6
- rolled a 2
- rolled a 4
- rolled a 3
Total turn score = 17
Final score: 102 vs 88
Player One wins!

我的程序正在运行一个无限循环。有人可以指导我哪里出错了吗?

【问题讨论】:

  • 尝试使用调试器并单步执行代码。 iPython 有一个很好的交互式调试器,具有很好的自动完成功能。

标签: python python-3.x statistics probability dice


【解决方案1】:

尽我所能坚持你的格式,使用while True循环与for循环组合,如果任一玩家在他们的回合中击中100,for循环中断以阻止下一个玩家获得回合,然后是 while 循环中断

import random

players = ['player_one', 'vash']
scores = {'player_one': 0, 'vash': 0}
print('Well, hello there')
random.shuffle(players)

while True:
    for i in players:
        turn_score = 0  
        while turn_score <= 20:
            roll = random.randint(1,6)
            if roll == 1:
                turn_score = 0
                scores[i] += 0
                print('\n{} rolled a {}. Pigged out!'.format(i, roll))
                break
            else:
                turn_score += roll
                print('\n{} rolled a {}.'.format(i, roll))
        scores[i] += turn_score
        print('Turn score: {}'.format(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 = [i for i in scores if scores[i] == max(scores.values())]
print('{} is the winner!'.format(*winner))
Well, hello there

vash rolled a 2.

vash rolled a 1. Pigged out!
Turn score: 0
vash score: 0 player_one score: 0

player_one rolled a 5.

player_one rolled a 4.

player_one rolled a 5.

player_one rolled a 6.

player_one rolled a 6.
Turn score: 26
vash score: 0 player_one score: 26

....

vash score: 89 player_one score: 94

vash rolled a 3.

vash rolled a 4.

vash rolled a 4.

vash rolled a 4.

vash rolled a 3.

vash rolled a 6.
Turn score: 24
vash score: 113 player_one score: 94
vash is the winner!

【讨论】:

    【解决方案2】:

    roll_AI(player) 只终止if dice_value==1,但随后设置point=0 并返回0。所以没有积分可以加到 100

    【讨论】:

    • 所以我应该添加一个 break 语句来代替?我希望一旦掷出 1 就停止循环并继续下一个玩家轮流。
    • @ggezpython3 使用break 可能看起来更漂亮,但这不是问题。为什么在if dice_value==1: 块中设置point=0
    • 如果 1 卷起来我猜要重置它
    猜你喜欢
    • 1970-01-01
    • 2014-03-16
    • 2019-05-09
    • 1970-01-01
    • 2015-09-28
    • 1970-01-01
    • 1970-01-01
    • 2011-01-19
    • 1970-01-01
    相关资源
    最近更新 更多