数据连接类
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data.SqlClient;
namespace ConsoleApplication1
{
class dzconnect
{
private static string connstring = System.Configuration.ConfigurationSettings.AppSettings["connstring"];
public static SqlConnection conn
{
get
{
return new SqlConnection(connstring);
}
}
}
}
实体类
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApplication1
{
public class dzb
{
private int ids;
public int Ids
{
get { return ids; }
set { ids = value; }
}
private string name;
public string Name
{
get { return name; }
set { name = value; }
}
private int gongji;
public int Gongji
{
get { return gongji; }
set { gongji = value; }
}
private int fangyu;
public int Fangyu
{
get { return fangyu; }
set { fangyu = value; }
}
private int mingzhong;
public int Mingzhong
{
get { return mingzhong; }
set { mingzhong = value; }
}
private int shanbi;
public int Shanbi
{
get { return shanbi; }
set { shanbi = value; }
}
private int shengming;
public int Shengming
{
get { return shengming; }
set { shengming = value; }
}
private int dengji;
public int Dengji
{
get { return dengji; }
set { dengji = value; }
}
}
}
数据访问类
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data.SqlClient;
namespace ConsoleApplication1
{
class dzbDA
{
private SqlConnection _conn;
private SqlCommand _cmd;
private SqlDataReader _dr;
public dzbDA()
{
_conn = dzconnect.conn;
_cmd = _conn.CreateCommand();
}
public bool add(string name)
{
int seed = (int)Convert.ToChar(name.Substring(0, 1)) + (int)Convert.ToChar(name.Substring(1, 1));
Random rand = new Random(seed);
int shengming = 5000 + rand.Next(5000);
int gongji = 500 + rand.Next(500);
int fangyu = 500 + rand.Next(500);
int mingzhong = 50 + rand.Next(50);
int shanbi = 50 + rand.Next(50);
_cmd.CommandText = "insert into dzb values(@name,@gongji,@fangyu,@mingzhong,@shanbi,@shengming)";
_cmd.Parameters.Clear();
_cmd.Parameters.AddWithValue("@name", name);
_cmd.Parameters.AddWithValue("@gongji", gongji);
_cmd.Parameters.AddWithValue("@fangyu", fangyu);
_cmd.Parameters.AddWithValue("@mingzhong", mingzhong);
_cmd.Parameters.AddWithValue("@shanbi", shanbi);
_cmd.Parameters.AddWithValue("@shengming", shengming);
_conn.Open();
int n = _cmd.ExecuteNonQuery();
if (n > 0)
{
return true;
}
else
{
return false;
}
}
public List<dzb> select(string name)
{
_cmd.CommandText = "select * from dzb where name=@name";
_cmd.Parameters.Clear();
_cmd.Parameters.AddWithValue("@name", name);
_conn.Open();
_dr = _cmd.ExecuteReader();
List<dzb> list = new List<dzb>();
if (_dr.HasRows)
{
while (_dr.Read())
{
dzb data = new dzb();
data.Name = _dr[1].ToString();
data.Gongji =Convert.ToInt32 (_dr[2]);
data.Fangyu = Convert.ToInt32(_dr[3]);
data.Shanbi = Convert.ToInt32(_dr[4]);
data.Mingzhong = Convert.ToInt32(_dr[5]);
data.Shengming = Convert.ToInt32(_dr[6]);
list.Add(data);
}
}
_conn.Close();
return list;
}
}
}
具体操作表
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApplication1
{
class Program
{
static dzb pk(dzb a1, dzb a2)
{
Console.WriteLine(a1.Name + "攻击" + a2.Name);
System.Threading.Thread.Sleep(3000);
Random rand = new Random();
int gongji1 = a1.Gongji + rand.Next(500);
int mingzhong1 = a1.Mingzhong + rand.Next(50);
int fangyu2 = a2.Fangyu + rand.Next(100);
int shanbi2 = a2.Shanbi + rand.Next(10);
if (mingzhong1 - shanbi2 > 0)
{
if (gongji1 - fangyu2 > 0)
{
a2.Shengming = a2.Shengming - (gongji1 - fangyu2)*5;
Console.WriteLine(a1.Name + "打掉" +a2.Name + (gongji1 - fangyu2)*5 + "点血");
}
if (gongji1 - fangyu2 < 0)
{
Console.WriteLine("未破防");
}
}
else
{
Console.WriteLine("未命中");
}
return a2;
}
static void Main(string[] args)
{
Console.WriteLine("输入1添加,输入2战斗");
string a = Console.ReadLine();
if (a == "1")
{
Console.WriteLine("请输入玩家:");
string name = Console.ReadLine();
dzbDA da = new dzbDA();
if (da.add(name))
{
Console.WriteLine("添加成功!");
}
else
{
Console.WriteLine("添加失败!");
}
}
if (a == "2")
{
dzbDA da = new dzbDA();
Console.WriteLine("请输入玩家1:");
string name1 = Console.ReadLine();
Console.WriteLine("请输入玩家2:");
string name2 = Console.ReadLine();
List<dzb> list1 = da.select(name1);
foreach (dzb data in list1)
{
Console.WriteLine(data.Name + "__" + data.Gongji + "__" + data.Fangyu + "__" + data.Shanbi + "__" + data.Mingzhong + "__" + data.Shengming);
}
List<dzb> list2 = da.select(name2);
foreach (dzb data in list2)
{
Console.WriteLine(data.Name + "__" + data.Gongji + "__" + data.Fangyu + "__" + data.Shanbi + "__" + data.Mingzhong + "__" + data.Shengming);
}
while (list1[0].Shengming> 0 &&list2[0].Shengming> 0)
{
dzb pk2 = pk(list1[0], list2[0]);
list2[0] = pk2;
dzb pk1 = pk(list2[0], list1[0]);
list1[0] = pk1;
Console.WriteLine(list2[0].Name + ":" + (list2[0].Shengming < 0 ? 0 : list2[0].Shengming) + "----------" + list1[0].Name + ":" + (list1[0].Shengming < 0 ? 0 : list1[0].Shengming));
Console.WriteLine();
}
Console.WriteLine("战斗结束");
if (list1[0].Shengming > 0 && list2[0].Shengming <= 0)
{
Console.WriteLine(list1[0].Name+"赢了");
}
else if (list1[0].Shengming <= 0 && list2[0].Shengming > 0)
{
Console.WriteLine(list2[0].Name + "赢了");
}
else if (list1[0].Shengming <= 0 && list2[0].Shengming <= 0)
{
Console.WriteLine("同归于尽");
}
}
Console.ReadLine();
}
}
}