主頁 > 知識庫 > .net制作的抽餐廳和游戲裝備的小程序

.net制作的抽餐廳和游戲裝備的小程序

熱門標簽:400免費電話怎么辦理 河南語音外呼系統(tǒng)公司 外呼電銷機器人軟件 河北網(wǎng)絡回撥外呼系統(tǒng) 威海電銷 t3出行地圖標注怎么做 關于宗地圖標注技術規(guī)范 寧夏機器人電銷 400電話辦理最優(yōu)質

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

Default.aspx

%@ 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

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 Dictionarystring, int> Goods = new Dictionarystring, int>();
  public int TotalWeight = 0;
 
  public class Good
  {
    /// summary>
    /// 名稱
    /// /summary>
    public string Name
    {
      get;
      set;
    }
    /// summary>
    /// 權重(大于等于1,否則出現(xiàn)的可能性為0)
    /// /summary>
    public int Weight
    {
      get;
      set;
    }
  }
 
  public ListGood> Result = new ListGood>();
 
  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 (KeyValuePairstring, 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 (KeyValuePairstring, 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 (KeyValuePairstring, 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 + "nbsp;權重:" + kvp.Value + "nbsp;暴出次數(shù):" + c.ToString() + "nbsp;暴出率:" + 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 (KeyValuePairstring, 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 + "nbsp;權重:" + kvp.Value + "nbsp;暴出次數(shù):" + c.ToString() + "nbsp;暴出率:" + rate + "%br/>";
    }
 
  }
 
  /// summary>
  /// 根據(jù)隨機權重判斷所在范圍
  /// /summary>
  /// param name="Weight">/param>
  protected void ProduceResult(int Weight)
  {
    int min = 1;
    int max = 1;
    foreach (KeyValuePairstring, 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 = "";
  }
}

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

您可能感興趣的文章:
  • .NET實現(xiàn)魔方游戲(一)之任意階魔方的表示
  • .NET開發(fā)實現(xiàn)一個微信跳一跳的輔助程序

標簽:樂山 廣元 賀州 咸寧 淮北 池州 吉林 固原

巨人網(wǎng)絡通訊聲明:本文標題《.net制作的抽餐廳和游戲裝備的小程序》,本文關鍵詞  .net,制作,的,抽,餐廳,和,;如發(fā)現(xiàn)本文內(nèi)容存在版權問題,煩請?zhí)峁┫嚓P信息告之我們,我們將及時溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡,涉及言論、版權與本站無關。
  • 相關文章
  • 下面列出與本文章《.net制作的抽餐廳和游戲裝備的小程序》相關的同類信息!
  • 本頁收集關于.net制作的抽餐廳和游戲裝備的小程序的相關信息資訊供網(wǎng)民參考!
  • 推薦文章