Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/279.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/wpf/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python程序,模拟由计算机控制的两个玩家玩骰子游戏_Python_Python 3.x_Statistics_Probability_Dice - Fatal编程技术网

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

Python程序,模拟由计算机控制的两个玩家玩骰子游戏,python,python-3.x,statistics,probability,dice,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 =

该程序应随机选择哪个玩家先走。信息技术 应该为每个玩家保留一个总分,并在两个回合之间交替轮换 电脑会一直播放,直到有一个玩家的回合结束时得分为 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!.")

我的程序正在运行一个无限while循环。有人能告诉我哪里出了问题吗?

掷骰AI(玩家)
仅在骰子值=1时终止
,然后设置
点=0
并返回
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))

所以我应该添加一个break语句?我希望循环在掷完1后停止,并继续下一个玩家回合。@ggezpython3使用
break
可能看起来更漂亮,但这不是问题所在。为什么要在
if dice_value==1:
块中设置
point=0
?要在出现1时重置它,我尝试使用调试器并单步执行代码。iPython有一个很好的交互式调试器,具有良好的自动完成功能。
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!