Python 版本hellohailuo.com
python
import random
def get_computer_choice():
choices = [‘石头’, ‘剪刀’, ‘布’]
return random.choice(choices)
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 play_game():
print(“欢迎来到’石头、剪刀、布’游戏!”)
player_choice = input("请输入你的选择(石头/剪刀/布): “)
computer_choice = get_computer_choice()
print(f"计算机的选择是:{computer_choice}”)
result = determine_winner(player_choice, computer_choice)
print(result)
if name == “main”:
play_game()
JavaScript 版本(网页版)
html
石头、剪刀、布游戏
开始游戏 C# 版本(控制台应用) csharp using System;class Program
{
static string GetComputerChoice()
{
string[] choices = { “石头”, “剪刀”, “布” };
Random random = new Random();
return choices[random.Next(choices.Length)];
}
static string DetermineWinner(string playerChoice, string computerChoice)
{
if (playerChoice == computerChoice)
return "平局!";
else if (
(playerChoice == "石头" && computerChoice == "剪刀") ||
(playerChoice == "剪刀" && computerChoice == "布") ||
(playerChoice == "布" && computerChoice == "石头")
)
return "你赢了!";
else
return "你输了!";
}
static void Main(string[] args)
{
Console.WriteLine("欢迎来到'石头、剪刀、布'游戏!");
Console.Write("请输入你的选择(石头/剪刀/布): ");
string playerChoice = Console.ReadLine();
string computerChoice = GetComputerChoice();
Console.WriteLine($"计算机的选择是:{computerChoice}");
string result = DetermineWinner(playerChoice, computerChoice);
Console.WriteLine(result);
}
}
这三个示例展示了如何使用Python、JavaScript(网页版)和C#(控制台应用)来实现一个简单的“石头、剪刀、布”游戏。每个版本都包含了获取玩家和计算机选择、判断胜负以及输出结果当然,我会为你提供几种不同编程语言实现的简单小程序游戏代码示例。这次,我们将实现一个“石头、剪刀、布”游戏,这是一个经典的两人游戏。
Python 版本
python
import random
def get_computer_choice():
choices = [‘石头’, ‘剪刀’, ‘布’]
return random.choice(choices)
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 play_game():
print(“欢迎来到’石头、剪刀、布’游戏!”)
player_choice = input("请输入你的选择(石头/剪刀/布): “)
computer_choice = get_computer_choice()
print(f"计算机的选择是:{computer_choice}”)
result = determine_winner(player_choice, computer_choice)
print(result)
if name == “main”:
play_game()
JavaScript 版本(网页版)
html
石头、剪刀、布游戏
开始游戏 C# 版本(控制台应用) csharp using System;class Program
{
static string GetComputerChoice()
{
string[] choices = { “石头”, “剪刀”, “布” };
Random random = new Random();
return choices[random.Next(choices.Length)];
}
static string DetermineWinner(string playerChoice, string computerChoice)
{
if (playerChoice == computerChoice)
return "平局!";
else if (
(playerChoice == "石头" && computerChoice == "剪刀") ||
(playerChoice == "剪刀" && computerChoice == "布") ||
(playerChoice == "布" && computerChoice == "石头")
)
return "你赢了!";
else
return "你输了!";
}
static void Main(string[] args)
{
Console.WriteLine("欢迎来到'石头、剪刀、布'游戏!");
Console.Write("请输入你的选择(石头/剪刀/布): ");
string playerChoice = Console.ReadLine();
string computerChoice = GetComputerChoice();
Console.WriteLine($"计算机的选择是:{computerChoice}");
string result = DetermineWinner(playerChoice, computerChoice);
Console.WriteLine(result);
}
}
这三个示例展示了如何使用Python、JavaScript(网页版)和C#(控制台应用)来实现一个简单的“石头、剪刀、布”游戏。每个版本都包含了获取玩家和计算机选择、判断胜负以及输出结果