
- Python 版本
python
import random
def get_computer_choice(): cnavmall.cn
choices = [‘石头’, ‘剪刀’, ‘布’]
return random.choice(choices)
def determine_winner(player, computer):
if player == computer:
return “平局!”
elif (player == ‘石头’ and computer == ‘剪刀’) or \
(player == ‘剪刀’ and computer == ‘布’) or \
(player == ‘布’ and computer == ‘石头’):
return “你赢了!”
else:
return “你输了!”
def play_game():
choices = [‘石头’, ‘剪刀’, ‘布’]
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()
2. JavaScript (HTML + JavaScript) 版本
html
石头剪刀布游戏
石头 剪刀 布<script>
function getComputerChoice() {
const choices = ['石头', '剪刀', '布'];
return choices[Math.floor(Math.random() * choices.length)];
}
function determineWinner(player, computer) {
if (player === computer) {
return "平局!";
} else if ((player === '石头' && computer === '剪刀') ||
(player === '剪刀' && computer === '布') ||
(player === '布' && computer === '石头')) {
return "你赢了!";
} else {
return "你输了!";
}
}
function playGame(playerChoice) {
const computerChoice = getComputerChoice();
const result = determineWinner(playerChoice, computerChoice);
document.getElementById('result').innerText = `你选择了${playerChoice},计算机选择了${computerChoice}。${result}`;
}
</script>
3. C# 版本 (控制台应用程序)
csharp
using System;
class Program
{
static string GetComputerChoice()
{
string[] choices = { “石头”, “剪刀”, “布” };
Random rand = new Random();
return choices[rand.Next(choices.Length)];
}
static string DetermineWinner(string player, string computer)
{
if (player == computer)
return "平局!";
else if ((player == "石头" && computer == "剪刀") ||
(player == "剪刀" && computer == "布") ||
(player == "布" && computer == "石头"))
return "你赢了!";
else
return "你输了!";
}
static void Main()
{
Console.WriteLine("请选择石头、剪刀或布(输入对应的中文):");
string playerChoice = Console.ReadLine();
string computerChoice = GetComputerChoice();
Console.WriteLine($"计算机选择了:{computerChoice}");
string result = DetermineWinner(playerChoice, computerChoice);
Console.WriteLine(result);
}
}
这些示例展示了如何在不同编程语言中实现一个简单的“石头、剪刀、布”游戏。每个示例都包含了玩家输入、计算机随机选择、以及判断胜负的逻辑。当然,我可以为你提供几种不同编程语言中实现的简单小游戏代码示例。这次,我将选择实现一个经典的“石头、剪刀、布”游戏。这个游戏需要玩家和计算机各自选择一个手势(石头、剪刀、布),然后比较这两个手势来决定胜负。
- Python 版本
python
import random
def get_computer_choice():
choices = [‘石头’, ‘剪刀’, ‘布’]
return random.choice(choices)
def determine_winner(player, computer):
if player == computer:
return “平局!”
elif (player == ‘石头’ and computer == ‘剪刀’) or \
(player == ‘剪刀’ and computer == ‘布’) or \
(player == ‘布’ and computer == ‘石头’):
return “你赢了!”
else:
return “你输了!”
def play_game():
choices = [‘石头’, ‘剪刀’, ‘布’]
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()
2. JavaScript (HTML + JavaScript) 版本
html
石头剪刀布游戏
石头 剪刀 布<script>
function getComputerChoice() {
const choices = ['石头', '剪刀', '布'];
return choices[Math.floor(Math.random() * choices.length)];
}
function determineWinner(player, computer) {
if (player === computer) {
return "平局!";
} else if ((player === '石头' && computer === '剪刀') ||
(player === '剪刀' && computer === '布') ||
(player === '布' && computer === '石头')) {
return "你赢了!";
} else {
return "你输了!";
}
}
function playGame(playerChoice) {
const computerChoice = getComputerChoice();
const result = determineWinner(playerChoice, computerChoice);
document.getElementById('result').innerText = `你选择了${playerChoice},计算机选择了${computerChoice}。${result}`;
}
</script>
3. C# 版本 (控制台应用程序)
csharp
using System;
class Program
{
static string GetComputerChoice()
{
string[] choices = { “石头”, “剪刀”, “布” };
Random rand = new Random();
return choices[rand.Next(choices.Length)];
}
static string DetermineWinner(string player, string computer)
{
if (player == computer)
return "平局!";
else if ((player == "石头" && computer == "剪刀") ||
(player == "剪刀" && computer == "布") ||
(player == "布" && computer == "石头"))
return "你赢了!";
else
return "你输了!";
}
static void Main()
{
Console.WriteLine("请选择石头、剪刀或布(输入对应的中文):");
string playerChoice = Console.ReadLine();
string computerChoice = GetComputerChoice();
Console.WriteLine($"计算机选择了:{computerChoice}");
string result = DetermineWinner(playerChoice, computerChoice);
Console.WriteLine(result);
}
}
这些示例展示了如何在不同编程语言中实现一个简单的“石头、剪刀、布”游戏。每个示例都包含了玩家输入、计算机随机选择、以及判断胜负的逻辑。
,然后比较这两个手势来决定&spm=1001.2101.3001.5002&articleId=140263306&d=1&t=3&u=00373e2d66414bd093337455dc28146a)
2547

被折叠的 条评论
为什么被折叠?



