我最近在C#中设计了一个战利品表,它工作得非常出色。然而,我想添加最后一件东西,这是一种计算从表格中挑选物品的概率的方法。由于我已将所有数据输入到在线交互式图表中,我已经知道百分比概率应该是多少,我只是不知道如何手动计算它。这里是我LootTable类:从战利品表中计算概率
using System;
using System.Collections.Generic;
using System.Linq;
namespace NovaEngineFramework.Framework.Probability
{
public class LootTable
{
private readonly List<string> _lootTable;
private readonly Dictionary<string , uint> _cachedLoot;
private readonly Random _rngInstance;
private bool _isRebuildRequired;
public LootTable()
{
_rngInstance = new Random();
_cachedLoot = new Dictionary<string , uint>();
_lootTable = new List<string>();
}
public LootTable(Random random)
{
this._rngInstance = random;
_cachedLoot = new Dictionary<string , uint>();
_lootTable = new List<string>();
}
public void AddLoot(uint probability , string name)
{
if(!_cachedLoot.ContainsKey(name))
{
this._cachedLoot.Add(name , probability);
_isRebuildRequired = true;
}
else
{
throw new Exception("Item: " + name + " Already Exists!");
}
}
public void DeleteLoot(string name)
{
if(_cachedLoot.ContainsKey(name))
{
this._cachedLoot.Remove(name);
_isRebuildRequired = true;
}
else
{
throw new Exception("Item: " + name + " Did not exist!");
}
}
public double CalculateProbability(string name)
{
if(_cachedLoot.ContainsKey(name))
{
return 0; // Calculate the actual percent probability here
}
throw new Exception("Item: " + name + " Does not exist.");
}
public uint CheckRarity(string name)
{
if(_cachedLoot.ContainsKey(name))
{
return _cachedLoot[ name ];
}
throw new Exception("Item: " + name + " Does not exist.");
}
public List<string> CheckLoot()
{
return this._cachedLoot.Keys.ToList();
}
public void EditLoot(string name , uint newProbability)
{
if(_cachedLoot.ContainsKey(name))
{
this._cachedLoot[ name ] = newProbability;
_isRebuildRequired = true;
}
else
{
throw new Exception("Item: " + name + " Does not exist.");
}
}
public void ClearAllLoot()
{
this._cachedLoot.Clear();
this._isRebuildRequired = true;
}
private void Build()
{
_lootTable.Clear();
foreach(KeyValuePair<string , uint> pair in _cachedLoot)
{
for(int i = 0; i < pair.Value; i++)
{
_lootTable.Add(pair.Key);
}
}
_isRebuildRequired = false;
}
public string NextRandomItem()
{
if(!_isRebuildRequired)
{
return _lootTable[ _rngInstance.Next(_lootTable.Count) ];
}
this.Build(); // A rebuild is needed, let's go ahead and take care of it!
return _lootTable[ _rngInstance.Next(_lootTable.Count) ];
}
}
}
,这里是我的测试中执行:
LootTable swordTable = new LootTable();
swordTable.AddLoot(1 , "Sword_Legendary_SwordOfIllimitableDemise");
swordTable.AddLoot(200 , "Sword_Common_SteelGreatSword");
swordTable.AddLoot(50 , "Sword_Rare_MagicImbuedLongBlade");
swordTable.AddLoot(15 , "Sword_Epic_Hopesfire");
swordTable.AddLoot(400 , "Sword_Trash_RustySword");
Console.WriteLine(swordTable.NextRandomItem());
和图像,以帮助促进更好的理解:
这里是交互式的图表链接,如果蒙上阴影,显示实际的基于百分比的概率。 Interactive Chart
我要求输出的视觉举例:
什么是计算战利品表的probabilites像在线图表不正确的方法是什么?
最后编辑 对于那些谁想要的最终产品与包括答案,这里是类的引擎收录链接:
生锈的剑== 400;项目总数== 666; (400/666)= 60.06% – Plutonix
@Putonix如果有人想将实际输出从“0.600600600600601”转换为更具视觉吸引力的“60.06”? – Krythic
乘以10000,转换为int,然后除以100. –