石头剪刀布小游戏c#代码
时间: 2024-05-02 14:15:03 浏览: 74
HTML5石头剪刀布手机微信小游戏代码.zip
当然可以!以下是一个简单的石头剪刀布小游戏的C#代码示例:
```csharp
using System;
class Program
{
static void Main(string[] args)
{
Console.WriteLine("欢迎来到石头剪刀布游戏!");
while (true)
{
Console.WriteLine("请选择:1. 石头 2. 剪刀 3. 布");
int playerChoice = Convert.ToInt32(Console.ReadLine());
Random random = new Random();
int computerChoice = random.Next(1, 4);
Console.WriteLine("你选择了:" + GetChoiceName(playerChoice));
Console.WriteLine("电脑选择了:" + GetChoiceName(computerChoice));
string result = GetGameResult(playerChoice, computerChoice);
Console.WriteLine(result);
Console.WriteLine("是否继续游戏?(Y/N)");
string continueGame = Console.ReadLine();
if (continueGame.ToUpper() != "Y")
break;
}
Console.WriteLine("游戏结束!");
}
static string GetChoiceName(int choice)
{
switch (choice)
{
case 1:
return "石头";
case 2:
return "剪刀";
case 3:
return "布";
default:
return "";
}
}
static string GetGameResult(int playerChoice, int computerChoice)
{
if (playerChoice == computerChoice)
return "平局!";
else if ((playerChoice == 1 && computerChoice == 2) ||
(playerChoice == 2 && computerChoice == 3) ||
(playerChoice == 3 && computerChoice == 1))
return "你赢了!";
else
return "你输了!";
}
}
```
这段代码实现了一个简单的石头剪刀布游戏。玩家通过输入数字选择石头、剪刀或布,电脑随机生成选择,然后判断胜负并输出结果。玩家可以选择是否继续游戏。
阅读全文