.net制作的抽餐廳和游戲裝備的小程序
來(lái)源:易賢網(wǎng) 閱讀:910 次 日期:2015-03-31 11:42:03
溫馨提示:易賢網(wǎng)小編為您整理了“.net制作的抽餐廳和游戲裝備的小程序”,方便廣大網(wǎng)友查閱!

這篇文章主要介紹了.net制作的抽餐廳和游戲裝備的小程序,小伙伴們以后再也不用每天都考慮去哪里吃飯了,非常好玩和實(shí)用,需要的朋友可以參考下

隨便新建一個(gè)asp.net web項(xiàng)目,拖入即可?,F(xiàn)在知道為什么游戲老是暴不出好裝備,因?yàn)樗臋?quán)重是非常小地。。。中午吃飯隨機(jī)一下,想去哪一家,就把權(quán)重值設(shè)大一點(diǎn),再中不了,就認(rèn)命吧!

名單

.net制作的抽餐廳和游戲裝備的小程序 三聯(lián)

Default.aspx

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "">

<html xmlns="">

<head runat="server">

<title>加權(quán)隨機(jī)</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="開(kāi)始抽獎(jiǎng)(裝備)" onclick="btnRandom_Click"/>

<br />

<br />

<asp:Button ID="btnRandomFood" runat="server" Text="開(kāi)始抽獎(jiǎng)(餐廳)" 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

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>:<名稱(chēng):權(quán)重>

public Dictionary<string, int> Goods = new Dictionary<string, int>();

public int TotalWeight = 0;

public class Good

{

/// <summary>

/// 名稱(chēng)

/// </summary>

public string Name

{

get;

set;

}

/// <summary>

/// 權(quán)重(大于等于1,否則出現(xiàn)的可能性為0)

/// </summary>

public int Weight

{

get;

set;

}

}

public List<Good> Result = new List<Good>();

protected void Page_Load(object sender, EventArgs e)

{

}

/// <summary>

/// 初始化裝備 俺是《夢(mèng)三國(guó)》玩家,嘿嘿

/// </summary>

protected void InitGoods()

{

Goods.Clear();

TotalWeight = 0;

Goods.Add("寒冰爪", 2); //神器(拳爪類(lèi))

Goods.Add("重毛皮", 300);

Goods.Add("小毛皮", 1000);

Goods.Add("輕毛皮", 1000);

Goods.Add("神之防具打造書(shū)", 5);

Goods.Add("傳說(shuō)武器打造書(shū)", 20);

Goods.Add("紫檀樹(shù)枝", 300);

Goods.Add("硬毛皮", 600);

Goods.Add("勾魂水晶", 8);

Goods.Add("真龍炙舞劍", 30);

Goods.Add("董卓勾魂", 1); //神器中的神器(長(zhǎng)柄類(lèi))

foreach (KeyValuePair<string, int> kvp in Goods)

{

TotalWeight += kvp.Value;

}

}

/// <summary>

/// 初始化餐廳,公司樓下餐廳比較多,只寫(xiě)幾個(gè),意思意思。。。

/// </summary>

protected void InitFood()

{

Goods.Clear();

TotalWeight = 0;

Goods.Add("清真蘭州", 1);

Goods.Add("長(zhǎng)安客", 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;

}

}

//開(kāi)始抽獎(jiǎng)(裝備)

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 + "物品名稱(chēng):" + kvp.Key + " 權(quán)重:" + kvp.Value + " 暴出次數(shù):" + c.ToString() + " 暴出率:" + rate + "%<br/>";

}

}

//開(kāi)始抽獎(jiǎng)(餐廳)

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 + "餐廳名稱(chēng):" + kvp.Key + " 權(quán)重:" + kvp.Value + " 暴出次數(shù):" + c.ToString() + " 暴出率:" + rate + "%<br/>";

}

}

/// <summary>

/// 根據(jù)隨機(jī)權(quán)重判斷所在范圍

/// </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>

/// 隨機(jī)種子值(防止速度過(guò)快造成的重復(fù))

/// </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 = "";

}

}

以上所述就是本文的全部?jī)?nèi)容了,希望大家能夠喜歡。

更多信息請(qǐng)查看IT技術(shù)專(zhuān)欄

更多信息請(qǐng)查看網(wǎng)絡(luò)編程
易賢網(wǎng)手機(jī)網(wǎng)站地址:.net制作的抽餐廳和游戲裝備的小程序
由于各方面情況的不斷調(diào)整與變化,易賢網(wǎng)提供的所有考試信息和咨詢回復(fù)僅供參考,敬請(qǐng)考生以權(quán)威部門(mén)公布的正式信息和咨詢?yōu)闇?zhǔn)!

2025國(guó)考·省考課程試聽(tīng)報(bào)名

  • 報(bào)班類(lèi)型
  • 姓名
  • 手機(jī)號(hào)
  • 驗(yàn)證碼
關(guān)于我們 | 聯(lián)系我們 | 人才招聘 | 網(wǎng)站聲明 | 網(wǎng)站幫助 | 非正式的簡(jiǎn)要咨詢 | 簡(jiǎn)要咨詢須知 | 新媒體/短視頻平臺(tái) | 手機(jī)站點(diǎn) | 投訴建議
工業(yè)和信息化部備案號(hào):滇ICP備2023014141號(hào)-1 云南省教育廳備案號(hào):云教ICP備0901021 滇公網(wǎng)安備53010202001879號(hào) 人力資源服務(wù)許可證:(云)人服證字(2023)第0102001523號(hào)
云南網(wǎng)警備案專(zhuān)用圖標(biāo)
聯(lián)系電話:0871-65099533/13759567129 獲取招聘考試信息及咨詢關(guān)注公眾號(hào):hfpxwx
咨詢QQ:1093837350(9:00—18:00)版權(quán)所有:易賢網(wǎng)
云南網(wǎng)警報(bào)警專(zhuān)用圖標(biāo)