-5

You all might have came across the scenarios like the following ones:

-(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;

}

In short, how to effectively simplify the above scenario in Objective-C's ambience?

Any Suggestions/Ideas/Solutions ? Cheers :)

Edit: For reference , scenario taken from here.I hope this question might save even one sec of needy developer.

Community
  • 1
  • 1
itechnician
  • 1,645
  • 1
  • 14
  • 24
  • Have you tried a switch statement? Or are you looking for something else? http://www.techotopia.com/index.php/The_Objective-C_switch_Statement – sridesmet Mar 27 '14 at 12:02
  • 3
    Why don't you use one of the answers to the question http://stackoverflow.com/questions/22501230/too-many-if-statements that you linked to? - It should be easy to translate the proposed Java solutions to C or Objective-C. – Martin R Mar 27 '14 at 12:03
  • 1
    What else can be added that is not in the thread you are referring to? – Raul Guiu Mar 27 '14 at 12:03
  • @MartinR: Note: I stated here that in objective c environment , I need the solution. If you can then post the solution rather than negating – itechnician Mar 27 '14 at 12:08
  • @itechnician: I don't know what you mean by "negating" (but I did *not* downvote your question). - I have simply suggested that you try one of the already available solutions. – Martin R Mar 27 '14 at 12:13

1 Answers1

6

Objective C is built over C. So any good C solution will be also appropriate for Objective C. Like

int result[][4] = {
    { 0, 0, 1, 2 },
    { 0, 0, 2, 1 },
    { 2, 1, 3, 3 },
    { 1, 2, 3, 3 }
};
return result[one][two]

As I know there is no Objective C - specific good practices for such problems.

Avt
  • 16,927
  • 4
  • 52
  • 72
  • Thanks for consideration.I needed the solution in Objective C because our MAC based tool app was running on Ojbective C based Architecture and for adding 'C' language instructions we need to have more 2^16 memory. – itechnician Mar 27 '14 at 12:19