简体   繁体   中英

What can I use instead of a (Switch) here?

public int[] Level1Items(int floor)
{
    switch (floor) 
    {
        case 0:
        case 1:
        case 2:
        case 3:
        case 4: return CreateItems(0, 0, 0, 0, 0, 0);
        case 5:
        case 6: return CreateItems(1, 0, 0, 0, 0, 0);
        case 7:
        case 8:
        case 9: return CreateItems(1, 1, 0, 0, 0, 0);

    }
    return generationItems;
}

Basically I have a level generation method where at certain levels the generation will change and ifferent items will appear. Levels 0-4 will have no additional spawning, level 5-6 will have 1 extra X and levels 7-9 will have 1X and 1Y etc..

So before I go and make cases 10 all the way through to 99 is there a better way I could be tackling this? Maybe with a series of ifs that just change the array at specific floors? Or is there something different entierly that I've not thought of.

Any ideas would be hugely appreciated :)

Edit 1: Thanks everyone for your input, really helped me solve my problem and thanks for all the quick responses too.

I decided to simply savemy int[] in my GameData and use the switch only to change it on the specific floors. Thanks again!

Assuming each of the six parameters in the CreateItems call will have a value of either 0 or 1 (depending on the value of floor ), then you can just determine their values by:

  1. Dividing floor by the required 'cut-off' level (integer divide will give zero for anything less than that level, and non-integer for anything equal to or above).
  2. Convert all non-zero values to 1 (leaving all zero values unchanged).

So, for example, if we use temporary variables, p0 through p5 for the parameters (you could also use an array), then:

p0 = (floor / 5 > 0) ? 1 : 0;
p1 = (floor / 7 > 0) ? 1 : 0;
//... and so forth for the other 4 parameters/levels
return CreateItems(p0, p1, p2, p3, p4, p5);

Using an array would make the code more elegant, and you could even put your 'cut-off' levels in another array, then have a loop with code like this:

for (int i = 0; i < 6; i++) p[i] = (floor / cutoff[i] > 0) ? 1 : 0;
return CreateItems(p[0], p[1], p[2], p[3], p[4], p[5]);

Actually switch is quite efficient as it is compiled into a Dictionary (HashMap) kind of access and is therefore one single access.


You could of course also simply use if here like

public int[] Level1Items(int floor)
{
    if(floor <= 4) return CreateItems(0, 0, 0, 0, 0, 0);

    if(floor <= 6) return CreateItems(1, 0, 0, 0, 0, 0);

    if(floor <= 9) return CreateItems(1, 1, 0, 0, 0, 0);

    return generationItems;
}

but this different to switch now requires 3 int compares at worse case.

基本上,您可能希望将 level:items 对存储到配置文件,可能是 json 或 xml,并在运行时根据 level 和 floor 读取配置

Personally I think, that hardcoding these values is wrong idea.

If potential number of level is high, the best way to solve this problem, is some kind of generic formula, that will calculate each parameter based on number of level.

Using a switch statement, you could write it (C# 8) like so:

public int[] Level1Items(int floor) => floor switch
{
  var i when i <= 4 => CreateItems(0, 0, 0, 0, 0, 0),
  var i when i <= 6 => CreateItems(1, 0, 0, 0, 0, 0),
  var i when i <= 9 => CreateItems(1, 1, 0, 0, 0, 0),
  _ => generationItems
}

Not sure, if I would prefer this option over simple ifs or something like this

public int[] Level1Items(int floor) =>
    (i <= 4) ? CreateItems(0, 0, 0, 0, 0, 0) :
    (i <= 6) ? CreateItems(1, 0, 0, 0, 0, 0) :
    (i <= 9) ? CreateItems(1, 1, 0, 0, 0, 0) :
    generationItems;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM