我有一个庞大的关卡列表,以及该关卡需要多少经验值(在游戏中逐步升级)
清单:
Level 1 - 0 xp Level 2 - 83 xp Level 3 - 174 xp Level 4 - 276 xp Level 5 - 388 xp Level 6 - 512 xp Level 7 - 650 xp Level 8 - 801 xp Level 9 - 969 xp Level 10 - 1154 xp Level 11 - 1358 xp Level 12 - 1584 xp Level 13 - 1833 xp Level 14 - 2107 xp Level 15 - 2411 xp Level 16 - 2746 xp Level 17 - 3115 xp Level 18 - 3523 xp Level 19 - 3973 xp Level 20 - 4470 xp Level 21 - 5018 xp Level 22 - 5624 xp Level 23 - 6291 xp Level 24 - 7028 xp Level 25 - 7842 xp Level 26 - 8740 xp Level 27 - 9730 xp Level 28 - 10824 xp Level 29 - 12031 xp Level 30 - 13363 xp Level 31 - 14833 xp Level 32 - 16456 xp Level 33 - 18247 xp Level 34 - 20224 xp Level 35 - 22406 xp Level 36 - 24815 xp Level 37 - 27473 xp Level 38 - 30408 xp Level 39 - 33648 xp Level 40 - 37224 xp Level 41 - 41171 xp Level 42 - 45529 xp Level 43 - 50339 xp Level 44 - 55649 xp Level 45 - 61512 xp Level 46 - 67983 xp Level 47 - 75127 xp Level 48 - 83014 xp Level 49 - 91721 xp Level 50 - 101333 xp Level 51 - 111945 xp Level 52 - 123660 xp Level 53 - 136594 xp Level 54 - 150872 xp Level 55 - 166636 xp Level 56 - 184040 xp Level 57 - 203254 xp Level 58 - 224466 xp Level 59 - 247886 xp Level 60 - 273742 xp Level 61 - 302288 xp Level 62 - 333804 xp Level 63 - 368599 xp Level 64 - 407015 xp Level 65 - 449428 xp Level 66 - 496254 xp Level 67 - 547953 xp Level 68 - 605032 xp Level 69 - 668051 xp Level 70 - 737627 xp Level 71 - 814445 xp Level 72 - 899257 xp Level 73 - 992895 xp Level 74 - 1096278 xp Level 75 - 1210421 xp Level 76 - 1336443 xp Level 77 - 1475581 xp Level 78 - 1629200 xp Level 79 - 1798808 xp Level 80 - 1986068 xp Level 81 - 2192818 xp Level 82 - 2421087 xp Level 83 - 2673114 xp Level 84 - 2951373 xp Level 85 - 3258594 xp Level 86 - 3597792 xp Level 87 - 3972294 xp Level 88 - 4385776 xp Level 89 - 4842295 xp Level 90 - 5346332 xp Level 91 - 5902831 xp Level 92 - 6517253 xp Level 93 - 7195629 xp Level 94 - 7944614 xp Level 95 - 8771558 xp Level 96 - 9684577 xp Level 97 - 10692629 xp Level 98 - 11805606 xp Level 99 - 13034431 xp Level 100 - 14391160 xp Level 101 - 15889109 xp Level 102 - 17542976 xp Level 103 - 19368992 xp Level 104 - 21385073 xp Level 105 - 23611006 xp Level 106 - 26068632 xp Level 107 - 28782069 xp Level 108 - 31777943 xp Level 109 - 35085654 xp Level 110 - 38737661 xp Level 111 - 42769801 xp Level 112 - 47221641 xp Level 113 - 52136869 xp Level 114 - 57563718 xp Level 115 - 63555443 xp Level 116 - 70170840 xp Level 117 - 77474828 xp Level 118 - 85539082 xp Level 119 - 94442737 xp Level 120 - 104273167 xp Level 121 - 115126838 xp Level 122 - 127110260 xp Level 123 - 140341028 xp Level 124 - 154948977 xp Level 125 - 171077457 xp Level 126 - 188884740 xp
我希望能够根据xp从该列表中返回一个级别。
所以,例如,如果我有200 xp,它将返回我的等级3 如果我有4,787,746 xp,它将返回我88级 (4,787,746 xp介于88和89之间,这就是为什么我会达到88级。只有当你至少达到该级别所需的最小xp时才达到新的水平)
所以我希望能够实现上述目标,而不会出现大量的条件语句。必须有比这更好的方法。
以下是获得某个级别所需经验数量的公式(如果有任何帮助):
(L是你正在计算的等级)
答案 0 :(得分:1)
简单:
var levels =
Enumerable
.Range(1, 126)
.Select(L => (int)Math.Floor(
Enumerable
.Range(1, L - 1)
.Select(x => Math.Floor(x + 300 * System.Math.Pow(2.0, x / 7.0)))
.Sum() / 4.0))
.ToArray();
Func<int, int> getLevel = xp => levels.Where(x => x <= xp).Count();
然后这个:
Console.WriteLine(getLevel(200));
Console.WriteLine(getLevel(4787746));
给出这个:
3 88
你甚至可以这样做:
Func<int, int> getLevel = xp =>
Enumerable
.Range(1, 126)
.Select(L => (int)Math.Floor(
Enumerable
.Range(1, L - 1)
.Select(x => Math.Floor(x + 300 * System.Math.Pow(2.0, x / 7.0)))
.Sum() / 4.0))
.Where(x => x <= xp)
.Count();
我的时序测试显示,这会在1毫秒内计算出一个值,因此它不应该是性能问题。
答案 1 :(得分:0)
我希望这可以提供帮助:
static void Main(string[] args)
{
//*****init for test
List<Game> g = new List<Game>();
Game g1 = new Game();
g1.level = 1;
g1.xp = 0;
g.Add(g1);
g1.level = 2;
g1.xp = 83;
g.Add(g1);
g1.level = 3;
g1.xp = 174;
g.Add(g1);
g1.level = 7;
g1.xp = 276;
g.Add(g1);
//**********
//setting your xp in order to find your level
int your_level = 180;
bool exact_sp = g.Exists(x => x.xp == your_level);
if (!exact_sp)
{
g1.level = -1; //because you do not know
g1.xp = your_level;
g.Add(g1);
List<Game> new_list = g.OrderBy(x => x.xp).ToList();
for (int i = 0; i < new_list.Count; i++)
{
if (new_list[i].xp == your_level)
{
Console.WriteLine("Your level is: " + new_list[i - 1].level);
}
}
}
else
{
for (int i = 0; i < g.Count; i++)
{
if (g[i].xp == your_level)
{
Console.WriteLine("Your level is: " + g[i].level);
}
}
}
}
struct Game
{
public int level { get; set; }
public int xp { get; set; }
}
答案 2 :(得分:0)
你去......
private void button1_Click(object sender, EventArgs e)
{
Console.WriteLine(Level(0).ToString());
Console.WriteLine(Level(83).ToString());
Console.WriteLine(Level(4787746).ToString());
Console.WriteLine(Level(188884739).ToString());
Console.WriteLine(Level(188884740).ToString());
Console.WriteLine(Level(999999999).ToString());
}
private List<int> MinimumExperience = null;
private int Level(int experience)
{
if (MinimumExperience == null)
{
MinimumExperience = new List<int>();
List<int> numerators = Enumerable.Range(1, 125).Select(x => (int)Math.Floor((double)x + 300D * Math.Pow(2, (double)x / 7D))).ToList();
for (int L = 1; L <= 126; L++)
{
int sum = 0;
for (int x = 1; x < L; x++)
{
sum = sum + numerators[x - 1];
}
MinimumExperience.Add((int)Math.Floor((double)sum / 4D));
}
}
for(int i = 0; i < MinimumExperience.Count; i++)
{
if (experience < MinimumExperience[i])
{
return i;
}
}
return MinimumExperience.Count;
}