Can you help me simplify those IF please

Can you explain to me how to simplify all those IF, thank you

Here's the code : http://textuploader.com/dlgba
it would help if you describe what you're trying to do.
1
2
3
4
5
6
7
8
if(a){
   if(b){
     //here a and b are true
   }
}
if( a and b){
   //...
}
when checking «royale» you already know that it was a «straight», so may only check the first value.

Separate «flush» and «straight» to their own function
1
2
3
4
5
6
7
8
9
10
11
//straight
for(int K=1; K<5; ++K)
   if( hand[K].value not_eq hand[0].value+K )
      return false;
return true;

//flush
for(int K=1; K<5; ++K)
   if( hand[K].suit not_eq hand[0].suit)
      return false;
return true;
you can make the code prettier with loops as shown.
You can combine the comparisons into one statement (if cond1 && cond2 && .... condN).

you can make it incomprehensible with bitwise logic, or even boil it down to a single integer comparison. For example you can stuff 4 1-byte values into a 4 byte int and check that against an int pointer cast of values. That is just ugly but it does reduce to a single check. 5 bytes for the suits gets you a flush check.

you can also make them strings and simply do a single == check. Or write your own class with comparisons to do an == check.

I recommend a balance of being able to understand it against a deeply nested condition given that it does not seem like you need to speed it up.


Another approach:
switch(a +2* b) {
case 0: //a false, b false
case 1: // a true, b false
case2: // a false, b true
case3: // a true, b true
}
This is one of a collection of techniques seldom seen but always useful.
Topic archived. No new replies allowed.