Python 版本grsyzp.cn
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"你的选择是:{player_choice},计算机的选择是:{computer_choice}")
print(determine_winner(player_choice, computer_choice))
play_game()
Java 版本
java
import java.util.Random;
import java.util.Scanner;
public class RockPaperScissors {
public static String getComputerChoice() {
String[] choices = {“石头”, “剪刀”, “布”};
Random rand = new Random();
return choices[rand.nextInt(choices.length)];
}
public static String determineWinner(String playerChoice, String computerChoice) {
if (playerChoice.equals(computerChoice)) {
return "平局!";
} else if ((playerChoice.equals("石头") && computerChoice.equals("剪刀")) ||
(playerChoice.equals("剪刀") && computerChoice.equals("布")) ||
(playerChoice.equals("布") && computerChoice.equals("石头"))) {
return "你赢了!";
} else {
return "你输了!";
}
}
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("欢迎来到石头剪刀布游戏!");
System.out.print("请选择石头、剪刀或布:");
String playerChoice = scanner.nextLine();
String computerChoice = getComputerChoice();
System.out.println(String.format("你的选择是:%s,计算机的选择是:%s", playerChoice, computerChoice));
System.out.println(determineWinner(playerChoice, computerChoice));
scanner.close();
}
}
C# 版本
csharp
using System;
class RockPaperScissors
{
static string GetComputerChoice()
{
string[] choices = { “石头”, “剪刀”, “布” };
Random rand = new Random();
return choices[rand.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()
{
Console.WriteLine("欢迎来到石头剪刀布游戏!");
Console.Write("请选择石头、剪刀或布:");
string playerChoice = Console.ReadLine();
string computerChoice = GetComputerChoice();
Console.WriteLine($"你的选择是:{playerChoice},计算机的选择是:{computerChoice}");
Console.WriteLine(DetermineWinner(playerChoice, computerChoice));
}
}
这三个示例都实现了“石头剪刀布”游戏,但是使用了不同的编程语言。每个示例都包含生成计算机选择、获取玩家输入、判断胜负以及输出结果的逻辑。当然,我会为你提供几种不同编程语言实现的一个简单小游戏——“石头剪刀布”的示例代码。这个游戏涉及玩家和计算机各自选择一个手势(石头、剪刀、布),然后比较两者来决定胜负。
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"你的选择是:{player_choice},计算机的选择是:{computer_choice}")
print(determine_winner(player_choice, computer_choice))
play_game()
Java 版本
java
import java.util.Random;
import java.util.Scanner;
public class RockPaperScissors {
public static String getComputerChoice() {
String[] choices = {“石头”, “剪刀”, “布”};
Random rand = new Random();
return choices[rand.nextInt(choices.length)];
}
public static String determineWinner(String playerChoice, String computerChoice) {
if (playerChoice.equals(computerChoice)) {
return "平局!";
} else if ((playerChoice.equals("石头") && computerChoice.equals("剪刀")) ||
(playerChoice.equals("剪刀") && computerChoice.equals("布")) ||
(playerChoice.equals("布") && computerChoice.equals("石头"))) {
return "你赢了!";
} else {
return "你输了!";
}
}
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("欢迎来到石头剪刀布游戏!");
System.out.print("请选择石头、剪刀或布:");
String playerChoice = scanner.nextLine();
String computerChoice = getComputerChoice();
System.out.println(String.format("你的选择是:%s,计算机的选择是:%s", playerChoice, computerChoice));
System.out.println(determineWinner(playerChoice, computerChoice));
scanner.close();
}
}
C# 版本
csharp
using System;
class RockPaperScissors
{
static string GetComputerChoice()
{
string[] choices = { “石头”, “剪刀”, “布” };
Random rand = new Random();
return choices[rand.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()
{
Console.WriteLine("欢迎来到石头剪刀布游戏!");
Console.Write("请选择石头、剪刀或布:");
string playerChoice = Console.ReadLine();
string computerChoice = GetComputerChoice();
Console.WriteLine($"你的选择是:{playerChoice},计算机的选择是:{computerChoice}");
Console.WriteLine(DetermineWinner(playerChoice, computerChoice));
}
}
这三个示例都实现了“石头剪刀布”游戏,但是使用了不同的编程语言。每个示例都包含生成计算机选择、获取玩家输入、判断胜负以及输出结果的逻辑。