操作数据库(对战小游戏)

创建数据库

 1 create database duizhan
 2 go
 3 use duizhan
 4 go
 5 create table duizhan
 6 (
 7    Code varchar(20) not null primary key,
 8    Name varchar(20) not null,
 9    Sex varchar(20) not null,
10    Blood int,
11    Attack int,
12    Defence int,
13    Mingzhong int,
14    Shanbi int, 
15    Speed int,
16    Experience int,
17    Lv int,
18 )

DBconnect.cs

 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Text;
 5 using System.Threading.Tasks;
 6 using System.Data.SqlClient;
 7 
 8 namespace 对战
 9 {
10     public class DBconnect
11     {
12         private static string connstring = "server=.;database=duizhan;user=sa;pwd=diushiDEwutong0";
13 
14         public static SqlConnection conn
15         {
16             get
17             {
18                 return new SqlConnection(connstring);
19             }
20         }
21 
22     }
23 }

duizhan.cs

  1 using System;
  2 using System.Collections.Generic;
  3 using System.Linq;
  4 using System.Text;
  5 using System.Threading.Tasks;
  6 
  7 namespace 对战
  8 {
  9     public class duizhan
 10     {
 11         private string code;
 12 
 13         public string Code
 14         {
 15             get { return code; }
 16             set { code = value; }
 17         }
 18 
 19         private string name;
 20 
 21         public string Name
 22         {
 23             get { return name; }
 24             set { name = value; }
 25         }
 26 
 27         private string sex;
 28 
 29         public string Sex
 30         {
 31             get { return sex; }
 32             set { sex = value; }
 33         }
 34 
 35         private int blood;
 36 
 37         public int Blood
 38         {
 39             get { return blood; }
 40             set { blood = value; }
 41         }
 42 
 43         private int attack;
 44 
 45         public int Attack
 46         {
 47             get { return attack; }
 48             set { attack = value; }
 49         }
 50 
 51         private int defence;
 52 
 53         public int Defence
 54         {
 55             get { return defence; }
 56             set { defence = value; }
 57         }
 58 
 59         private int mingzhong;
 60 
 61         public int Mingzhong
 62         {
 63             get { return mingzhong; }
 64             set { mingzhong = value; }
 65         }
 66 
 67         private int shanbi;
 68 
 69         public int Shanbi
 70         {
 71             get { return shanbi; }
 72             set { shanbi = value; }
 73         }
 74 
 75         private int speed;
 76 
 77         public int Speed
 78         {
 79             get { return speed; }
 80             set { speed = value; }
 81         }
 82 
 83         private int experience;
 84 
 85         public int Experience
 86         {
 87             get { return experience; }
 88             set { experience = value; }
 89         }
 90 
 91         private int lv;
 92 
 93         public int Lv
 94         {
 95             get { return lv; }
 96             set { lv = value; }
 97         }
 98 
 99     }
100 }

duizhanDA.cs

  1 using System;
  2 using System.Collections.Generic;
  3 using System.Linq;
  4 using System.Text;
  5 using System.Threading.Tasks;
  6 using System.Data.SqlClient;
  7 
  8 namespace 对战
  9 {
 10     public class duizhanDA
 11     {
 12         private SqlConnection _conn;
 13         private SqlCommand _cmd;
 14         private SqlDataReader _dr;
 15 
 16         //用构造函数来初始化连接对象,命令对象
 17         public duizhanDA()
 18         {
 19             _conn = DBconnect.conn;
 20             _cmd = _conn.CreateCommand();
 21         }
 22 
 23         //添加数据
 24 
 25         public bool Add(string code, string name, string sex)
 26         {
 27             int seed = (int)Convert.ToChar(name.Substring(0, 1)) + (int)Convert.ToChar(name.Substring(1, 1));
 28             Random rand = new Random(seed);
 29             //Random rand = new Random();
 30 
 31             int blood = 3000 + rand.Next(3000);
 32             int attack = 600 + rand.Next(400);
 33             int defence = 10 + rand.Next(90);
 34             int minzhong = rand.Next(50) + 50;
 35             int shanbi = rand.Next(50) + 10;
 36             int speed = 100 + rand.Next(50);
 37             int exprience = 0;
 38             int lv = 1;
 39 
 40             _cmd.CommandText = "insert into duizhan values(@code,@name,@sex,@blood,@attack,@defence,@minzhong,@shanbi,@speed,@experience,@lv)";
 41             _cmd.Parameters.Clear();
 42             _cmd.Parameters.AddWithValue("@code", code);
 43             _cmd.Parameters.AddWithValue("@name", name);
 44             _cmd.Parameters.AddWithValue("@sex", sex);
 45             _cmd.Parameters.AddWithValue("@blood", blood);
 46             _cmd.Parameters.AddWithValue("@attack", attack);
 47             _cmd.Parameters.AddWithValue("@defence", defence);
 48             _cmd.Parameters.AddWithValue("@minzhong", minzhong);
 49             _cmd.Parameters.AddWithValue("@shanbi", shanbi);
 50             _cmd.Parameters.AddWithValue("@speed", speed);
 51             _cmd.Parameters.AddWithValue("@experience", exprience);
 52             _cmd.Parameters.AddWithValue("@lv", lv);
 53 
 54             _conn.Open();
 55             int n = _cmd.ExecuteNonQuery();
 56             _conn.Close();
 57 
 58             if (n > 0)
 59             {
 60                 return true;
 61             }
 62             else
 63             {
 64                 return false;
 65             }
 66         }
 67 
 68         //查询所有数据
 69         public List<duizhan> Select()
 70         {
 71             _cmd.CommandText = "select * from duizhan";
 72             _conn.Open();
 73             _dr = _cmd.ExecuteReader();
 74 
 75 
 76             //定义一个空的集合
 77             List<duizhan> list = new List<duizhan>();
 78 
 79             if (_dr.HasRows)
 80             {
 81                 while (_dr.Read())
 82                 {
 83                     //造一个duizhan对象
 84                     duizhan data = new duizhan();
 85 
 86                     data.Code = _dr[0].ToString();
 87                     data.Name = _dr[1].ToString();
 88                     data.Sex = _dr[2].ToString();
 89                     data.Blood = (int)_dr[3];
 90                     data.Attack = (int)_dr[4];
 91                     data.Defence = (int)_dr[5];
 92                     data.Mingzhong = (int)_dr[6];
 93                     data.Shanbi = (int)_dr[7];
 94                     data.Speed = (int)_dr[8];
 95                     data.Experience = (int)_dr[9];
 96                     data.Lv = (int)_dr[10];
 97 
 98                     //扔到集合里面
 99                     list.Add(data);
100                 }
101             }
102             _conn.Close();
103             return list;
104         }
105 
106 
107 
108         //根据姓名查询
109         public List<duizhan> Select(string name)
110         {
111             _conn.Open();
112             _cmd.CommandText = "select * from duizhan where name=@name";
113             _cmd.Parameters.Clear();
114             _cmd.Parameters.AddWithValue("@name", name);
115 
116             _dr = _cmd.ExecuteReader();
117 
118 
119             List<duizhan> list = new List<duizhan>();
120 
121             if (_dr.HasRows)
122             {
123                 while (_dr.Read())
124                 {
125                     //造一个duizhan对象
126                     duizhan data = new duizhan();
127 
128                     data.Code = _dr[0].ToString();
129                     data.Name = _dr[1].ToString();
130                     data.Sex = _dr[2].ToString();
131                     data.Blood = (int)_dr[3];
132                     data.Attack = (int)_dr[4];
133                     data.Defence = (int)_dr[5];
134                     data.Mingzhong = (int)_dr[6];
135                     data.Shanbi = (int)_dr[7];
136                     data.Speed = (int)_dr[8];
137                     data.Experience = (int)_dr[9];
138                     data.Lv = (int)_dr[10];
139 
140                     //扔到集合里面
141                     list.Add(data);
142                 }
143             }
144             _conn.Close();
145             return list;
146         }
147 
148         //更改经验数据
149         public bool update(int experience, string name)
150         {
151             _cmd.CommandText = "update duizhan set experience ='" + experience + "' where name=@name";
152             _cmd.Parameters.Clear();
153             _cmd.Parameters.AddWithValue("@name", name);
154             _conn.Open();
155             int n = _cmd.ExecuteNonQuery();
156             _conn.Close();
157 
158             if (n > 0)
159             {
160                 return true;
161             }
162             else
163             {
164                 return false;
165             }
166         }
167 
168         //升级
169         public bool update(string name)
170         {
171             List<duizhan> list = Select(name);
172             
173             list[0].Blood += 300;
174             list[0].Attack += 50;
175             list[0].Defence += 10;
176             list[0].Lv += 1;
177             _cmd.CommandText = "update duizhan set  Blood=" + list[0].Blood + ",Attack=" + list[0].Attack + ",Defence=" + list[0].Defence  + ",lv=" + list[0].Lv+ " where Name=@name";
178             _cmd.Parameters.Clear();
179             _cmd.Parameters.AddWithValue("@name", name);
180             _conn.Open();
181             int n = _cmd.ExecuteNonQuery();
182             _conn.Close();
183             if (n > 0)
184             {
185                 return true;
186             }
187             else
188             {
189                 return false;
190             }
191         }
192 
193         //删除程序
194         public bool delete(string name)
195         {
196             _cmd.CommandText = "delete  from duizhan where name=@name";
197             _cmd.Parameters.Clear();
198             _cmd.Parameters.AddWithValue("@name", name);
199             _conn.Open();
200             int n = _cmd.ExecuteNonQuery();
201             _conn.Close();
202 
203             if (n > 0)
204             {
205                 return true;
206             }
207             else
208             {
209                 return false;
210             }
211 
212         }
213 
214         //对战
215         public List<duizhan> PK(List<duizhan> list1, List<duizhan> list2)
216         {
217 
218             int s1 = list1[0].Speed;
219             int s2 = list2[0].Speed;
220             while (list1[0].Blood > 0 && list2[0].Blood > 0)
221             {
222                 Random mz = new Random();
223                 while (s1 != 0 && s2 != 0)
224                 {
225                     s1--;
226                     s2--;
227                    
228                 }
229                 Console.ForegroundColor = ConsoleColor.Blue;
230                 Console.WriteLine(list1[0].Name + "速度值:" + s1 +"---"+ list2[0].Name + "速度值:" + s2);
231                 Console.ForegroundColor = ConsoleColor.Black;
232                 if (s1 == 0)
233                 {
234                     Console.WriteLine(list1[0].Name + "攻击");
235                     if (mz.Next(101) < list1[0].Mingzhong)
236                     {
237                         Random sb = new Random();
238                         if (sb.Next(101) < list2[0].Shanbi)
239                         {
240                             Console.ForegroundColor = ConsoleColor.Green;
241                             Console.WriteLine(list1[0].Name + "未命中");
242                             Console.ForegroundColor = ConsoleColor.Black;
243 
244                         }
245                         else
246                         {
247                             Console.ForegroundColor = ConsoleColor.Red;
248                             list2[0].Blood = list2[0].Blood - list1[0].Attack * (1-(list2[0].Defence / 300));
249                             Console.WriteLine(list1[0].Name + "输出" + (list1[0].Attack * (1 - list2[0].Defence / 300)) + "伤害");
250                             Console.ForegroundColor = ConsoleColor.Black;
251                         }
252                     }
253                     else
254                     {
255                         Console.ForegroundColor = ConsoleColor.Green;
256                         Console.WriteLine(list1[0].Name + "未命中");
257                         Console.ForegroundColor = ConsoleColor.Black;
258                     }
259                     Console.ForegroundColor = ConsoleColor.Red;
260                     Console.WriteLine(list2[0].Name + "血量:" + (list2[0].Blood < 0 ? 0 : list2[0].Blood) + "———" + list1[0].Name + "血量:" + (list1[0].Blood < 0 ? 0 : list1[0].Blood));
261                     Console.ForegroundColor = ConsoleColor.Black;
262                     s1 = list1[0].Speed;
263                 }
264                 else if (s2 == 0)
265                 {
266                     Console.WriteLine(list2[0].Name + "攻击");
267                     if (mz.Next(101) < list2[0].Mingzhong)
268                     {
269                         Random sb = new Random();
270                         if (sb.Next(101) < list1[0].Shanbi)
271                         {
272                             Console.ForegroundColor = ConsoleColor.Green;
273                             Console.WriteLine(list2[0].Name + "未命中");
274                             Console.ForegroundColor = ConsoleColor.Black;
275                         }
276                         else
277                         {
278                             Console.ForegroundColor = ConsoleColor.Red;
279                             list1[0].Blood = list1[0].Blood - (list2[0].Attack * (1-(list1[0].Defence / 300)));
280                             Console.WriteLine(list2[0].Name + "输出" + (list2[0].Attack * (1 - (list1[0].Defence / 300))) + "伤害");
281                             Console.ForegroundColor = ConsoleColor.Black;
282                         }
283                     }
284                     else
285                     {
286                         Console.ForegroundColor = ConsoleColor.Green;
287                         Console.WriteLine(list2[0].Name + "未命中");
288                         Console.ForegroundColor = ConsoleColor.Black;
289                     }
290                     Console.ForegroundColor = ConsoleColor.Red;
291                     Console.WriteLine(list2[0].Name + "血量:" + (list2[0].Blood < 0 ? 0 : list2[0].Blood) + "———" + list1[0].Name + "血量:" + (list1[0].Blood < 0 ? 0 : list1[0].Blood));
292                     Console.ForegroundColor = ConsoleColor.Black;
293                     s2 = list2[0].Speed;
294                 }
295                 System.Threading.Thread.Sleep(1000);
296                 Console.WriteLine();
297 
298             }
299             if (list1[0].Blood < 0)
300             {
301                 List<duizhan> fanhui = list2;
302                 return fanhui;
303             }
304             else
305             {
306                 List<duizhan> fanhui = list1;
307                 return fanhui;
308             }
309 
310 
311 
312 
313 
314 
315 
316         }
317 
318 
319 
320 
321     }
322 
323 }

主函数

  1 using System;
  2 using System.Collections.Generic;
  3 using System.Linq;
  4 using System.Text;
  5 using System.Threading.Tasks;
  6 using System.Data.SqlClient;
  7 
  8 namespace 对战
  9 {
 10     class Program
 11     {
 12         
 13 
 14         static void Main(string[] args)
 15         {
 16             Console.BackgroundColor = ConsoleColor.White;
 17             Console.ForegroundColor = ConsoleColor.Black;
 18             string[] AR = new string[] { "code", "姓名", "性别", "血量", "攻击", "防御", "命中", "闪避", "速度", "经验", "等级" };
 19 
 20             Console.WriteLine("添加还是查询?添加输入1,查询输入2,战斗输入3");
 21             string s = Console.ReadLine();
 22             duizhanDA da = new duizhanDA();
 23             string code = "";
 24             string name = "";
 25             if (s == "1")
 26             {
 27                 //添加数据
 28                 while (true)
 29                 {
 30                     Console.WriteLine("添加数据程序");
 31                     Console.WriteLine("请输入您要添加的成员代号");
 32                     code = Console.ReadLine();
 33                     Console.WriteLine("请输入您要添加的成员姓名");
 34                     name = Console.ReadLine();
 35                     Console.WriteLine("请输入您要添加的成员性别");
 36                     string sex = Console.ReadLine();
 37                     if (da.Add(code, name, sex))
 38                     {
 39                         Console.WriteLine("添加成功");
 40                     }
 41                     else
 42                     {
 43                         Console.WriteLine("添加失败");
 44                     }
 45                     Console.WriteLine("是否继续添加,继续请输入1,跳出输入任意键");
 46                     string a = Console.ReadLine();
 47                     if (a == "1")
 48                     {
 49                         Console.WriteLine("继续输入");
 50                     }
 51                     else
 52                     {
 53                         Console.WriteLine("程序跳出");
 54                         break;
 55                     }
 56                 }
 57             }
 58             else if (s == "2")
 59             {
 60                 Console.WriteLine("输出属性");
 61 
 62                 Console.WriteLine("可查询的人员名单:");
 63                 List<duizhan> List = new List<duizhan>();
 64                 while (true)
 65                 {
 66                     List = da.Select();
 67                     if (List.Count() != 0)
 68                     {
 69 
 70                         int i = 0;
 71 
 72                         while (i < List.Count())
 73                         {
 74                             Console.Write(List[i].Name + "	");
 75                             i++;
 76                         }
 77 
 78                         Console.Write("
");
 79                         break;
 80                     }
 81                     else
 82                     {
 83                         Console.WriteLine("输入错误,请重新输入");
 84                         code = Console.ReadLine();
 85                     }
 86 
 87                 }
 88 
 89                 Console.WriteLine("请输入要查询的姓名");
 90                 name = Console.ReadLine();
 91 
 92      
 93                 while (true)
 94                 {
 95                     List = da.Select(name);
 96                     if (List.Count() != 0)
 97                     {
 98 
 99                         int i = 0;
100                         while (i < 11)
101                         {
102                             Console.Write(AR[i] + "	");
103                             i++;
104                         }
105                         Console.Write("
");
106 
107                         Console.Write(List[0].Code + "	");
108                         Console.Write(List[0].Name + "	");
109                         //Console.Write(((List[0].Sex == "True") ? "男" : "女") + "	");    
110                         Console.Write(List[0].Sex + "	");
111                         Console.Write(List[0].Blood + "	");
112                         Console.Write(List[0].Attack + "	");
113                         Console.Write(List[0].Defence + "	");
114                         Console.Write(List[0].Mingzhong + "	");
115                         Console.Write(List[0].Shanbi + "	");
116                         Console.Write(List[0].Speed + "	");
117                         Console.Write(List[0].Experience + "	");
118                         Console.Write(List[0].Lv + "	");
119 
120                         Console.Write("
");
121                         break;
122                     }
123                     else
124                     {
125                         Console.WriteLine("输入错误,请重新输入");
126                         code = Console.ReadLine();
127                     }
128 
129                 }
130             }
131             else if (s == "3")
132             {
133                 Console.WriteLine("战斗序列");
134                 Console.WriteLine("可以选择的对战人员为:");
135                 List<duizhan> List = new List<duizhan>();
136                 while (true)
137                 {
138                     List = da.Select();
139                     if (List.Count() != 0)
140                     {
141 
142                         int i = 0;
143 
144                         while (i < List.Count())
145                         {
146                             Console.Write(List[i].Name + "	");
147                             Console.Write("等级"+List[i].Lv + "	");
148                             i++;
149                         }
150 
151                         Console.Write("
");
152                         break;
153                     }
154                     else
155                     {
156                         Console.WriteLine("输入错误,请重新输入");
157                         code = Console.ReadLine();
158                     }
159 
160                 }
161 
162                 Console.WriteLine("请输入参与战斗的人员");
163                 Console.WriteLine("请输入第一位参加战斗的人员姓名");
164                 string name1 = Console.ReadLine();
165                 Console.WriteLine("请输入第二位参加战斗的人员姓名");
166                 string name2 = Console.ReadLine();
167                 List<duizhan> List1 = da.Select(name1);
168                 List<duizhan> List2 = da.Select(name2);
169 
170 
171                 List<duizhan> jieguo = da.PK(List1, List2);
172                 Console.WriteLine(jieguo[0].Name + "胜利");
173                 int experience = 0;
174                 if (jieguo[0].Name == List1[0].Name)
175                 {
176                     experience = 50 * List2[0].Lv + List1[0].Experience;
177                     da.update(experience, List1[0].Name);
178                     Console.WriteLine(jieguo[0].Name+"获得经验"+experience);
179                 }
180                 else
181                 {
182                     experience = 50 * List1[0].Lv + List2[0].Experience;
183                     da.update(experience, List2[0].Name);
184                     Console.WriteLine(jieguo[0].Name + "获得经验" + experience);
185                 }
186                 //升级需要经验50,100,200,400,800,1600,3200,6400,12800
187                 int[] lvexp = new int[] {0, 50,100,200,400,800,1600,3200,6400,12800};
188                 List<duizhan> uplv = da.Select(jieguo[0].Name);
189                 int lv = uplv[0].Lv;
190                 while (true)
191                 {
192                     if (lvexp[lv] <= uplv[0].Experience)
193                     {
194                         Console.WriteLine("升级了");
195                         da.update(uplv[0].Name);
196                         Console.WriteLine("属性改变为:");
197                         while (true)
198                         {
199                             List = da.Select(uplv[0].Name);
200                             if (List.Count() != 0)
201                             {
202 
203                                 int i = 0;
204                                 while (i < 11)
205                                 {
206                                     Console.Write(AR[i] + "	");
207                                     i++;
208                                 }
209                                 Console.Write("
");
210                                 Console.Write(List[0].Code + "	");
211                                 Console.Write(List[0].Name + "	");
212                                 //Console.Write(((List[0].Sex == "True") ? "男" : "女") + "	");    
213                                 Console.Write(List[0].Sex + "	");
214                                 Console.Write(List[0].Blood + "	");
215                                 Console.Write(List[0].Attack + "	");
216                                 Console.Write(List[0].Defence + "	");
217                                 Console.Write(List[0].Mingzhong + "	");
218                                 Console.Write(List[0].Shanbi + "	");
219                                 Console.Write(List[0].Speed + "	");
220                                 Console.Write(List[0].Experience + "	");
221                                 Console.Write(List[0].Lv + "	");
222                                 Console.Write("
");
223                                 break;
224                             }                           
225 
226                         }
227                         lv++;
228                     }
229                     else
230                     {
231                         break;
232                     }
233                 }
234 
235             }
236             else
237             {
238                 Console.WriteLine("删除数据");
239             }
240 
241 
242 
243             Console.ReadLine();
244 
245         }
246     }
247 }
原文地址:https://www.cnblogs.com/bloodPhoenix/p/5787659.html