猜拳游戏:石头、剪刀、布的游戏
代码如下:
双人对战:
-
-
-
- choices = ["石头", "剪刀", "布"]
- player1_score = 0
- player2_score = 0
-
- while True:
- # 玩家1进行选择
- player1_choice = input("玩家1请出拳(石头、剪刀、布):")
-
- # 玩家2进行选择
- player2_choice = input("玩家2请出拳(石头、剪刀、步):")
-
- # 判断胜负并更新分数
- if player1_choice == player2_choice:
- print("平局!")
- elif (player1_choice == "石头" and player2_choice == "剪刀") or \
- (player1_choice == "剪刀" and player2_choice == "布") or \
- (player1_choice == "布" and player2_choice == "石头"):
- print("玩家1赢了!")
- player1_score += 1
- else:
- print("玩家1输了!")
- player2_choice += 1
-
- # 显示当前分数
- print(f"当前分数:玩家1 {player1_score} - {player2_score} 玩家2")
-
- # 询问是否继续游戏
- play_again = input("是否继续游戏?(输入 y 继续,其他任意键退出):")
- if play_again.lower() != "y":
- break
-
- print("游戏结束!")
人机对战:
- import random
-
- choices = ["石头", "剪刀", "布"]
- player_score = 0
- computer_score = 0
-
- while True:
- # 玩家进行选择
- player_choice = input("请出拳(石头、剪刀、布):")
-
- # 随机生成计算机的选择
- computer_choice = random.choice(choices)
-
- # 判断胜负并更新分数
- if player_choice == computer_choice:
- print("平局!")
- elif (player_choice == "石头" and computer_choice == "剪刀") or \
- (player_choice == "剪刀" and computer_choice == "布") or \
- (player_choice == "布" and computer_choice == "石头"):
- print("你赢了!")
- player_score += 1
- else:
- print("你输了!")
- computer_score += 1
-
- # 显示当前分数
- print(f"当前分数:玩家 {player_score} - {computer_score} 计算机")
-
- # 询问是否继续游戏
- play_again = input("是否继续游戏?(输入 y 继续,其他任意键退出):")
- if play_again.lower() != "y":
- break
-
- print("游戏结束!")
-
-
-