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

对战游戏

要求:

  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; }
}
}
}

数据访问类:

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(, )) + (int)Convert.ToChar(name_p.Substring(, ));
Random rand = new Random(seed);
p.Lev = ;
p.Blood = + rand.Next();
p.Attack = + rand.Next();
p.Defense = + rand.Next();
p.Hit = + rand.Next();
p.Dodge = + rand.Next();
//添加
_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 > )
{
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[].ToString();
data.Sex =Convert.ToBoolean(_dr[]);
data.Lev = Convert.ToInt32(_dr[]);
data.Blood = Convert.ToInt32(_dr[]);
data.Attack = Convert.ToInt32(_dr[]);
data.Defense = Convert.ToInt32(_dr[]);
data.Hit = Convert.ToInt32(_dr[]);
data.Dodge = Convert.ToInt32(_dr[]);
list.Add(data);
}
}
_conn.Close();
return list;
} //修改
public bool Update(string name)
{
List<Player> list = Select(name);
list[].Lev += ;
list[].Blood += ;
list[].Attack += ;
list[].Defense += ;
list[].Hit += ;
list[].Dodge += ;
_cmd.CommandText = "update Player set Lev=" + list[].Lev + ",Blood=" + list[].Blood + ",Attack=" + list[].Attack + ",Defense=" + list[].Defense + ",Hit=" + list[].Hit + ",Dodge=" + list[].Dodge + " where Name=@name";
_cmd.Parameters.Clear();
_cmd.Parameters.AddWithValue("@name", name);
_conn.Open();
int n = _cmd.ExecuteNonQuery();
_conn.Close();
if (n > )
{
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();
int h2 = rand.Next() + p_1.Hit;
int d1 = rand.Next() + p_2.Dodge;
if (h2 > )
{
if (d1 > )
{
Console.ForegroundColor = ConsoleColor.Green;
Console.WriteLine(p_2.Name + "躲开了" + p_1.Name + "的攻击");
Console.ForegroundColor = ConsoleColor.Black;
}
else
{
int sh = rand.Next() + 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[].Blood > && p2[].Blood > )
{
Player pp2 = gj(p1[],p2[]);
p2[] = pp2;
Player pp1 = gj(p2[],p1[]);
p1[] = pp1;
Console.WriteLine(p2[].Name + ":"+(p2[].Blood<?:p2[].Blood)+"————————"+p1[].Name+":"+(p1[].Blood<?:p1[].Blood));
Console.WriteLine();
Console.WriteLine();
System.Threading.Thread.Sleep();
}
//pk结束
Console.WriteLine("**********战斗结束**********");
Console.WriteLine();
if (p1[].Blood <= && p2[].Blood > )
{
Console.WriteLine(p2[].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[].Blood <= && p1[].Blood > )
{
Console.WriteLine(p1[].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[].Blood<=&&p2[].Blood<=)
{
Console.WriteLine("同归于尽!");
}
Console.ReadLine();
}
}
}
上一篇:【AC自动机】洛谷三道模板题


下一篇:free store VS heap(*存储区VS堆)