Why will this not work? Bad use of IF function?

Jun 24, 2015 at 6:12pm
Hi guys,

I have just finishing writing a program that simulates the casino game 'Three Card Poker'. I did it by creating an array of objects (my playing cards), shuffling them using a FOR statement, and then dealing them. I had a problem getting the program (compiler?) to recognise 3 of a kind hands however, and although I have solved this problem, I cannot seem to figure out why the first way did not work. I feel as though I am misunderstanding something and wondered if anyone could shed a light onto this.

This is the code that DOES WORK.

1
2
3
4
5
6
             //Three of a Kind 2s
            else if ((pcard1.value == 2) && (pcard2.value == 2) && (pcard3.value == 2))
            {
                playerhandrank = 42;
                bankroll += win*4;
            }


I made multiple ELSE IF statements for each value.

This is what I originally had (below), and the program kept giving back random rankings.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15

  else if((pcard1.value == pcard2.value) && (pcard1.value == pcard3.value))
     {
          if (pcard1.value == 2)
          {
             playerhandrank = 42;
             bankroll += win*4;
           }

           else if (pcard1.value == 3)
          {
             playerhandrank = 43;
             bankroll += win*4;
           }
    }


Etc etc. I cannot understand why the first code works but the second does not? Am I using bad form and confusing the compiler?!

Thanks very much & apologies for the lengthy post.

Last edited on Jun 24, 2015 at 6:14pm
Jun 24, 2015 at 6:17pm
Can you post a minimal compiling example reproducing your problem?
http://sscce.org/
Jun 24, 2015 at 6:17pm
There's nothing wrong with the code you've posted, by itself. There could be something wrong in the code that comes later that trashes the program state.
Jun 24, 2015 at 8:04pm
MiiNiPaa, the first code compiles:

1
2
Player Hand Values = 2, 2, 2
Playerhand rank = 42.


The second compiles exactly the same, however the rank is incorrect for all different 3 of a kind values. i.e I compiled that code just and got the result:

1
2
Player Hand Values = 2, 2, 2
Playerhand rank = 7.


I assumed that it was an error in the code but helios suggests that the code is fine, as I originally thought myself. Confused :S
Last edited on Jun 24, 2015 at 8:05pm
Jun 24, 2015 at 9:02pm
MiiNiPaa, the first code compiles:
No it is not: http://ideone.com/u5DqxP
Minimal compiling example means code which we can compile and see your problem for ourselves.
Your code is indeed fine and that why I asked for code.
Jun 25, 2015 at 8:37am
Sorry MiiniPaa, I misunderstood. As it turns out I accidentally fell upon the solution whilst doing something else, the problem was using == when equalling pcard1.value to pcard3.value. Using just = made it work, although i'm not sure why. Ill research operators some more to see if there is a reason for that. Thanks very much for the replies.
Jun 25, 2015 at 1:16pm
= doesn't compare the operands. It sets the variable on the left to the value on right and the whole expression evaluates to the value on the right. (x = y) == y regardless of the value of x.
Topic archived. No new replies allowed.