Python程序,通过计算机控制两个玩家模拟骰子游戏

时间:2018-09-30 23:22:38

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

程序应随机选择先播放哪个播放器。它 应该保持每位玩家的总得分,并且交替交替 计算机玩家,直到一名玩家的回合得分为 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!

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

2 个答案:

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

答案 1 :(得分:0)

roll_AI(player)仅终止if dice_value==1,然后设置point=0并返回0。所以没有积分加起来等于100