python - 模拟骰子游戏的 Python 程序,两个玩家都由计算机控制
问题描述
程序应该随机选择哪个玩家先走。它应该为每个玩家保留一个总分,并在计算机玩家之间交替轮流,直到一个玩家以 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 循环。有人可以指导我哪里出错了吗?
解决方案
我尽可能地坚持你的格式,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!
推荐阅读
- javascript - 如何更改可编辑 iFrame 中文本的颜色?
- javascript - 单击锚链接时平滑滚动
- heroku - Heroku 和 Cloudflare 集成“没有这样的应用程序”
- json - 将 Postgresql jsonb 列与行数据连接起来
- javascript - Discord.js 对服务器上的所有文本通道进行排序
- protocol-buffers - 删除 protobuf 中的字段
- javascript - BigNumber.js 简单的减法给出错误的输出
- ios - XCODE:标签约束表现得很奇怪
- java - 如何从 txt 文件中读取单独的部分以在 Java GUI 中显示?
- python - 如何基于多级索引计算时间戳的差异?