1.要求玩家对象(Player)、计算机对象(Computer)、裁判对象(Judge)。 玩家出拳由用户控制,使用数字代表:1石头、2剪子、3布 计算机出拳由计算机随机产生 裁判根据玩家与计算机的出拳情况进行判断输赢
玩家对象代码:
class Player
{
public Player(string a)
{
Name = a;
}
public string Name { get; set; }
public int ShowResult()
{
Console.WriteLine("选择:1.石头,2.剪刀,3.布");
string type = Console.ReadLine();
while (!(type == "1" || type == "2" || type == "3"))
{
Console.WriteLine("请重新选择输入正确的指令");
type = Console.ReadLine();
}
switch (int.Parse(type))
{
case 1:
Console.WriteLine(Name+"选择的是石头");
break;
case 2:
Console.WriteLine(Name+"选择的是剪刀");
break;
case 3:
Console.WriteLine(Name+"选择的是布");
break;
default:
break;
}
return int.Parse( type);
}
}
电脑 对象 代码:
class Computer
{
Random r = new Random();
public int ShowResult()
{
int type = r.Next(1, 4);
switch (type)
{
case 1:
Console.WriteLine( "电脑选择的是石头");
break;
case 2:
Console.WriteLine( "电脑选择的是剪刀");
break;
case 3:
Console.WriteLine( "电脑选择的是布");
break;
default:
break;
}
return type;
}
}
裁判对象代码:
class Judg
{
//石头1
//剪刀2
//布3
public void ShowResult(int play,int computer)
{
if (play-computer==-2||play-computer==1)
{
Console.WriteLine("玩家败!");
}
else if (play==computer)
{
Console.WriteLine("平局");
}
else
{
Console.WriteLine("玩家胜");
}
}
}
Main函数代码实现:
class Program
{
static void Main(string[] args)
{
while (true)
{
Player p = new Player("张三");
Computer com = new Computer();
Judg judg = new Judg();
judg.ShowResult(p.ShowResult(), com.ShowResult());
Console.ReadLine();
}
}
}
博客介绍了石头剪刀布游戏的代码实现,要求创建玩家、计算机和裁判对象。玩家出拳由用户控制,用数字代表不同手势;计算机出拳随机产生;裁判根据双方出拳判断输赢,并给出了玩家、电脑、裁判对象及Main函数的代码实现。
2639

被折叠的 条评论
为什么被折叠?



