欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

.net制作的抽餐厅和游戏装备的小程序

程序员文章站 2024-02-21 21:31:31
随便新建一个asp.net web项目,拖入即可。现在知道为什么游戏老是暴不出好装备,因为他的权重是非常小地。。。中午吃饭随机一下,想去哪一家,就把权重值设大一点,再中不了...

随便新建一个asp.net web项目,拖入即可。现在知道为什么游戏老是暴不出好装备,因为他的权重是非常小地。。。中午吃饭随机一下,想去哪一家,就把权重值设大一点,再中不了,就认命吧!

.net制作的抽餐厅和游戏装备的小程序

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

以上所述就是本文的全部内容了,希望大家能够喜欢。