views:

169

answers:

4

Hello! I've designed a simple card game where two cards are displayed and the user has to bet on whether they will get a card that is inbetween the two cards displayed. If the user doesn't want to bet, they just deal again. The user begins with £100.
The game works fine in most aspects, but has a huge flaw. The user can bet more than they have in thier balance. So, if the user has £100, they bet £105, and they win, they will have £205 in thier balance. This is clearly bad! And if they have £100, they bet £105 and they lose, thier balance stays the same. This is also pretty bad.
So I thought a simple if-statement would sort this out:

if (wager > balance)
{
    winLoseLabel.Text = "You can't bet more than you have!";
}  
switch (betResult)
{
    case TIE:
        winloseLabel.Text = "Tie. You still lose. HA!";
        myRules.Balance -= wager;
        break;

    case PLAYERWINS:    
        winloseLabel.Text = "You win. Woop-de-do..";
        myRules.Balance += wager;
        break;

    case DEALERWINS:
        winloseLabel.Text = "You lose. Get over it.";
        myRules.Balance -= wager;
        break;
}

Why doesn't this work? I'm pretty sure it's something so simple, but I'm pretty new to C#, so go easy on me!

+12  A: 

You should have an else there:

if (wager > balance)
{
    winLoseLabel.Text = "You can't bet more than you have!";
}
else
{  
    switch (betResult)
    {
        //...
    }
}
Dan Dumitru
@Dan Dumitru: Wow, I've clearly been staring at my screen to long that my brain has fried. Sorry for my stupidity! Thank you!
New Start
Remember to accept the answer...
Christian W
@Christain W: I know. You have to wait a certain amount of time before you can.
New Start
Yes yes, 15 minutes. And thanks guys for the upvotes, but I think it's OK now, I don't deserve anymore of them for this simple answer...
Dan Dumitru
Still helped me out though! Oh, and I posted a question yesterday which is half resolved, I'v edited it. If people could have a look I'd be very grateful, it's much more important than my card game!
New Start
Sorry, didn't know about the time limit. :)
Christian W
+2  A: 

After your if statement you go into the case statement anyway, shouldn't you have an else around the case statement/?

w69rdy
+3  A: 

Your if-statement is correct, however, you do not end the routine if it's triggered.

You can do this by adding a "return;" statement after setting the label, or, if you are depending on code underneath what you are showing us, you can include the switch-statement in the "else" part of the if-statement...

Christian W
+2  A: 

I dont understand exactly, but try

if (wager > balance)
{
    winLoseLabel.Text = "You can't bet more than you have!";
    return;
}  

or

if (wager <= balance)
{
    switch (betResult)
    {
        case TIE:
            winloseLabel.Text = "Tie. You still lose. HA!";
            myRules.Balance -= wager;
            break;

        case PLAYERWINS:    
            winloseLabel.Text = "You win. Woop-de-do..";
            myRules.Balance += wager;
            break;

        case DEALERWINS:
            winloseLabel.Text = "You lose. Get over it.";
            myRules.Balance -= wager;
            break;
    }
}  
Serkan Hekimoglu