對戰游戲 要求: 數據連接類: 實體類: namespace ConsoleApplication1 { public class Player { private string name; //玩家姓名 public string Name { get { return name; } set { ...
對戰游戲
要求:
- 自建數據表(例如:數據表包含:代號,姓名,性別,血量,攻擊力,防禦力,命中,閃避,等級等欄位)
- 需要通過程式向數據表添加人員
- 添加的時候,根據用戶輸入的名字,自動計算生成相應的血量、攻擊力、防禦力等(例如,名字的前兩個字作為種子生成血量,名字的第一個字作為種子生成攻擊力,名字的第二個字作為種子生成防禦力等)
- 添加完成用戶可以輸入兩個用戶進行對戰
- 通過程式邏輯實現對戰雙方的血量變化並顯示(使用隨機數浮動攻擊防禦)
- 最終血量先到達0的失敗,勝利的一方屬性增加(比如等級level加1,血量加一些,攻擊力加一些等)
- 訪問資料庫採用三個類的模式
數據連接類:
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; } } } }View Code
數據訪問類:
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; } } } }View Code
添加人物:
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(); } } }View Code
對戰:
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(); } } }View Code