if (Arrays.asList(0, 3, 4, 9).contains(terrainBase[playerxpos+ix-1][playerypos+iy])) {
// do something if the value is valid
} else {
// do something if the value is not valid
}
Alternatively, if you need to know what …
switch (terrainBase[playerxpos+ix-1][playerypos+iy]) {
case 0:
// do something if 0
break;
case 3:
// do something if 3
break;
case 4:
case 9:
// do something if 4 or 9
break;
default:
// do s.th if nothing from above
break;
}
One way would be to sort your blocks so that the ones you can collide against (if that’s what you are testing for) are all consecutive so you could just check if terrainBase[] < 5 for example.
Another very fast mechanism is to use a bit-field. If you have fewer than 32 different blocks, you can store the block type as an integer with only one bit set for each block, so as powers of 2. For example, air would be 1, dirt is 2, wood is 4, stone is 8, and so on. (terrainBase[] & 6) > 0 if the block is either dirt or wood since 2 + 4 = 6.
You can make it cleaner with some constants:
final int AIR = 1;
final int DIRT = 2;
final int WOOD = 4;
final int STONE = 8;
int x = WOOD;
println( (x & (DIRT | WOOD | STONE)) > 0 );
If you have between 32 and 64 block types, you could use a 64-bit long integer.
For the larger numbers you can write (1 << 15), for instance which is 1 left-shifted 15 places = 2^15 = 32768. e.g.:
A better way to organize it might be to create a bit-field of properties for each block type. Lets say you want to test blocks for “fall through”, “stand on”, “does damage”, “slippery”, and so on. Let the block types be consecutive numbers and make an array of block properties of type int.
final int FALL_THROUGH = 1 << 0;
final int STAND_ON = 1 << 1;
final int DOES_DAMAGE = 1 << 2;
final int SLIPPERY = 1 << 3;
int[] block_properties = {
/* AIR */ FALL_THROUGH,
/* LAVA */ FALL_THROUGH | DOES_DAMAGE,
/* ICE */ STAND_ON | SLIPPERY
};
// somewhere deep in your code:
int blockId = terrainBase[ playerPos ];
if( block_properties[ blockId ] & DOES_DAMAGE ) heath--;
This way you can easily add new block types and give them the appropriate properties without having to run through your code to add them to the if statements for their behavior. You could add a red lava block that is cool enough you can STAND_ON it, but still hot enough that it DOES_DAMAGE. Add it to the block list and properties and no need to change your code.
String stuff = "0 5 9 12 182";
for(int i = 0; i < 512; i++) {
if(stuff.indexOf(str()) => 0) {
//do stuff that only happens for the numbers in stuff
}
}
I have solved it:
If someone needs it just copy it. (obviously)
String objectsToSearchFor = "1>2>6>12>65>"; //any other symbol than ">" works
boolean hasOneOfTheSearched;
for(int ix = 0; ix < 64; i++) {
if (!(objectsToSearchFor.indexOf(str(someArray)+">") >= 0)) {
//here change the ">" to the symbol that is behind every number in the String
hasOneOfTheSearched = true;
}
}
the issue @glv pointed out is still not solved by that as it still matches 5>, and also 2> at12>, but latter is luckily also one you want to search on your search pattern…
Actually I really not understand why you convert to string and searched based on string if you just want to search for some int values and not simply do…
if (Arrays.asList(1, 2, 6, 12, 65).contains(someArrayElement) {
// do something if one of searched values
} else {
// do something else or nothing
}
But nevertheless, seems you feel comfortable with your approach, but imo it could be made better…
As a (good) programmer you should strive for almost clean and stable algorithms
The consequences of getting too lazy is that you run into more confusions the more complexer your tasks are getting…
You only need to put spaces around the numbers as long as you include one at both the start and end of your list. So " 83 8 3 " and then search for " 8 ".
But please don’t. Converting back and forth from integers to strings is just needlessly slow. Just keep things as integers as @mnse suggested and let Java find the integer elements within the array.
Or better yet, use the constant-time solution. Make an array of booleans, all false except true for the ones you want to match and just check: