用visual studio2010旗舰版visual c#控制台应用程序写一个石头剪刀布的游戏代码
时间: 2024-12-19 08:21:41 浏览: 21
在Visual Studio 2010中创建一个控制台版本的石头、剪刀、布游戏,你可以按照以下步骤编写基本的代码:
```csharp
using System;
class Game
{
enum Choice
{
Rock,
Paper,
Scissors
}
static void Main()
{
Console.WriteLine("欢迎来到石头、剪刀、布游戏!");
Random rand = new Random();
int playerChoice;
Choice computerChoice;
do
{
// 让玩家选择
Console.Write("请输入你的选择 (R - 石头, P - 纸, S - 布): ");
string input = Console.ReadLine().ToUpper();
if (input == "R" || input == "P" || input == "S")
playerChoice = input == "R" ? Choice.Rock : input == "P" ? Choice.Paper : Choice.Scissors;
else
{
Console.WriteLine("无效输入,请再试一次.");
continue;
}
// 计算机随机选择
computerChoice = (Choice)rand.Next(0, 3);
// 判断胜负
switch ((int)playerChoice + (int)computerChoice)
{
case 0:
Console.WriteLine("平局! 你出了{0},电脑出了{1}.", playerChoice, computerChoice);
break;
case 1:
if (playerChoice == Choice.Rock && computerChoice == Choice.Scissors)
Console.WriteLine("你赢了! 你出了{0}打败了电脑的{1}.", playerChoice, computerChoice);
else if (playerChoice == Choice.Scissors && computerChoice == Choice.Paper)
Console.WriteLine("你赢了! 你出了{0}打败了电脑的{1}.", playerChoice, computerChoice);
else
Console.WriteLine("你输了! 你出了{0},电脑出了{1}.", playerChoice, computerChoice);
break;
case 2:
if (playerChoice == Choice.Paper && computerChoice == Choice.Rock)
Console.WriteLine("你赢了! 你出了{0}打败了电脑的{1}.", playerChoice, computerChoice);
else if (playerChoice == Choice.Rock && computerChoice == Choice.Scissors)
Console.WriteLine("你输了! 你出了{0},电脑出了{1}.", playerChoice, computerChoice);
else
Console.WriteLine("你输了! 你出了{0},电脑出了{1}.", playerChoice, computerChoice);
break;
}
} while (Console.KeyAvailable); // 循环直到用户按下任意键
Console.WriteLine("游戏结束, 欢迎下次再来!");
}
}
```
阅读全文