The following code does work how I need it to, but it's ugly, excessive or a number of other things. I've looked at formulas and attempted to write a few solutions, but I end up with a similar amount of statements.
Is there a type of math formula that would benefit me in this instance or are 16 if statements acceptable?
To explain the code, it's for a kind of simultaneous-turn-based game.. two players have four action buttons each and the results come from an array (0-3), but the variables 'one' & 'two' can be assigned anything if this helps. The result is, 0 = neither win, 1 = p1 wins, 2 = p2 wins, 3 = both win.
public int fightMath(int one, int two) {
if(one == 0 && two == 0) { result = 0; }
else if(one == 0 && two == 1) { result = 0; }
else if(one == 0 && two == 2) { result = 1; }
else if(one == 0 && two == 3) { result = 2; }
else if(one == 1 && two == 0) { result = 0; }
else if(one == 1 && two == 1) { result = 0; }
else if(one == 1 && two == 2) { result = 2; }
else if(one == 1 && two == 3) { result = 1; }
else if(one == 2 && two == 0) { result = 2; }
else if(one == 2 && two == 1) { result = 1; }
else if(one == 2 && two == 2) { result = 3; }
else if(one == 2 && two == 3) { result = 3; }
else if(one == 3 && two == 0) { result = 1; }
else if(one == 3 && two == 1) { result = 2; }
else if(one == 3 && two == 2) { result = 3; }
else if(one == 3 && two == 3) { result = 3; }
return result;
}
To be quite honest, everyone has their own style of code. I wouldn't have thought performance would be affected too much. If you understand this better than using a switch case version, then carry on using this.
You could nest the ifs , so potentially there would be a slight performance increase for your last if checks as it wouldn't have gone through as many if statements. But in your context of a basic java course it probably won't benefit.
So, instead of...
You'd do...
And just reformat it as you'd prefer.
This doesn't make the code look better, but potentially speeds it up a little I believe.
I personally like to cascade ternary operators:
But in your case, you can use:
Or, you can notice a pattern in bits:
So you can use magic:
I don't have experience with Java so there might be some typos. Please consider the code as pseudo-code.
I'd go with a simple switch. For that, you'd need a single number evaluation. However, for this case, since
0 <= one < 4 <= 9
and0 <= two < 4 <= 9
, we can convert both ints to a simple int by multiplyingone
by 10 and addingtwo
. Then use a switch in the resulting number like this:There's another short method that I just want to point out as a theoretical code. However I wouldn't use it because it has some extra complexity that you don't normally want to deal with. The extra complexity comes from the base 4, because the counting is 0, 1, 2, 3, 10, 11, 12, 13, 20, ...
Really just additional note, in case I'm missing something from Java. In PHP I'd do:
Why not use an array?
I will start from the beginning. I see a pattern, the values goes from 0 to 3 and you want catch all possible values. This is your table:
when we look at this same table binary we see the following results:
Now maybe you already see some pattern but when I combine value one and two I see that you're using all values 0000, 0001, 0010,..... 1110 and 1111. Now let's combine value one and two to make a single 4 bit integer.
When we translate this back into decimal values we see an very possible array of values where the one and two combined could be used as index:
The array is then
{0, 0, 1, 2, 0, 0, 2, 1, 2, 1, 3, 3, 2, 1, 3, 3}
, where it's index is simply one and two combined.I'm not a Java programmer but you can get rid of all if statements and just write it down as something like this:
I don't know if a bitshift by 2 is faster than multiplication. But it could be worth a try.
This uses a little bit of bitmagic (you're already doing it by holding two bits of information (low/high & attack/block) in a single integer):
I haven't run it, only typed it here, please doublecheck.The idea surely works. EDIT: It is now tested for every input, works fine.Or should I suggest to separate the two bits of information into separate variables? Code based mostly on bit operations like this above is usually really hard to maintain.
Here's a fairly concise version, similar to JAB's response. This utilises a map to store which moves triumph over others.
Example:
Prints: