Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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!

like image 507
James Lambley Avatar asked Feb 26 '20 12:02

James Lambley


People also ask

What can I use instead of a switch statement JavaScript?

Luckily, JavaScript's object literals are a pretty good alternative for most switch statement use-cases I can think of. The idea is to define an object with a key for each case you would have in a switch statement. Then you can access its value directly using the expression you would pass to the switch statement.

What can I use instead of a switch in Python?

What is the replacement of Switch Case in Python? Unlike every other programming language we have used before, Python does not have a switch or case statement. To get around this fact, we use dictionary mapping.

What is better switch or if-else?

A switch statement is usually more efficient than a set of nested ifs. Deciding whether to use if-then-else statements or a switch statement is based on readability and the expression that the statement is testing.


2 Answers

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]);
like image 87
Adrian Mole Avatar answered Oct 27 '22 01:10

Adrian Mole


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.

like image 2
derHugo Avatar answered Oct 27 '22 00:10

derHugo