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!
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 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.
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.
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:
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).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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With