标签:
扩展属性
有外键关系时将信息处理成用户可看懂的
利用扩展属性
如:Info表中的民族列显示的是民族代号处理成Nation表中的民族名称
需要在Info类里面扩展一个显示nation名称的属性
例:先前在NationDA里面已经下好了一个代号换成名称的方法
public class Info { //扩展的名族名称属性 public string NationName { get { NationDA da = new NationDA(); return da.NationName(this.nation);//this关键字,该对象里面是不是有一个nation } }
配置文件(App.config)
换台电脑、数据库名不同时,为了让别的用户也可以运行,把数据连接写在配置文件中,方便修改
例:
App.config中:
<?xml version="1.0" encoding="utf-8" ?> <configuration> <startup> <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" /> </startup>
//key 配置名称 <appSettings> <add key="connstring" value="server=.;database=mydb;user=sa;pwd=123"/> </appSettings> </configuration>
数据连接类中:
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 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; } } } }
数据访问类:
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; } } } }
添加人物:
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(); } } }
对战:
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(); } } }
标签:
原文地址:http://www.cnblogs.com/ShenG1/p/5790604.html