Javascript - 查找表的结构 [英] Javascript - Structure for a look-up table

查看:121
本文介绍了Javascript - 查找表的结构的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不太确定标题中的措辞是否准确地描述了我正在寻找的内容,请允许我解释:



假设在游戏中,你会得到不同类型的积分从升级。
但是每个级别的每个类别的点数可以是任意的。



例如
我得到3 每个2级攻击点。
但是我得到2,4,5和6级的2 防守点。
除了第一个,





$ b

现在这里是我做的:

  //假设我有我的英雄
var Sylin = new Hero();
Sylin.Level = 5;

//我的目标是设置
// Sylin.OffensivePoint为6
// Sylin.DefensivePoint为6
// Sylin.SupportivePoint为4

Sylin.prototype.OffensivePoint = function(){
return 3 * Math.floor(this.Level / 2);
};

Sylin.prototype.DefensivePoint = function(){
var defPoint = 0;
(var i = 2; i< = this.Level; i ++){
//最大等级为6
if(i == 2 || i> = 4) {
defPoint + = 2;
}
}
return defPoint;
};

Sylin.prototype.SupportivePoint = function(){
return this.Level - 1;
};

这些都很好,很漂亮,但是如果最大级别被扩展,点列表将被更新,那么它真的很笨拙,特别是如果我有这样的事情:
每3级2分,而在9级和13级3级,或者显然缺少模式,所以我不能总是像我一样对于 OffensivePoint()






这种类型的问题一般是这样的结构:



级别 TotalPoint

。 。 1。 。 。 。 。 a

。 。 2。 。 。 。 。 b

。 。 3。 。 。 。 。 c

。 。 4。 。 。 。 。 d

。 。 5。 。 。 。 。 e

等等,直到最高级别



在代码中,我可以这样做:

  Sylin.prototype.talentPoint = function(){
return readTalentPointTable(this.Level); //?
};

但是,如果有20个级别可以获得5种不同类型的积分,那么这仍然可以得到很好的回旋说:/










编辑






好的,所以我可以这样做:

  var OffensivePointTable = [0,0 ,2,2,4,6,8,8,8,10,12]; 
函数getOffensivePoint(level){
return OffensivePointTable [level];
}

如果我将数据存储在一个点或者按上述的运行总计?










编辑2






好的,我可以将结构的顺序倒过来看看类型,然后是级别?

  var theTable = {
o:[0,1,0,1,1,0,0,0],
d:[0,0,2,0,2,0 ,2,0],
s:[0,1,2,3,4,5,6,7]}

//然后你的CalculateStats:
Sylin.prototype .CalculateStats = function(){
this.offensivePoint = 0;
for(var i = 1; i< = this.Level; i ++){
this.offensivePoint + = theTable [o] [i];
}
}


解决方案

你可以使用一个对象存储每个表上的点数增量(我没有使用你的确切数字,但你得到的想法):

  var LevelPoints = {
'1':{
o:1,
d:2,
s:1
}
'2 ':{
o:3,
d:1,
s:1
}
'3':{
o:1,
d:1 ,
s:0
}
'4':{
o:2,
d:3,
s:1
}
//等等。
}

例如,要访问级别2的进攻点增加,请使用 LevelPoints ['2'] o



这需要大量的打字,我想,但有时只是拥有所有数据使事情更容易。使您的代码可读取,易于更改始终不错。它也可以作为一个快速的参考 - 如果你想知道将在6级获得多少进攻点,你可以马上知道。不需要破译任何程序代码。当然,这是个人偏好。程序方法更快,使用更少的内存,所以取决于你是否值得。在这种情况下,差异可以忽略不计,所以我建议使用数据驱动的方法。



另外,请注意,我使用 var 设置此对象。因为它可以由 Sylin 构造函数的所有实例使用,将其设置为实例变量(使用)是浪费,因为它会为每个 Sylin 的实例创建对象。使用 var 可以让他们共享它,节省内存。



或者,您可以将运行中的总计存储在每个级别但是,IMO这个需要更多的努力没有好的理由。写一个函数需要较少的时间:

  Sylin.prototype.CalculateStats = function(){
this.OffensivePoint = 0;
this.DefensivePoint = 0;
this.SupportivePoint = 0;

for(var i = 1; i< = this.Level; i ++){
this.OffensivePoint + = LevelPoints [i] .o;
this.DefensivePoint + = LevelPoints [i] .d;
this.SupportivePoint + = LevelPoints [i] .s;
}
}

然后,只要用户更改水平的人物。不需要传递级别,因为函数已经可以访问 this.Level 变量。


I am not quite sure if the wording in the title accurately describes what I am looking to do, allow me to explain:

Suppose in a game, you get different type of points from leveling up. But the amount of points of each type you get each level can be arbitrary.

For example, I get 3 offensive point for every 2 level. But I get 2 defensive point at level 2, 4, 5, and 6 say. And perhaps 1 supportive point at every level except the first.


Now here's what I've done:

//Suppose I have my hero
var Sylin = new Hero();
Sylin.Level = 5;

//My goal is then to set
//    Sylin.OffensivePoint to 6 
//    Sylin.DefensivePoint to 6
//    Sylin.SupportivePoint to 4 

Sylin.prototype.OffensivePoint = function() {
    return 3*Math.floor(this.Level/2);
};

Sylin.prototype.DefensivePoint = function() {
    var defPoint = 0;
    for(var i=2; i <= this.Level; i++) {
        //maximum level being 6
        if(i==2 || i >= 4) {
            defPoint += 2;
        }
    }
    return defPoint;
};

Sylin.prototype.SupportivePoint = function() {
    return this.Level - 1;
};

It's all fine and dandy, but if the maximum level is extended, the points lists will be updated and then it gets really clumsy, especially if I have things like: 2 points every 3 level, but 3 point on the 9th and 13th level or something apparently lacking in pattern so I can't always do it like what I have for OffensivePoint().


What I have in mind for this type of problems in general is a structure like so:

Level TotalPoint
. . 1 . . . . . a
. . 2 . . . . . b
. . 3 . . . . . c
. . 4 . . . . . d
. . 5 . . . . . e
and so on until the maximum level

In the code, I could then perhaps do:

Sylin.prototype.talentPoint = function() {
    return readTalentPointTable(this.Level); //?
};

But then this can still get quite convoluted if there's 20 level with 5 different types of points you can get, say :/

.
.


EDIT


Ok, so I could do something like:

var OffensivePointTable = [0,0,2,2,4,6,8,8,8,10,12];
function getOffensivePoint(level) {
    return OffensivePointTable[level];
}

Would it be easier if I store the data by the level in which a point is increased, or by the running total as above?

.
.


EDIT 2


Ok, can I perhaps reverse the order of the structure to look at the type first, then level?

var theTable = {
    o: [0,1,0,1,1,0,0,0],
    d: [0,0,2,0,2,0,2,0],
    s: [0,1,2,3,4,5,6,7]}

//then your CalculateStats:
Sylin.prototype.CalculateStats = function() {
    this.offensivePoint = 0;
    for(var i=1; i<= this.Level; i++) {
        this.offensivePoint += theTable[o][i];
    }
}

解决方案

You could use an object to store the amount of points to increment at each table (I didn't use your exact numbers, but you get the idea):

var LevelPoints = {
  '1': {
      o: 1,
      d: 2,
      s: 1
  }
  '2': {
      o: 3,
      d: 1,
      s: 1
  }
  '3': {
      o: 1,
      d: 1,
      s: 0
  }
  '4': {
      o: 2,
      d: 3,
      s: 1
  }
  //etc.
}

For example, to access the offensive point increase at level 2, use LevelPoints['2'].o.

This requires a lot of typing I suppose, but sometimes just having all the data there makes things easier. Making your code readable to you and easy to change is always nice. It's also useful as a quick reference—if you're wondering how many offensive points will be gained at level 6, you can know immediately. No need to decipher any procedural code. Of course, this is personal preference. Procedural approaches are faster and use less memory, so it's up to you whether that's worth it. In this case the difference will be negligible, so I recommend the data-driven approach.

Also, note that I used var to set this object. Because it can be used by all instances of the Sylin constructor, setting it as an instance variable (using this) is wasteful, as it will create the object for every instance of Sylin. Using var lets them all share it, saving memory.

Alternately, you could store the running total at each level, but IMO this requires more effort for no good reason. It would take less of your time to write a function:

Sylin.prototype.CalculateStats = function() {
    this.OffensivePoint = 0;
    this.DefensivePoint = 0;
    this.SupportivePoint = 0;

    for (var i = 1; i <= this.Level; i++) {
        this.OffensivePoint += LevelPoints[i].o;
        this.DefensivePoint += LevelPoints[i].d;
        this.SupportivePoint += LevelPoints[i].s;
    }
}

Then just run this function any time the user changes the level of the character. No need to pass the level, as the function will already have access to the this.Level variable.

这篇关于Javascript - 查找表的结构的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆