结构体案例之对战游戏
class Program { struct Player { public string name; public int blood; public int attack; public int defence; public int shanbi; public ArrayList jineng ; public ArrayList shanghai; } static void Main(string[] args) { #region 定义技能库 string [] jn = new string[10]{ "横扫千军", "叽叽歪歪", "龙腾", "降龙十八掌", "大鹏展翅", "一阳指", "六脉神剑", "死亡一指", "恩赐解脱", "破釜沉舟", }; int [] sh = new int[10]{ 500, 300, 350, 500, 360, 400, 480, 490, 500, 450 }; #endregion Console.WriteLine("请输入第一个玩家的姓名:"); string name1 = Console.ReadLine(); Console.WriteLine("请输入第二个玩家的姓名:"); string name2 = Console.ReadLine(); #region 造玩家,赋初始值 Player p1 = new Player(); int seed1=0; if (name1.Length == 2) { seed1 = (int)Convert.ToChar(name1.Substring(0, 1)) + (int)Convert.ToChar(name1.Substring(1, 1)); } else { seed1 = (int)Convert.ToChar(name1.Substring(0, 1)) + (int)Convert.ToChar(name1.Substring(1, 1)) + (int)Convert.ToChar(name1.Substring(2, 1)); } Random r1 = new Random(seed1); p1.name = name1; p1.blood = 3000+r1.Next(1500); p1.attack = 200 + r1.Next(100); p1.defence = 80 + r1.Next(50); p1.shanbi = 2+r1.Next(2); p1.jineng = new ArrayList(); p1.shanghai = new ArrayList(); Random rj = new Random(); for(int i=0;i<3;i++) { int jnh = rj.Next(10); string jnm = jn[jnh]; if (!p1.jineng.Contains(jnm)) { p1.jineng.Add(jnm); p1.shanghai.Add(sh[jnh]); } else { i--; } } Player p2 = new Player(); int seed2 = 0; if (name2.Length == 2) { seed2 = (int)Convert.ToChar(name2.Substring(0, 1)) + (int)Convert.ToChar(name2.Substring(1, 1)); } else { seed2 = (int)Convert.ToChar(name2.Substring(0, 1)) + (int)Convert.ToChar(name2.Substring(1, 1)) + (int)Convert.ToChar(name2.Substring(2, 1)); } Random r2 = new Random(seed2); p2.name = name2; p2.blood = 3000 + r2.Next(1500); p2.attack = 200 + r2.Next(100); p2.defence = 80 + r2.Next(50); p2.shanbi = 2 + r2.Next(2); p2.jineng = new ArrayList(); p2.shanghai = new ArrayList(); for (int i = 0; i < 3; i++) { int jnh = rj.Next(10); string jnm = jn[jnh]; if (!p2.jineng.Contains(jnm)) { p2.jineng.Add(jnm); p2.shanghai.Add(sh[jnh]); } else { i--; } } #endregion #region 显示玩家基础信息 Console.WriteLine("玩家1:姓名:{0},血量{1},攻击力{2},防御{3},闪避{4},所会技能1:{5},所会技能2:{6},所会技能3:{7}",p1.name,p1.blood,p1.attack,p1.defence,p1.shanbi,p1.jineng[0],p1.jineng[1],p1.jineng[2]); Console.WriteLine("玩家2:姓名:{0},血量{1},攻击力{2},防御{3},闪避{4},所会技能1:{5},所会技能2:{6},所会技能3:{7}", p2.name, p2.blood, p2.attack, p2.defence, p2.shanbi, p2.jineng[0], p2.jineng[1], p2.jineng[2]); #endregion Console.WriteLine("--------------------------------------------"); Console.WriteLine("请按空格键开始对战"); ConsoleKeyInfo key = Console.ReadKey(); Console.Clear(); if (key.Key.ToString().ToLower() == "spacebar") { #region 对战 while (true) { //判断退出条件 if (p1.blood == 0 && p2.blood == 0) { Console.ForegroundColor = ConsoleColor.Yellow; Console.WriteLine("同归于尽了!"); break; } else if (p1.blood == 0) { Console.ForegroundColor = ConsoleColor.Gray; Console.WriteLine(p2.name + "将" + p1.name + "KO了。。。"); break; } else if (p2.blood == 0) { Console.ForegroundColor = ConsoleColor.Gray; Console.WriteLine(p1.name + "将" + p2.name + "KO了。。。"); break; } //开始打 #region p1打p2 //p1对p2进行殴打 Random rdz = new Random(); int dz1 = rdz.Next(10); if (dz1 > 7) //使用大招 { int dzs = rdz.Next(3); Console.ForegroundColor = ConsoleColor.White; Console.WriteLine(p1.name + "使用了" + p1.jineng[dzs] + ",对" + p2.name + "造成了" + p1.shanghai[dzs] + "伤害!"); p2.blood = p2.blood - Convert.ToInt32(p1.shanghai[dzs]); if (p2.blood <= 0) { p2.blood = 0; } } else //普通攻击 { int ptgjsb = rdz.Next(10); if (ptgjsb < p2.shanbi) //p2闪避 { Console.ForegroundColor = ConsoleColor.Green; Console.WriteLine(p2.name + "躲避了" + p1.name + "的这次攻击!"); } else //未闪避 { int p2sh = (p1.attack + rdz.Next(50)) - (p2.defence + rdz.Next(20)); Console.ForegroundColor = ConsoleColor.Red; Console.WriteLine(p1.name + "对" + p2.name + "进行了普通攻击,造成了" + p2sh + "伤害!"); p2.blood = p2.blood - p2sh; if (p2.blood <= 0) { p2.blood = 0; } } } #endregion #region P2打P1 //p1对p2进行殴打 int dz2 = rdz.Next(10); if (dz2 > 7) //使用大招 { int dzs = rdz.Next(3); Console.ForegroundColor = ConsoleColor.White; Console.WriteLine(p2.name + "使用了" + p2.jineng[dzs] + ",对" + p1.name + "造成了" + p2.shanghai[dzs] + "伤害!"); p1.blood = p1.blood - Convert.ToInt32(p2.shanghai[dzs]); if (p1.blood <= 0) { p1.blood = 0; } } else //普通攻击 { int ptgjsb = rdz.Next(10); if (ptgjsb < p1.shanbi) //p2闪避 { Console.ForegroundColor = ConsoleColor.Green; Console.WriteLine(p1.name + "躲避了" + p2.name + "的这次攻击!"); } else //未闪避 { int p1sh = (p2.attack + rdz.Next(50)) - (p1.defence + rdz.Next(20)); Console.ForegroundColor = ConsoleColor.Red; Console.WriteLine(p2.name + "对" + p1.name + "进行了普通攻击,造成了" + p1sh + "伤害!"); p1.blood = p1.blood - p1sh; if (p1.blood <= 0) { p1.blood = 0; } } } #endregion Console.ForegroundColor = ConsoleColor.DarkYellow; Console.WriteLine(p1.name + "的剩余血量为:" + p1.blood + ", " + p2.name + "的剩余血量为:" + p2.blood); Console.ForegroundColor = ConsoleColor.White; Console.WriteLine("-----------------------------------------"); Console.WriteLine(); Thread.Sleep(2000); } #endregion } } }