解决问题
下面是一个使用Python编写的剪刀、石头、布游戏的程序,包含玩家与计算机对战和模拟计算机对战10次的功能。
import random
def get_computer_choice():
return random.randint(0, 2)
def get_user_choice():
choice = input("请输入剪刀(0)、石头(1)、布(2): ")
while choice not in ['0', '1', '2']:
choice = input("输入无效,请输入剪刀(0)、石头(1)、布(2): ")
return int(choice)
def determine_winner(user_choice, computer_choice):
if user_choice == computer_choice:
return "平局,要不再来一局!"
elif (user_choice == 0 and computer_choice == 2) or \
(user_choice == 1 and computer_choice == 0) or \
(user_choice == 2 and computer_choice == 1):
return "恭喜,你赢了!"
else:
return "Game over!"
def play_game():
user_choice = get_user_choice()
computer_choice = get_computer_choice()
print(f"你的选择: {user_choice} (剪刀:0, 石头:1, 布:2)")
print(f"计算机的选择: {computer_choice} (剪刀:0, 石头:1, 布:2)")
result = determine_winner(user_choice, computer_choice)
print(result)
def simulate_games(num_simulations):
wins = 0
losses = 0
draws = 0
for _ in range(num_simulations):
user_choice = random.randint(0, 2)
computer_choice = get_computer_choice()
result = determine_winner(user_choice, computer_choice)
if result == "恭喜,你赢了!":
wins += 1
elif result == "Game over!":
losses += 1
else:
draws += 1
print(f"模拟{num_simulations}次后的结果:")
print(f"玩家赢: {wins}次")
print(f"玩家输: {losses}次")
print(f"平局: {draws}次")
if __name__ == "__main__":
play_game()
simulate_games(10)
代码解释
1. 导入模块:
import random
导入Python的random模块,用于生成随机数。
2. 获取计算机的选择:
def get_computer_choice():
return random.randint(0, 2)
get_computer_choice函数用于生成计算机的随机选择(0, 1, 2)。
3. 获取用户的选择:
def get_user_choice():
choice = input("请输入剪刀(0)、石头(1)、布(2): ")
while choice not in ['0', '1', '2']:
choice = input("输入无效,请输入剪刀(0)、石头(1)、布(2): ")
return int(choice)
get_user_choice函数用于获取用户输入的选择,并确保输入有效。
4. 判断胜负:
def determine_winner(user_choice, computer_choice):
if user_choice == computer_choice:
return "平局,要不再来一局!"
elif (user_choice == 0 and computer_choice == 2) or \
(user_choice == 1 and computer_choice == 0) or \
(user_choice == 2 and computer_choice == 1):
return "恭喜,你赢了!"
else:
return "Game over!"
determine_winner函数用于判断用户和计算机的胜负关系。
5. 进行游戏:
def play_game():
user_choice = get_user_choice()
computer_choice = get_computer_choice()
print(f"你的选择: {user_choice} (剪刀:0, 石头:1, 布:2)")
print(f"计算机的选择: {computer_choice} (剪刀:0, 石头:1, 布:2)")
result = determine_winner(user_choice, computer_choice)
print(result)
play_game函数用于进行一次游戏,获取用户和计算机的选择,判断胜负并输出结果。
6. 模拟游戏:
def simulate_games(num_simulations):
wins = 0
losses = 0
draws = 0
for _ in range(num_simulations):
user_choice = random.randint(0, 2)
computer_choice = get_computer_choice()
result = determine_winner(user_choice, computer_choice)
if result == "恭喜,你赢了!":
wins += 1
elif result == "Game over!":
losses += 1
else:
draws += 1
print(f"模拟{num_simulations}次后的结果:")
print(f"玩家赢: {wins}次")
print(f"玩家输: {losses}次")
print(f"平局: {draws}次")
simulate_games函数用于模拟多次游戏,统计玩家的胜负和平局次数。
7. 主程序:
if __name__ == "__main__":
play_game()
simulate_games(10)
在主程序中,首先调用play_game函数进行一次游戏,然后调用simulate_games函数模拟10次游戏。
标签:十次,return,get,python,choice,computer,user,print,剪刀 From: https://blog.csdn.net/2401_87245171/article/details/142435805