1.使用GUID实现真正的随机数
2.重复游戏
3.计算分数
using System;
using System.Diagnostics;
namespace 猜拳小游戏
{
/// <summary>
/// 0代表石头,1代表剪刀,2代表布
/// 三局两胜,结束打印出分数和胜利者
/// </summary>
class Program
{
static void Main(string[] args)
{
int yourScore = 0; //你的分数
int computerScore = 0; //电脑的分数
string[] descs = new string[3] { "石头","剪刀","布"};
Console.WriteLine("开始玩猜拳小游戏!");
while (true)
{
for (int i = 0; i < 3; i++)
{
Console.WriteLine("输入你出什么?");
string yourDesc = string.Empty;
int yourSelector = -1;
while (true)
{
yourDesc = Console.ReadLine();
if(yourDesc == "石头")
{
yourSelector = 0;
break;
}
else if(yourDesc == "剪刀")
{
yourSelector = 1;
break;
}
else if(yourDesc == "布")
{
yourSelector = 2;
break;
}
else
{
Console.WriteLine("输入错误,情重新输入!");
}
}
Console.WriteLine("你选择的是" + yourDesc);
int computerSelector = GetRandomByGuid(0, 3);
string result = string.Empty;
switch (IsWin(yourSelector,computerSelector))
{
case 0:
result = "你输了!";
computerScore++;
break;
case 1:
result = "平局!";
break;
case 2:
result = "你赢了!";
yourScore++;
break;
default:
break;
}
Console.WriteLine(computerSelector + "计算机选择" + descs[computerSelector]+" "+result);
}
Console.WriteLine("电脑分数:"+computerScore);
Console.WriteLine("你的分数:"+yourScore);
string winer="";
if (computerScore > yourScore)
winer = "胜利者:电脑";
else if (computerScore < yourScore)
winer = "胜利者:你";
else
winer = "平手";
Console.WriteLine("最终结果:"+winer);
Console.WriteLine("是否需要重来:输入1重来,输入其他退出!");
char other = Console.ReadKey().KeyChar;
if(other!='1')
{
return;
}
}
}
/// <summary>
/// 判断一局的正负
/// </summary>
/// <param name="yourSelector"></param>
/// <param name="computerSelector"></param>
/// <returns></returns>
static int IsWin(int yourSelector,int computerSelector)
{
if (yourSelector == computerSelector)
return 1;
else if (
yourSelector == 0 && computerSelector == 1
||
yourSelector == 1 && computerSelector == 2
||
yourSelector == 2 && computerSelector == 0
)
{
return 2;
}
return 0;
}
/// <summary>
/// 使用Guid产生的种子生成真随机数
/// </summary>
static int GetRandomByGuid(int min,int max)
{
Random random = new Random(GetRandomSeedbyGuid());
int randNum = random.Next(min,max);
return randNum;
}
/// <summary>
/// 使用Guid生成种子
/// </summary>
/// <returns></returns>
static int GetRandomSeedbyGuid()
{
return new Guid().GetHashCode();
}
}
}