0

I have the method below, which in my Blackjack app will get the value of the hand which is an NSMutableArray. The problem here is that when 2 Ace's are in a hand, it should be a 12, but because it counts Ace's as 11, it results in being 22, which then makes lowValue returned.

How can I make it so that I can check and see if the for loop has already found an Ace and if it finds another, makes the next Ace worth only 1, not 11?

Thanks!

int getHandValue(NSMutableArray *hand) {
    int lowValue = 0;
    int highValue = 0;

    for (KCCard *aCard in hand) {
            if (aCard.value == Ace) {
                lowValue+= 1;
                highValue+= 11;
            } else if (aCard.value == Jack || aCard.value == Queen || aCard.value == King) {
                lowValue += 10;
                highValue += 10;
            } else {
                lowValue += aCard.value;
                highValue += aCard.value;
            }
    }

    return (highValue > 21) ? lowValue : highValue;     
}
2
  • You can add a "BOOL isFindAce" to save whether you have find an ACE. Commented Mar 26, 2012 at 5:21
  • @zsxwing So if I put a BOOL underneath int highValue, and when the value is an Ace, set the BOOL to true/yes, and if another Ace comes up, it reads true/yes and makes it worth only 1? Commented Mar 26, 2012 at 5:22

3 Answers 3

1

Perhaps you can add a boolean value before the for loop setting it initially to NO. When an Ace is found then you can break from the for loop after setting the boolean to YES, that way if you encounter another Ace && your boolean value == YES you can handle the case accordingly.

Sign up to request clarification or add additional context in comments.

1 Comment

That is what I was thinking before, but didn't know if it was efficient. Now, hearing in writing makes it sound so clear. Thanks Tams!
0
int getHandValue(NSMutableArray *hand) {
    int lowValue = 0;
    int highValue = 0;
    BOOL isFoundAce = NO;

for (KCCard *aCard in hand) {
        if (aCard.value == Ace) {
            if (isFoundAce) {
                 lowValue+= 1;
                 highValue+= 1;
            }
            else {
                 lowValue+= 1;
                 highValue+= 11;
                 isFoundAce= YES;
            }

        } else if (aCard.value == Jack || aCard.value == Queen || aCard.value == King) {
            lowValue += 10;
            highValue += 10;
        } else {
            lowValue += aCard.value;
            highValue += aCard.value;
        }
}

return (highValue > 21) ? lowValue : highValue;     
}

2 Comments

I'm sorry that I have not compile this code. It's just an example.
I finished typing out the code and it came out looking nearly exactly what you put! Thanks zsxwing!
0

My example without a redundant code from zsxwing's example:

int getHandValue(NSMutableArray *hand) {
    int cardValue = 0;
    int aceCount = 0;
    for (KCCard *aCard in hand) {
        if (aCard.value == Ace) {
            aceCount++;
            cardValue += 11;
        } else if (aCard.value == Jack || aCard.value == Queen || aCard.value == King) {
            cardValue += 10;
        } else {
            cardValue += aCard.value;
        }
    }
    while ((cardValue > 21) && (aceCount > 0)) {
        cardValue -= 10;
        aceCount--;
    }
    return cardValue;
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.