标签:
程序入口:Main()方法:
namespace Guess
{
class Program
{
static void Main(string[] args)
{
Game game = new Game();
game.StartGame();
}
}
}
1.Game类:
namespace Guess
{
class Game
{
Computer c = new Computer();
Person p = new Person();
int count = 0;
public void StartGame()
{
Console.WriteLine("开始游戏");
Console.WriteLine("游戏规则:1.剪刀 2.石头 3.布(请输入相应的数字就OK啦!!!)");
Console.WriteLine("先选择你的挑战对手吧:1.刘备 2.孙权 3.曹操(同上)");
int i = Convert.ToInt16((Console.ReadLine()));
switch(i)
{
case 1:
c.name = "刘备";
Console.WriteLine("逆选择的对手是:{0}\n",c.name);
break;
case 2:
c.name = "孙权";
Console.WriteLine("你选择的对手是:{0}\n",c.name);
break;
case 3:
c.name = "曹操";
Console.WriteLine("你选择的对手是{0}\n:",c.name);
break;
default:
Console.Write("输入有误,退出系统 ");
//System.Diagnostics.Process.GetCurrentProcess().Kill();
Environment.Exit(0);
break;
}
Console.WriteLine("请输入你的名字:");
p.name = Convert.ToString(Console.ReadLine());
Console.WriteLine("");
Console.WriteLine(p.name + "vs" + c.name+"\n");
Console.WriteLine("要开始吗?(y/n)");
string ans = Convert.ToString(Console.ReadLine());
while ("y".Equals(ans))
{
int pID = p.chuquan();
int cID = c.chuquan();
if (pID == cID)
{
Console.WriteLine("平局");
count++;
}
else if (cID == 1 && pID == 3 || cID == 2 && pID == 1 || cID == 3 && pID == 2)
{
Console.WriteLine("{0}胜利", c.name);
c.scoer++;
}
else if (pID == 1 && cID == 3 || pID == 2 && cID == 1 || pID == 3 && cID == 2)
{
Console.WriteLine("你赢了");
p.scoer++;
}
Console.WriteLine("是否继续游戏(y/n)");
ans = Convert.ToString(Console.ReadLine());
}
Console.WriteLine("游戏结束,结果如下\n");
Console.WriteLine("平 局:{0}",count);
Console.WriteLine("{0}胜: {1}",c.name,c.scoer);
Console.WriteLine("我 胜: {0}",p.scoer);
}
}
}
2.Person类;
namespace Guess
{
class Person
{
public string name;
public int scoer;
public Person()
{
}
public int chuquan()
{
Console.WriteLine("请你出拳:");
int ID = Convert.ToInt16(Console.ReadLine());
switch(ID)
{
case 1: Console.WriteLine("我出拳为剪刀");
break;
case 2: Console.WriteLine("我出拳为石头");
break;
case 3:Console.WriteLine("我出拳为布");
break;
default:
Console.WriteLine("输入有误");
Environment.Exit(0);
break;
}
return ID;
}
}
}
3.电脑Computer类:
namespace Guess
{
class Computer
{
public Computer()
{
}
public string name;
public int scoer;
public int chuquan()
{
Random random = new Random();
int ID = random.Next(1, 4);
switch (ID)
{
case 1: Console.WriteLine(name+"出拳:剪刀");
break;
case 2: Console.WriteLine(name+"出拳;石头");
break;
case 3: Console.WriteLine(name+"出拳:布");
break;
}
return ID;
}
}
}
人机猜拳C#实现
标签:
原文地址:http://www.cnblogs.com/buazgc/p/4657778.html