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


    数据连接类
    
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.Data.SqlClient;
    
    namespace 对战
    {
    public class DZConnect
    {
    private static string connstring = "server=.;database=duizhan;user=sa;pwd=123";
    
    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 对战
    {
    public class DZ
    {
    private int code;
    
     
    
    public int Code
    {
    get { return code; }
    set { code = value; }
    }
    
     
    
    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 xue;
    
     
    
    public int Xue
    {
    get { return xue; }
    set { xue = value; }
    }
    private int gong;
    
     
    
    public int Gong
    {
    get { return gong; }
    set { gong = value; }
    }
    private int fang;
    
     
    
    public int Fang
    {
    get { return fang; }
    set { fang = value; }
    }
    private int zhong;
    
     
    
    public int Zhong
    {
    get { return zhong; }
    set { zhong = value; }
    }
    private int shan;
    
     
    
    public int Shan
    {
    get { return shan; }
    set { shan = value; }
    }
     
    
    }
    
     
    
    }
    
    数据访问类
    
     
    
     
    
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.Data.SqlClient;
    namespace 对战
    {
    class DZDA
    {
    private SqlConnection _conn;
    private SqlCommand _cmd;
    private SqlDataReader _dr;
    
    
    public DZDA()
    {
    _conn = DZConnect.Conn;
    _cmd = _conn.CreateCommand();
    }
    
    public bool Add(string code, string name)
    {
    
    int seed = (int)Convert.ToChar(name.Substring(0, 1)) + (int)Convert.ToChar(name.Substring(0, 1));
    Random rand = new Random();
    bool sex = true;
    int xue = 800 + rand.Next(100);
    int gong = 300 + rand.Next(100);
    int fang = 10 + rand.Next(20);
    int zhong = rand.Next(80) + 50;
    int shan = rand.Next(80) + 10;
    _cmd.CommandText = "insert into DZ values(@code,@name,@sex,@xue,@gong,@fang,@zhong,@shan)";
    _cmd.Parameters.Clear();
    _cmd.Parameters.AddWithValue("@code", code);
    _cmd.Parameters.AddWithValue("@name", name);
    _cmd.Parameters.AddWithValue("@sex", sex);
    _cmd.Parameters.AddWithValue("@xue", xue);
    _cmd.Parameters.AddWithValue("@gong", gong);
    _cmd.Parameters.AddWithValue("@fang", fang);
    _cmd.Parameters.AddWithValue("@zhong", zhong);
    _cmd.Parameters.AddWithValue("@shan", shan);
    
    _conn.Open();
    int n = _cmd.ExecuteNonQuery();
    _conn.Close();
    if (n > 0)
    {
    return true;
    }
    else
    {
    return false;
    }
    }
    public List<DZ> Select()
    {
    _cmd.CommandText = "select * from DZ";
    _conn.Open();
    _dr = _cmd.ExecuteReader();
    
    List<DZ> list = new List<DZ>();
    
    if (_dr.HasRows)
    {
    while (_dr.Read())
    {
    DZ data = new DZ();
    data.Code = (int)_dr[0];
    data.Name = _dr[1].ToString();
    data.Sex = Convert.ToBoolean(_dr[2]);
    data.Xue = (int)_dr[3];
    data.Gong = (int)_dr[4];
    data.Fang = (int)_dr[5];
    data.Zhong = (int)_dr[6];
    data.Shan = (int)_dr[7];
    
    list.Add(data);
    }
    
    }
    _conn.Close();
    
    return list;
    
    }
    
    public List<DZ> Select( string name)
    {
    _cmd.CommandText = "select * from DZ where Name=@name";
    _cmd.Parameters.Clear();
    _cmd.Parameters.AddWithValue("@name", name);
    _conn.Open();
    _dr = _cmd.ExecuteReader();
    
    List<DZ> list = new List<DZ>();
    
    if (_dr.HasRows)
    {
    while (_dr.Read())
    {
    DZ data = new DZ();
    data.Code = (int)_dr[0];
    data.Name = _dr[1].ToString();
    data.Sex = Convert.ToBoolean(_dr[2]);
    data.Xue = (int)_dr[3];
    data.Gong = (int)_dr[4];
    data.Fang =(int)_dr[5];
    data.Zhong = (int)_dr[6];
    data.Shan = (int)_dr[7];
    
    list.Add(data);
    }
    
    }
    _conn.Close();
    
    return list;
    
    }
    
    }
    }
    
    主函数
    
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.Data.SqlClient;
    
    namespace 对战
    {
    class Program
    {
    static DZ at(DZ d_1, DZ d_2)
    {
    Random rand = new Random();
    Console.ForegroundColor = ConsoleColor.Gray;
    Console.WriteLine(d_1.Name + "准备发起攻击····");
    Console.ForegroundColor = ConsoleColor.Black;
    
    System.Threading.Thread.Sleep(1000);
    int m2 = rand.Next(100) + d_1.Zhong;
    int s1 = rand.Next(100) + d_2.Shan;
    if (m2 > 100)
    {
    if (s1 > 150)
    {
    Console.ForegroundColor = ConsoleColor.Green;
    Console.WriteLine(d_2.Name + "躲开了" + d_1.Name + "的攻击");
    Console.ForegroundColor = ConsoleColor.Black;
    }
    else
    {
    int a = rand.Next(200) + d_1.Gong;
    d_2.Xue = d_2.Xue - a + d_2.Fang;
    Console.ForegroundColor = ConsoleColor.Red;
    Console.WriteLine(d_1.Name + "攻击掉" + d_2.Name + (a - d_2.Fang) + "");
    Console.ForegroundColor = ConsoleColor.Black;
    }
    }
    else
    {
    Console.ForegroundColor = ConsoleColor.DarkYellow;
    Console.WriteLine(d_1.Name + "攻击失误");
    Console.ForegroundColor = ConsoleColor.Black;
    }
    return (d_2);
    }
    static void Main(string[] args)
    {
    Console.WriteLine("1是添加,2是战斗");
    string c = Console.ReadLine();
    if (c == "1")
    {
    DZDA zz = new DZDA();
    int i = 0;
    while (i < 1)
    {
    Console.WriteLine("请输入代号:");
    string code = Console.ReadLine();
    Console.WriteLine("请输入名字:");
    string name = Console.ReadLine();
    
    if (zz.Add(code, name))
    {
    Console.WriteLine("输入成功!");
    }
    else
    {
    Console.WriteLine("输入失败!");
    }
    i++;
    Console.WriteLine("是否继续添加,继续添加选择1,停止选择2");
    int n = int.Parse(Console.ReadLine());
    if (n == 1)
    {
    i--;
    }
    else
    {
    Console.WriteLine("全部添加完毕");
    }
    }
    Console.WriteLine("回车显示添加数据");
    Console.ReadLine();
    
    
    List<DZ> list = zz.Select();
    
    foreach (DZ data in list)
    {
    string sexname=data.Sex?"":"";
    
    Console.WriteLine(data.Code + "--" + data.Name + "--" + sexname + "--" + data.Xue + "血值:" + data.Gong + "攻击:" + data.Fang + "防御:" + data.Zhong + "命中:" + data.Shan + "闪避:");
    }
    Console.ReadLine();
    }
    else if (c == "2")
    {
    DZDA zz = new DZDA();
    int j = 0;
    while (j < 1)
    {
    Console.WriteLine("请输入第一个玩家姓名:");
    string Name_1 = Console.ReadLine();
    List<DZ> list1 = zz.Select(Name_1);
    Console.WriteLine("*************");
    Console.WriteLine("请输入第二个玩家姓名:");
    string Name_2 = Console.ReadLine();
    List<DZ> list2 = zz.Select(Name_2);
    Console.WriteLine("*************");
    while (list1[0].Xue > 0 && list2[0].Xue > 0)
    {
    DZ pp2 = at(list1[0], list2[0]);
    list2[0] = pp2;
    DZ pp1 = at(list2[0], list1[0]);
    list1[0] = pp1;
    Console.ForegroundColor = ConsoleColor.Yellow;
    Console.WriteLine(list2[0].Name + ":" + (list2[0].Xue < 0 ? 0 : list2[0].Xue) + "**********" + (list1[0].Name + ":" + (list1[0].Xue < 0 ? 0 : list1[0].Xue)));
    //Console.WriteLine("aa");
    Console.WriteLine();
    Console.WriteLine();
    System.Threading.Thread.Sleep(2000);
    } 
    Console.WriteLine("---------战斗结束------------");
    Console.WriteLine();
    if (list1[0].Xue <= 0 && list2[0].Xue > 0)
    {
    Console.WriteLine(list2[0].Name + "赢了");
    }
    else if (list2[0].Xue <= 0 && list1[0].Xue > 0)
    {
    Console.WriteLine(list1[0].Name + "赢了");
    }
    else if (list1[0].Xue <= 0 && list2[0].Xue <= 0)
    {
    Console.WriteLine(list2[0].Name + "同归于尽");
    }
    j++;
    Console.WriteLine("需要继续战斗吗?继续:1,否:2");
    int d = int.Parse(Console.ReadLine());
    if (d == 1)
    {
    j--;
    }
    else
    {
    Console.WriteLine("对战完毕");
    }
    }
    }
    Console.ReadLine();
    
    }
    }
    }
    
     
    
     
  • 相关阅读:
    for each/in
    对象与结构体的区别
    php与构造函数和析构函数
    回顾PHP之类与对象
    回顾PHP之数组篇
    正则个人总结
    echo 与print_r??
    六月学习与感想
    晚到的五月博客
    java复习
  • 原文地址:https://www.cnblogs.com/zzzy0828/p/5795170.html
Copyright © 2020-2023  润新知