帮我生成一个人机大战的游戏
- User
帮我生成一个人机大战的游戏
Assistant
我可以为你设计一个简单的人机对战游戏。这是一个基于终端的猜拳游戏,你可以和电脑进行对战:
import random def rock_paper_scissors(): print("欢迎来到人机大战 - 石头剪刀布!") print("规则: 石头(1) 胜剪刀, 剪刀(2) 胜布, 布(3) 胜石头") choices = {1: "石头", 2: "剪刀", 3: "布"} player_score = 0 computer_score = 0 rounds = 5 for round_num in range(1, rounds + 1): print(f"\n第 {round_num} 回合:") # 玩家选择 while True: try: player_choice = int(input("请选择: 1(石头), 2(剪刀), 3(布): ")) if player_choice in [1, 2, 3]: break else: print("无效输入,请输入1、2或3") except ValueError: print("请输入数字") # 电脑选择 computer_choice = random.randint(1, 3) print(f"你选择了: {choices[player_choice]}") print(f"电脑选择了: {choices[computer_choice]}") # 判断胜负 if player_choice == computer_choice: print("平局!") elif (player_choice == 1 and computer_choice == 2) or \ (player_choice == 2 and computer_choice == 3) or \ (player_choice == 3 and computer_choice == 1): print("你赢了!") player_score += 1 else: print("电脑赢了!") computer_score += 1 print(f"当前比分 - 你: {player_score}, 电脑: {computer_score}") # 最终结果 print("\n游戏结束!") if player_score > computer_score: print(f"恭喜! 你以 {player_score}:{computer_score} 战胜了电脑!") elif player_score < computer_score: print(f"可惜! 你以 {player_score}:{computer_score} 输给了电脑!") else: print(f"平局! 最终比分 {player_score}:{computer_score}") if __name__ == "__main__": rock_paper_scissors()
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.