.net製作的抽餐廳和遊戲裝備的小程式

來源:互聯網
上載者:User

 這篇文章主要介紹了.net製作的抽餐廳和遊戲裝備的小程式,小夥伴們以後再也不用每天都考慮去哪裡吃飯了,非常好玩和實用,需要的朋友可以參考下

  

隨便建立一個asp.net web項目,拖入即可。現在知道為什麼遊戲老是暴不出好裝備,因為他的權重是非常小地。。。中午吃飯隨機一下,想去哪一家,就把權重值設大一點,再中不了,就認命吧!

Default.aspx

?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>   <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">   <html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> <title>加權隨機</title> </head> <body> <form id="form1" runat="server"> <div> <asp:TextBox ID="txtNum" runat="server" Text="100" ></asp:TextBox> <br /> <br /> <asp:Button ID="btnRandom" runat="server" Text="開始抽獎(裝備)" onclick="btnRandom_Click"/> <br /> <br /> <asp:Button ID="btnRandomFood" runat="server" Text="開始抽獎(餐廳)" onclick="btnRandomFood_Click"/> <br /> <br /> <asp:Button ID="btnClear" runat="server" Text="清屏" onclick="btnClear_Click"/> <br /> <asp:Literal ID="lblResult" runat="server"></asp:Literal> </div> </form> </body> </html>

Default.aspx.cs

?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls;   public partial class _Default : System.Web.UI.Page { //<string,int>:<名稱:權重> public Dictionary<string, int> Goods = new Dictionary<string, int>(); public int TotalWeight = 0;   public class Good { /// <summary> /// 名稱 /// </summary> public string Name { get; set; } /// <summary> /// 權重(大於等於1,否則出現的可能性為0) /// </summary> public int Weight { get; set; } }   public List<Good> Result = new List<Good>();   protected void Page_Load(object sender, EventArgs e) { }   /// <summary> /// 初始化裝備 俺是《夢三國》玩家,嘿嘿 /// </summary> protected void InitGoods() { Goods.Clear(); TotalWeight = 0;   Goods.Add("寒冰爪", 2); //神器(拳爪類) Goods.Add("重毛皮", 300); Goods.Add("小毛皮", 1000); Goods.Add("輕毛皮", 1000); Goods.Add("神之防具打造書", 5); Goods.Add("傳說武器打造書", 20); Goods.Add("紫檀樹枝", 300); Goods.Add("硬毛皮", 600); Goods.Add("勾魂水晶", 8); Goods.Add("真龍炙舞劍", 30); Goods.Add("董卓勾魂", 1); //神器中的神器(長柄類)   foreach (KeyValuePair<string, int> kvp in Goods) { TotalWeight += kvp.Value; } }   /// <summary> /// 初始化餐廳,公司樓下餐廳比較多,唯寫幾個,意思意思。。。 /// </summary> protected void InitFood() { Goods.Clear(); TotalWeight = 0;   Goods.Add("清真蘭州", 1); Goods.Add("長安客", 1); Goods.Add("迎客松", 1); Goods.Add("吉祥混沌", 1);   foreach (KeyValuePair<string, int> kvp in Goods) { TotalWeight += kvp.Value; } }   protected int GetTryParse() { try { return int.Parse(txtNum.Text); } catch { return 1; } }   //開始抽獎(裝備) protected void btnRandom_Click(object sender, EventArgs e) { InitGoods();   lblResult.Text = lblResult.Text + "<br/>";   int Count = GetTryParse(); for (int i = 1; i <= Count; i++) { Random rdm = new Random(GetRandomSeed());   int Weight = rdm.Next(1, TotalWeight + 1);   ProduceResult(Weight); }   foreach (KeyValuePair<string, int> kvp in Goods) { int c = Result.Count(d => d.Name == kvp.Key); double rate = c * 1.0 / Count * 1.0 * 100;   lblResult.Text = lblResult.Text + "物品名稱:" + kvp.Key + " 權重:" + kvp.Value + " 暴出次數:" + c.ToString() + " 暴出率:" + rate + "%<br/>"; }   }   //開始抽獎(餐廳) protected void btnRandomFood_Click(object sender, EventArgs e) { InitFood();   lblResult.Text = lblResult.Text + "<br/>";   int Count = GetTryParse(); for (int i = 1; i <= Count; i++) { Random rdm = new Random(GetRandomSeed());   int Weight = rdm.Next(1, TotalWeight + 1);   ProduceResult(Weight); }   foreach (KeyValuePair<string, int> kvp in Goods) { int c = Result.Count(d => d.Name == kvp.Key); double rate = c * 1.0 / Count * 1.0 * 100;   lblResult.Text = lblResult.Text + "餐廳名稱:" + kvp.Key + " 權重:" + kvp.Value + " 暴出次數:" + c.ToString() + " 暴出率:" + rate + "%<br/>"; }   }   /// <summary> /// 根據隨機權重判斷所在範圍 /// </summary> /// <param name="Weight"></param> protected void ProduceResult(int Weight) { int min = 1; int max = 1; foreach (KeyValuePair<string, int> kvp in Goods) { max = min + kvp.Value - 1;   if (Weight >= min && Weight <= max) { Good g = new Good(); g.Name = kvp.Key; g.Weight = kvp.Value; Result.Add(g); return; }   min = max + 1; } }   /// <summary> /// 隨機種子值(防止速度過快造成的重複) /// </summary> /// <returns></returns> private static int GetRandomSeed() { byte[] bytes = new byte[4]; System.Security.Cryptography.RNGCryptoServiceProvider rng = new System.Security.Cryptography.RNGCryptoServiceProvider(); rng.GetBytes(bytes); return BitConverter.ToInt32(bytes, 0); }   //清屏操作 protected void btnClear_Click(object sender, EventArgs e) { lblResult.Text = ""; } }

 

以上所述就是本文的全部內容了,希望大家能夠喜歡。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.