Help with Multiple If Statements

Page 1 of 1

9 Replies - 455 Views - Last Post: 01 May 2012 - 09:57 PMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=277537&amp;s=80705e74d388f469b9f01a1655d43ef8&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

#1 derekvw

Reputation: 0
• Posts: 9
• Joined: 28-April 12

Help with Multiple If Statements

Posted 30 April 2012 - 06:17 PM

I have the following code to evaluate a 3 of a kind hand in a poker game. I think the if statements are correct, but the evaluation of the card hand is not. Sometimes, I only get essentially sum+=bet...somehow the *2 isn't evaluated. Other times, I get the sum+=(bet*2). I don't really understand how it would be evaluating differently as the code is nearly identical and it always correctly tells me that I've got a three of a kind hand.

Any ideas?

Thanks so much!

```                                        //Three of a Kind (2 pts)
//No Joker
if (one == two && two == three)
{
label6.Text = "Three of a Kind";
sum += (bet * 2);
}

if (two == three && three == four)
{
label6.Text = "Three of a Kind";
sum += (bet * 2);
}

if (three == four && four == five)
{
label6.Text = "Three of a Kind";
sum += (bet * 2);
}
//One Joker
if (one == two && five == 15)
{
label6.Text = "Three of a Kind";
sum += (bet * 2);
}
if (two == three && five == 15)
{
label6.Text = "Three of a Kind";
sum += (bet * 2);
}
if (three == four && five == 15)
{
label6.Text = "Three of a Kind";
sum += (bet * 2);
}
//Two Jokers
if (four == 15 && five == 16)
{
label6.Text = "Three of a Kind";
sum += (bet * 2);
}
```

Is This A Good Question/Topic? 0

Replies To: Help with Multiple If Statements

#2 tlhIn`toq

Reputation: 4965
• Posts: 10,562
• Joined: 02-June 10

Re: Help with Multiple If Statements

Posted 30 April 2012 - 08:42 PM

This is horrible on so many levels.

First... label6... Really? Rename your controls as soon as you make them, giving them a meaningful name so anyone knows what they are just by looking at the code not just you because you wrote it. "lblFeedback" or "lblWinOrLost" for example.

Second: Do you really have individual variables for each card in the player's hand: one, two, three? Have you learned about List<>s or arrays yet?

Next... Are you really going to hardcode every possible combination of the hand like that? Just do a loop checking if the first element of the hand array matches all the successive ones if so increment a count, then the second element to the end and so on. Now you have a loop within a loop that dynamically checks for matches no matter how many cards are in the hand: Meaning you will get a result of 2 for a pair, 3 for a thee of a kind, 4 for four of a kind - whether you are dealing 5 card or 7 card variants of poker.

Next... Rule number one: We do not duplicate code. Bad coder, no Doritos. We do not copy/paste like that. What if you decide to change the payout later? You'd have to make a change for every time you pasted that line. Scrubbing through code is a maintenance nightmare. Either raise an event of "winner" that you react to in one place by setting the label and awarding the payout... or set a bool to true and do it one time at the end of the method.

Next... Don't set GUI properties like that: Use properties.

```{
bool winner = false;
if (IsTwoPair) winner = true;
if (IsThree) winner = true;
if (IsFullHouse) winner = true;
PlayerWins = winner;
}

public bool PlayerWins
{
set
{
lblWinOrLoose = value ? "Winner" : "Looser";
if (value) Player.Chips += (bet * 2);
}
}
```

Sorry to sound so harsh, but this is a good example of trying to code before learning the coding language. Its just going to make you really frustrated and walk away from coding because it feels so hard when it shouldn't. You would do yourself a HUGE favor by working on tutorials and learning C# much better before you tried designing your own programs from scratch. I don't want to see you want away just because you felt this was too hard because your reach exceeded your grasp.

Quote

Where do I start?

You start by learning a coding language FIRST.
Learn to plan before you type.
THEN you start designing software with a purpose.

Spoiler

If this sounds like you

Newbie/Rookie said:

I have a little programming experience but I need to write ...
Spoiler

Some of the tutorials below are for C# or Java not C, C++, VB.NET [...]. But the conceptual stuff of classes, object oriented design, events etc. are not language specific and should give you enough guidance in theory of program development for you to be able to look-up specific code example in your chosen coding language.

Resources, references and suggestions for new programmers. - Updated Mar 2012
Spoiler

#3 tlhIn`toq

Reputation: 4965
• Posts: 10,562
• Joined: 02-June 10

Re: Help with Multiple If Statements

Posted 30 April 2012 - 09:06 PM

derekvw You copy/pasted my entire post for some reason. No other content. ???

Did you have a question?

TIP:
You don't have to quote the entire previous message every time. It just makes the entire thread very, very long. If your comments are directly related to the last post you can just use reply box at the end of the page.

If you are responding to just a small portion of an earlier comment, then use the Quote and Respond button, but edit the portion quoted to just the part you are responding to. It keeps the reading more coherent to the rest of us without bloating the threads.

#4 derekvw

Reputation: 0
• Posts: 9
• Joined: 28-April 12

Re: Help with Multiple If Statements

Posted 01 May 2012 - 04:44 AM

Eh. Sorry. I did post something I thought.

I was just saying that I agree with your ideas but as I won't mess with this after it's turned in, I'm not going to redo the whole program at this point in the game. This is my first time working in Visual Studio (and in c#) so I really haven't got the naming conventions down yet. All we worked in in class was just c++ and a command prompt situation. I have learned about arrays, structures, classes, etc...but this is just how I saw it work out in my head. Not the ideal situation, I understand.

I was just curious as to why it sometimes evaluates one way and then other times evaluates a different way. Wouldn't it seem that anything the label put out "Three of a Kind" that it would do sum += (bet*2)? I'm honestly just thinking about putting all the math after the if statement and doing a separate if statement based on whatever label6 is.

• MrCupOfT

Reputation: 1998
• Posts: 8,808
• Joined: 29-May 08

Re: Help with Multiple If Statements

Posted 01 May 2012 - 04:56 AM

Quote

All we worked in in class was just c++ and a command prompt situation.

In .net this is called a Console Application, which is one of the Project Types you can create in VS.

#6 h4nnib4l

• The Noid

Reputation: 806
• Posts: 1,139
• Joined: 24-August 11

Re: Help with Multiple If Statements

Posted 01 May 2012 - 06:06 AM

I really think you should take tlhIn`toq's advice on rewriting your evaluations here. You're missing quite a few of the possible combinations for your three of a kind, so I can only assume you've made similar mistakes while evaluating for the other possible outcomes. For example, you evaluate for one == two && two == three, but what if one == two && two == four? And because you're using a series of plain if statements, you're potentially calling a four of a kind, a three of a kind. What if your first two if statements are both correct? That would mean four of a kind.

The point is, hard-coding should generally be avoided at all costs. Not only does that make the code cleaner, more concise, and easier to read, but it is also more extensible and reusable. This may not be a concern on this project, but it will be throughout your career, and you should learn and practice the way you will need to perform in the future.

EDIT: Grammar and spelling

This post has been edited by h4nnib4l: 01 May 2012 - 06:54 AM

#7 tlhIn`toq

Reputation: 4965
• Posts: 10,562
• Joined: 02-June 10

Re: Help with Multiple If Statements

Posted 01 May 2012 - 06:47 AM

Quote

Wouldn't it seem that anything the label put out "Three of a Kind" that it would do sum += (bet*2)?

Yes that's reasonable and correct. I will point out that if you aren't resetting that label's text anywhere then a change from "three of a kind" to "three of a kind" won't be noticed. And if it already says "three of a kind" and there is no match because as points out you are missing permutations then you have nothing to make it say anything else.

But the more important issue here is that you shouldn't be guessing about what is happening. There is no need to in Visual Studio. You can place a breakpoint at the start of the method and execute line by line while watching the variables change value.

What does this error mean?

#8 derekvw

Reputation: 0
• Posts: 9
• Joined: 28-April 12

Re: Help with Multiple If Statements

Posted 01 May 2012 - 08:32 AM

The one, two, three, four, and five are ints out of what I call, a sortArray. The cards are numbered 1>13 (Ace through Kind King) with 15/16 being two jokers. Once the hand is complete, the cards go into the array, are sorted by rank and suite and then put back into the one through five card slots. This way, my only possible combination for 3 of a kinds are what is in the code...or at least that's what I believe. Also, all of the possible card hands are sorted from highest point value to lowest so that way a 4 of a kind would evaluate before a 3 of a kind.

This post has been edited by tlhIn`toq: 01 May 2012 - 08:39 AM

#9 tlhIn`toq

Reputation: 4965
• Posts: 10,562
• Joined: 02-June 10

Re: Help with Multiple If Statements

Posted 01 May 2012 - 08:55 AM

derekvw, on 01 May 2012 - 09:32 AM, said:

This way, my only possible combination for 3 of a kinds are what is in the code...or at least that's what I believe.

Just a quick look but these permutations aren't being checked:
1, 3, 4
1, 4, 5
2, 4, 5

There may be others: I didn't study it that hard.

Then you have the problem of the if's not being exclusive, but additive.

Quote

Also, all of the possible card hands are sorted from highest point value to lowest so that way a 4 of a kind would evaluate before a 3 of a kind.

Sorting won't stop this situation 1,2,3,4 all the same: Four of a kind. You'll increase the winnings twice. You need to stop checking after you find a match. break; out of the loop or return; out of the method.

03 if (one == two && two == three)
04 {
05 label6.Text = "Three of a Kind";
06 sum += (bet * 2);
07 }
08
09 if (two == three && three == four)
10{
11 label6.Text = "Three of a Kind";
12 sum += (bet * 2);
13 }

The first if at line 3 is true so the sum is increased.
Then the if at line 9 is also true, so the sum is increased again.

Quote

4 of a kind would evaluate before a 3 of a kind.

Are you saying you have another method with all the permutation checks hard coded just like this one? This game must be 2000 lines long. If you have missed permutations in your 4 of a kind check just like you have in this 3 of a kind check then execution would have dropped through to this method, then maybe have the additive results that I just described.

#10 Momerath

• D.I.C Lover

Reputation: 818
• Posts: 1,961
• Joined: 04-October 09

Re: Help with Multiple If Statements

Posted 01 May 2012 - 09:57 PM

the numbers are sorted (he said so in his post above yours) so there are no permutations to check. He does do extra checks that he doesn't need to do, but he's covered everything.

Page 1 of 1

 .related ul { list-style-type: circle; font-size: 12px; font-weight: bold; } .related li { margin-bottom: 5px; background-position: left 7px !important; margin-left: -35px; } .related h2 { font-size: 18px; font-weight: bold; } .related a { color: blue; }