标签:http ar 使用 sp java 数据 on div 2014
现在要制作一个游戏,玩家与计算机进行猜拳游戏,玩家出拳,计算机出拳,计算机自动判断输赢。
根据需求,来分析一下对象,可分析出:玩家对象(Player)、计算机对象(Computer)、裁判对象(Judge)。 玩家出拳由用户控制,使用数字代表:1石头、2剪子、3布 计算机出拳由计算机随机产生 裁判根据玩家与计算机的出拳情况进行判断输赢
玩家类示例代码
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778classPlayer{string name;publicstring Name{get {returnname; }set { name = value; }}publicintShowFist(){Console.WriteLine("请问,你要出什么拳? 1.剪刀 2.石头 3.布");intresult = ReadInt(1,3);string fist = IntToFist(result);Console.WriteLine("玩家:{0}出了1个{1}", name, fist);returnresult;}/// <summary>/// 将用户输入的数字转换成相应的拳头/// </summary>/// <param name="input">/// <returns></returns>privatestring IntToFist(intinput){string result = string.Empty;switch(input){case1:result ="剪刀";break;case2:result ="石头";break;case3:result ="布";break;}returnresult;}/// <summary>/// 从控制台接收数据并验证有效性/// </summary>/// <param name="min">/// <param name="max">/// <returns></returns>privateintReadInt(intmin,intmax){while(true){//从控制台获取用户输入的数据string str = Console.ReadLine();//将用户输入的字符串转换成Int类型intresult;if(int.TryParse(str, out result)){//判断输入的范围if(result >= min && result <= max){returnresult;}else{Console.WriteLine("请输入1个{0}-{1}范围的数", min, max);continue;}}else{Console.WriteLine("请输入整数");}}}}
计算机类示例代码
123456789101112131415161718192021222324252627282930classComputer{//生成一个随机数,让计算机随机出拳Random ran =newRandom();publicintShowFist(){intresult = ran.Next(1,4);Console.WriteLine("计算机出了:{0}", IntToFist(result));returnresult;}privatestring IntToFist(intinput){string result = string.Empty;switch(input){case1:result ="剪刀";break;case2:result ="石头";break;case3:result ="布";break;}returnresult;}}
裁判类示例代码 这个类通过一个特殊的方式来判定结果
12345678910111213141516171819202122classJudge{publicvoidDetermine(intp1,intp2){//1剪刀 2石头 3布//1 3 1-3=-2 在玩家出1剪刀的情况下,计算机出3布,玩家赢//2 1 2-1=1 在玩家出2石头的情况下,计算机出1剪刀,玩家赢//3 2 3-2=1 在玩家出3布的情况下,计算机出2石头,玩家赢if(p1 - p2 == -2|| p1 - p2 ==1){Console.WriteLine("玩家胜利!");}elseif(p1 == p2){Console.WriteLine("平局");}else{Console.WriteLine("玩家失败!");}}}
12345678910111213staticvoidMain(string[] args){Player p1 =newPlayer() { Name="Tony"};Computer c1 =newComputer();Judge j1 =newJudge();while(true){intres1 = p1.ShowFist();intres2 = c1.ShowFist();j1.Determine(res1, res2);Console.ReadKey();}}
标签:http ar 使用 sp java 数据 on div 2014
原文地址:http://www.cnblogs.com/rr163/p/4112909.html