ADO.Net(五)——实战:对战游戏

2018-06-23 23:19:29来源:未知 阅读 ()

新老客户大回馈,云服务器低至5折

对战游戏

要求:

  1. 自建数据表(例如:数据表包含:代号,姓名,性别,血量,攻击力,防御力,命中,闪避,等级等字段)
  2. 需要通过程序向数据表添加人员
  3. 添加的时候,根据用户输入的名字,自动计算生成相应的血量、攻击力、防御力等(例如,名字的前两个字作为种子生成血量,名字的第一个字作为种子生成攻击力,名字的第二个字作为种子生成防御力等)
  4. 添加完成用户可以输入两个用户进行对战
  5. 通过程序逻辑实现对战双方的血量变化并显示(使用随机数浮动攻击防御)
  6. 最终血量先到达0的失败,胜利的一方属性增加(比如等级level加1,血量加一些,攻击力加一些等)
  7. 访问数据库采用三个类的模式

数据连接类:

namespace ConsoleApplication1
{
    public class DBConnect
    {
        private static string connstring = System.Configuration.ConfigurationSettings.AppSettings["connstring"];
        public static SqlConnection Conn
        {
            get
            {
                return new SqlConnection(connstring);
            }
        }
    }
}

实体类:

namespace ConsoleApplication1
{
    public class Player
    {
        private string name;
        //玩家姓名
        public string Name     
        {
            get { return name; }
            set { name = value; }
        }
        private bool sex;
        //玩家性别
        public bool Sex
        {
            get { return sex; }
            set { sex = value; }
        }
        private int lev;
        //玩家等级
        public int Lev
        {
            get { return lev; }
            set { lev = value; }
        }
        private int blood;
        //血量
        public int Blood
        {
            get { return blood; }
            set { blood = value; }
        }
        private int attack;
        //攻击
        public int Attack
        {
            get { return attack; }
            set { attack = value; }
        }
        private int defense;
        //防御
        public int Defense
        {
            get { return defense; }
            set { defense = value; }
        }
        private int hit;
        //命中
        public int Hit
        {
            get { return hit; }
            set { hit = value; }
        }
        private int dodge;
        //闪避
        public int Dodge
        {
            get { return dodge; }
            set { dodge = value; }
        }
    }
}
View Code

数据访问类:

namespace ConsoleApplication1
{
    public class PlayerDA
    {
        private SqlConnection _conn;
        private SqlCommand _cmd;
        private SqlDataReader _dr;
        public PlayerDA()
        {
            _conn = DBConnect.Conn;
            _cmd = _conn.CreateCommand();
        }
        //添加人员
        public bool AddPlayer(string name_p, bool sex_p)
        {
            //初始属性
            Player p = new Player();
            int seed = (int)Convert.ToChar(name_p.Substring(0, 1)) + (int)Convert.ToChar(name_p.Substring(1, 1));
            Random rand = new Random(seed);
            p.Lev = 1;
            p.Blood = 1500 + rand.Next(1000);
            p.Attack = 100 + rand.Next(100);
            p.Defense = 10 + rand.Next(100);
            p.Hit = 50 + rand.Next(80);
            p.Dodge = 10 + rand.Next(80);
            //添加
            _cmd.CommandText = "insert into Player values(@name,@sex," + p.Lev + "," + p.Blood + "," + p.Attack + "," + p.Defense + "," + p.Hit + "," + p.Dodge+ ")";
            _cmd.Parameters.Clear();
            _cmd.Parameters.AddWithValue("@name", name_p);
            _cmd.Parameters.AddWithValue("@sex", sex_p);
            _conn.Open();
            int add = _cmd.ExecuteNonQuery();
            _conn.Close();
            if (add > 0)
            {
                return true ;
            }
            else
            {
                return false; 
            }
        }
        //查询
        public  List<Player> Select(string name_p)
        {
            _cmd.CommandText = "select *from Player where Name=@name";
            _cmd.Parameters.Clear();
            _cmd.Parameters.AddWithValue("@name", name_p);
            _conn.Open();
            _dr = _cmd.ExecuteReader();         
            List<Player> list = new List<Player>();
            if (_dr.HasRows)
            {
                while (_dr.Read())
                {
                    Player data = new Player();
                    data.Name = _dr[1].ToString();
                    data.Sex =Convert.ToBoolean(_dr[2]);
                    data.Lev = Convert.ToInt32(_dr[3]);
                    data.Blood = Convert.ToInt32(_dr[4]);
                    data.Attack = Convert.ToInt32(_dr[5]);
                    data.Defense = Convert.ToInt32(_dr[6]);
                    data.Hit = Convert.ToInt32(_dr[7]);
                    data.Dodge = Convert.ToInt32(_dr[8]);
                    list.Add(data);
                }
            }
            _conn.Close();
            return list;
        }

        //修改
        public bool Update(string name)
        {
            List<Player> list = Select(name);
            list[0].Lev += 1;
            list[0].Blood += 150;
            list[0].Attack += 10;
            list[0].Defense += 10;
            list[0].Hit += 15;
            list[0].Dodge += 10;
            _cmd.CommandText = "update Player set Lev=" + list[0].Lev + ",Blood=" + list[0].Blood + ",Attack=" + list[0].Attack + ",Defense=" + list[0].Defense + ",Hit=" + list[0].Hit + ",Dodge=" + list[0].Dodge + " where Name=@name";
            _cmd.Parameters.Clear();
            _cmd.Parameters.AddWithValue("@name", name);
            _conn.Open();
            int n = _cmd.ExecuteNonQuery();
            _conn.Close();
            if (n > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
    }
}
View Code

添加人物:

namespace ConsoleApplication1
{
    class program1
    {
        static void Main1(string[] args)
        {
            while (true)
            {
                Console.WriteLine("请输入姓名:");
                string name = Console.ReadLine();
                Console.WriteLine("请输入性别:");
                bool sex = Console.ReadLine() == "" ? true : false;
                PlayerDA da = new PlayerDA();
                if (da.AddPlayer(name, sex))
                {
                    Console.WriteLine(name + "的属性信息为:");
                    List<Player> list = da.Select(name);
                    foreach (Player data in list)
                    {
                        string ssex = data.Sex ? "" : "";
                        Console.WriteLine("姓名:" + data.Name + "\t" + "性别:" + data.Sex + "\t" + "等级:" + data.Lev + "\t" + "血量:" + data.Blood + "\t" + "攻击:" + data.Attack + "\t" + "防御:" + data.Defense + "\t" + "命中:" + data.Hit + "\t" + "闪避:" + data.Dodge + "\t");
                    }
                }
                else
                {
                    Console.WriteLine("添加人物失败!");
                }
                Console.WriteLine("是否继续添加人物?Y or N");
                string yn = Console.ReadLine();
                if (yn == "Y")
                {
                }
                else
                {
                    Console.WriteLine("输入完毕");
                    break;
                }
            }
            Console.ReadLine();
        }
    }
}
View Code

对战:

namespace ConsoleApplication1
{
    class program
    {
        //攻击函数
        static Player gj(Player p_1, Player p_2)
        {
            Random rand = new Random();
            Console.ForegroundColor = ConsoleColor.Gray;
            Console.WriteLine(p_1.Name + "准备发起攻击... ...");
            Console.ForegroundColor = ConsoleColor.Black;

            System.Threading.Thread.Sleep(1000);
            int h2 = rand.Next(100) + p_1.Hit;
            int d1 = rand.Next(100) + p_2.Dodge;
            if (h2 > 100)
            {
                if (d1 > 150)
                {
                    Console.ForegroundColor = ConsoleColor.Green;
                    Console.WriteLine(p_2.Name + "躲开了" + p_1.Name + "的攻击");
                    Console.ForegroundColor = ConsoleColor.Black;
                }
                else
                {
                    int sh = rand.Next(200) + p_1.Attack;
                    p_2.Blood = p_2.Blood - sh + p_2.Defense;
                    Console.ForegroundColor = ConsoleColor.Red;
                    Console.WriteLine(p_1.Name + "攻击掉" + p_2.Name + (sh - p_2.Defense) + "");
                    Console.ForegroundColor = ConsoleColor.Black;
                }
            }
            else
            {
                Console.ForegroundColor = ConsoleColor.DarkYellow;
                Console.WriteLine(p_1.Name + "攻击失误");
                Console.ForegroundColor = ConsoleColor.Black;
            }
            return (p_2);
        }
        static void Main(string[] args)
        {
            Console.BackgroundColor = ConsoleColor.White;
            Console.ForegroundColor = ConsoleColor.Black;
            
            PlayerDA da = new PlayerDA();
            Console.Write("请输入第一个玩家的姓名:");
            string Name_1 = Console.ReadLine();

            List<Player> p1 = da.Select(Name_1);
            Console.WriteLine("*************************");

            Console.Write("请输入第二个玩家的姓名:");
            string Name_2 = Console.ReadLine();

            List<Player> p2 = da.Select(Name_2);
            Console.WriteLine("*************************");
            
            //pk阶段
            while (p1[0].Blood > 0 && p2[0].Blood > 0)
            {
                Player pp2 = gj(p1[0],p2[0]);
                p2[0] = pp2;
                Player pp1 = gj(p2[0],p1[0]);
                p1[0] = pp1;
                Console.WriteLine(p2[0].Name + ":"+(p2[0].Blood<0?0:p2[0].Blood)+"————————"+p1[0].Name+":"+(p1[0].Blood<0?0:p1[0].Blood));
                Console.WriteLine();
                Console.WriteLine();
                System.Threading.Thread.Sleep(2000);
            }
            //pk结束
            Console.WriteLine("**********战斗结束**********");
            Console.WriteLine();
            if (p1[0].Blood <= 0 && p2[0].Blood > 0)
            {
                Console.WriteLine(p2[0].Name + "赢了,恭喜升了一级!");
                Console.WriteLine("属性提升为:");
                if (da.Update(Name_2))
                {
                    List<Player> list = da.Select(Name_2);
                    foreach (Player data in list)
                    {
                        string ssex = data.Sex ? "" : "";
                        Console.WriteLine("姓名:" + data.Name + "\t" + "性别:" + ssex + "\t" + "等级:" + data.Lev + "\t" + "血量:" + data.Blood + "\t" + "攻击:" + data.Attack + "\t" + "防御:" + data.Defense + "\t" + "命中:" + data.Hit + "\t" + "闪避:" + data.Dodge + "\t");
                    }
                }
                else
                {
                    Console.WriteLine("错误!");
                }
            }
            else if (p2[0].Blood <= 0 && p1[0].Blood > 0)
            {
                Console.WriteLine(p1[0].Name + "赢了,恭喜升了一级!");
                Console.WriteLine("属性提升为:");
                if (da.Update(Name_1))
                {
                    List<Player> list = da.Select(Name_1);
                    foreach (Player data in list)
                    {
                        string ssex = data.Sex ? "" : "";
                        Console.WriteLine("姓名:" + data.Name + "\t" + "性别:" + ssex + "\t" + "等级:" + data.Lev + "\t" + "血量:" + data.Blood + "\t" + "攻击:" + data.Attack + "\t" + "防御:" + data.Defense + "\t" + "命中:" + data.Hit + "\t" + "闪避:" + data.Dodge + "\t");
                    }
                }
                else
                {
                    Console.WriteLine("错误!");
                }
            }
            else if (p1[0].Blood<=0&&p2[0].Blood<=0)
            {
                Console.WriteLine("同归于尽!");
            }
            Console.ReadLine();
        }
    }
}
View Code

 

标签:

版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有

上一篇:Ado.net[登录,增删改查,Get传值,全选,不选,批量删除,批量更

下一篇:C#窗体计算器