石头剪刀布游戏的编程可以通过多种编程语言实现,这里我将提供几种常见编程语言的实现方法。
Python 示例代码
```python
import random
def rock_paper_scissors():
options = ["剪刀", "石头", "布"]
player_choice = input("请输入剪头石头布类型,剪刀(0),石头(1),布(2): ")
computer_choice = random.randint(0, 2)
if player_choice == "剪刀" and computer_choice == 0:
print("你赢了")
elif player_choice == "石头" and computer_choice == 1:
print("你赢了")
elif player_choice == "布" and computer_choice == 2:
print("你赢了")
elif player_choice == 0 and computer_choice == 1:
print("你输了")
elif player_choice == 1 and computer_choice == 2:
print("你输了")
elif player_choice == 2 and computer_choice == 0:
print("你输了")
else:
print("平局啦")
rock_paper_scissors()
```
C 示例代码
```csharp
using System;
class RockPaperScissors
{
static void Main()
{
string[] options = { "剪刀", "石头", "布" };
Random random = new Random();
int playerScore = 0, computerScore = 0;
Console.WriteLine("欢迎来到剪刀石头布游戏!");
while (true)
{
Console.WriteLine("\n请选择:");
Console.WriteLine("0 - 剪刀");
Console.WriteLine("1 - 石头");
Console.WriteLine("2 - 布");
int playerChoice = Convert.ToInt32(Console.ReadLine());
int computerChoice = random.Next(0, 3);
if (playerChoice == 0 && computerChoice == 1)
{
Console.WriteLine("你赢了");
}
else if (playerChoice == 1 && computerChoice == 2)
{
Console.WriteLine("你赢了");
}
else if (playerChoice == 2 && computerChoice == 0)
{
Console.WriteLine("你赢了");
}
else if (playerChoice == 0 && computerChoice == 1)
{
Console.WriteLine("你输了");
}
else if (playerChoice == 1 && computerChoice == 2)
{
Console.WriteLine("你输了");
}
else if (playerChoice == 2 && computerChoice == 0)
{
Console.WriteLine("你输了");
}
else
{
Console.WriteLine("平局啦");
}
}
}
}
```
JavaScript 示例代码
```javascript
function rockPaperScissors() {
const options = ["剪刀", "石头", "布"];
const playerChoice = prompt("请输入剪头石头布类型,剪刀(0),石头(1),布(2): ");
const computerChoice = Math.floor(Math.random() * 3);
if (playerChoice === "剪刀" && computerChoice === 0) {
alert("你赢了");
} else if (playerChoice === "石头" && computerChoice === 1) {
alert("你赢了");
} else if (playerChoice === "布" && computerChoice === 2) {
alert("你赢了");
} else if (playerChoice === 0 && computerChoice === 1) {
alert("你输了");
} else if (playerChoice === 1 && computerChoice === 2) {
alert("你输了");
} else if (playerChoice === 2 && computerChoice === 0) {
alert("你输了");
} else {
alert("平局啦");
}
}
rockPaperScissors();
```
这些代码示例展示了如何使用不同编程语言实现石头剪刀布游戏的基本逻辑。你可以选择适合你的编程语言进行尝试和修改。