Python 模拟骰子游戏的程序,双方都由计算机控制

Python program that simulates a game of dice with both players controlled by the computer

程序应该随机选择哪个玩家先走。它 应该为每个玩家保留一个总分,并且交替轮流 计算机播放直到一个玩家以得分结束其回合 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!

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

roll_AI(player) 仅终止 if dice_value==1,然后设置 point=0 和 returns 0。所以没有积分可以加起来达到 100

尽我所能坚持你的格式,结合使用 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!