写一个猜拳游戏,你与电脑不断猜拳,有特定结束方式 且能够输出每一局结果、结果统计
时间: 2024-06-06 08:06:52 浏览: 135
人与电脑的猜拳小游戏
5星 · 资源好评率100%
import random
# 定义猜拳结果字典
result_dict = {"石头": {"石头": "平局", "剪刀": "你胜利", "布": "电脑胜利"},
"剪刀": {"石头": "电脑胜利", "剪刀": "平局", "布": "你胜利"},
"布": {"石头": "你胜利", "剪刀": "电脑胜利", "布": "平局"}}
# 定义游戏次数
rounds = 0
# 定义电脑胜、你胜、平局次数
computer_win = 0
player_win = 0
draw = 0
print("欢迎来到猜拳游戏!")
while True:
# 电脑随机出拳
computer_choice = random.choice(["石头", "剪刀", "布"])
# 玩家输入出拳
player_choice = input("请输入你的出拳(石头、剪刀、布):")
# 判断输入是否正确
if player_choice not in ["石头", "剪刀", "布"]:
print("输入错误,请重新输入!")
continue
# 输出结果
print(f"电脑出了{computer_choice},你出了{player_choice},本局{result_dict[player_choice][computer_choice]}!")
# 统计胜负次数
if result_dict[player_choice][computer_choice] == "电脑胜利":
computer_win += 1
elif result_dict[player_choice][computer_choice] == "你胜利":
player_win += 1
else:
draw += 1
# 游戏次数加一
rounds += 1
# 判断游戏是否结束
if rounds == 5 or player_win == 3 or computer_win == 3:
print("游戏结束!")
print(f"你和电脑的对局次数为{rounds}次,其中电脑胜利{computer_win}次,你胜利{player_win}次,平局{draw}次。")
if player_win > computer_win:
print("恭喜你获得了最终的胜利!")
elif player_win < computer_win:
print("很遗憾,电脑获得了最终的胜利!")
else:
print("最终的胜负居然是平局!")
break
阅读全文