Python 版本lnkryy.cn
Python 版本的“石头剪刀布”游戏是一个控制台应用。
python
import random
choices = [‘石头’, ‘剪刀’, ‘布’]
def get_player_choice():
while True:
choice = input("请输入你的选择(石头/剪刀/布): ").strip().lower()
if choice in choices:
return choice
else:
print(“无效的选择,请重新输入。”)
def determine_winner(player_choice, computer_choice):
if player_choice == computer_choice:
return “平局!”
elif (player_choice == ‘石头’ and computer_choice == ‘剪刀’) or \
(player_choice == ‘剪刀’ and computer_choice == ‘布’) or \
(player_choice == ‘布’ and computer_choice == ‘石头’):
return “你赢了!”
else:
return “你输了!”
def rock_paper_scissors():
print(“欢迎来到石头剪刀布游戏!”)
player_choice = get_player_choice()
computer_choice = random.choice(choices)
print(f"你的选择是:{player_choice},电脑的选择是:{computer_choice}")
result = determine_winner(player_choice, computer_choice)
print(result)
if name == “main”:
rock_paper_scissors()
JavaScript 版本(网页版)
这个JavaScript代码可以嵌入到HTML文件中,用于在浏览器中运行。
html
石头剪刀布游戏
开始游戏 C# 版本(控制台应用) 这个C#示例是一个简单的控制台应用程序,不包含Unity或任何图形用户界面。csharp
using System;
class Program
{
static string[] choices = { “石头”, “剪刀”, “布” };
static string GetPlayerChoice()
{
Console.Write("请输入你的选择(石头/剪刀/布): ");
string choice = Console.ReadLine().Trim().ToLower();
if (!Array.Exists(choices, element => element == choice))
{
Console.WriteLine("无效的选择,请重新运行程序。");
Environment.Exit(1); // 或者其他错误处理
}
return choice;
}
static string DetermineWinner(string playerChoice, string computerChoice)
{
if (playerChoice == computerChoice)
return "平局!";
if ((playerChoice == "石头" && computerChoice == "剪刀") ||
(playerChoice == "剪刀" && computerChoice == "布") ||
(playerChoice == "布" && computerChoice
当然,我将为你提供三种不同编程语言的小程序游戏代码示例,这次我们将实现一个简单的“石头剪刀布”游戏。
Python 版本
Python 版本的“石头剪刀布”游戏是一个控制台应用。
python
import random
choices = [‘石头’, ‘剪刀’, ‘布’]
def get_player_choice():
while True:
choice = input("请输入你的选择(石头/剪刀/布): ").strip().lower()
if choice in choices:
return choice
else:
print(“无效的选择,请重新输入。”)
def determine_winner(player_choice, computer_choice):
if player_choice == computer_choice:
return “平局!”
elif (player_choice == ‘石头’ and computer_choice == ‘剪刀’) or \
(player_choice == ‘剪刀’ and computer_choice == ‘布’) or \
(player_choice == ‘布’ and computer_choice == ‘石头’):
return “你赢了!”
else:
return “你输了!”
def rock_paper_scissors():
print(“欢迎来到石头剪刀布游戏!”)
player_choice = get_player_choice()
computer_choice = random.choice(choices)
print(f"你的选择是:{player_choice},电脑的选择是:{computer_choice}")
result = determine_winner(player_choice, computer_choice)
print(result)
if name == “main”:
rock_paper_scissors()
JavaScript 版本(网页版)
这个JavaScript代码可以嵌入到HTML文件中,用于在浏览器中运行。
html
石头剪刀布游戏
开始游戏 C# 版本(控制台应用) 这个C#示例是一个简单的控制台应用程序,不包含Unity或任何图形用户界面。csharp
using System;
class Program
{
static string[] choices = { “石头”, “剪刀”, “布” };
static string GetPlayerChoice()
{
Console.Write("请输入你的选择(石头/剪刀/布): ");
string choice = Console.ReadLine().Trim().ToLower();
if (!Array.Exists(choices, element => element == choice))
{
Console.WriteLine("无效的选择,请重新运行程序。");
Environment.Exit(1); // 或者其他错误处理
}
return choice;
}
static string DetermineWinner(string playerChoice, string computerChoice)
{
if (playerChoice == computerChoice)
return "平局!";
if ((playerChoice == "石头" && computerChoice == "剪刀") ||
(playerChoice == "剪刀" && computerChoice == "布") ||
(playerChoice == "布" && computerChoice