Question

I'm trying to calculate score of simple BlackJack game. I want to be able for computer to choose when to count Ace card as 1 or 11 depending on the situation of cards but I don't want to hard code this situation.

How should I do this?

This is how I count score in my dealers/players hand class:

public int calcScore()
{
    int score = 0;
    Link current = first;

    while(current != null)
    {
        score = score + current.card.getValue();
        current = current.next;
    }

    return score;
}

This is how I specify value of a card in Card class:

public int getValue()
{
    int value = 0;

    if (rank == 1)
        value = 11;
    else if (rank == 11 || rank == 12 || rank == 13)
        value = 10;
    else
        value = rank;

    return value;
}
Was it helpful?

Solution 4

As I kept in mind tips that you guys gave me plus few tips from another post on SO as fgb suggested, I came up with this.

Rather than count cards once when I need to I rather count cards as they come within inserting method of my Hand class.

This let me to count cards and decide whether to use 1 or 11 as Ace.

public void insert(Card card)
{
    Link newLink = new Link(card);
    newLink.next = first;

    if (card.rank == 1 && cardTotal + card.getValue() > 21)
        cardTotal = cardTotal + (card.getValue() - 10);
    else
        cardTotal = cardTotal + card.getValue();

    first = newLink;
}

The question is that if I should player decide when to use 1 or 11, but I'll worry about that later.

Thanks for help.

OTHER TIPS

The problem is that the calculation needs to know what the current total is, so code it that way:

public int getValue(int currentTotal) {
    int value = 0;

    if (rank == 1 && currentTotal < 11)
        value = 11;
    else if (rank == 1)
        value = 1;
    else if (rank == 11 || rank == 12 || rank == 13)
        value = 10;
    else
        value = rank;

    return value;
}

I haven't thuoght it through that thoroughly, but I'm pretty sure you don't need to check for multiple following aces, because the only way to bust would be if you hit on 21, which no one will do. If that doesn't work, you might have to pass on a cumluative total of how many aces have been counted and use more complicated logic.

I don't think you can put all the logic for determining a cards value in the card object, because you don't know until you have counted the value of all the cards whether ace=11 would have caused the hand to go bust - in which case ace magically turns into 1.

I would modify the design so that card has getMinValue() and getMaxValue(). They both work exactly the same as your getValue() except that if the card is an ace, then getMinValue returns 1 and getMaxValue returns 11.

Now you redesign the calcScore to first calculate the score using getMaxValue. If the hand goes bust, you do a more complex calculation of getMaxValue and getMinValue.

This way your card class does not have to encapsulate the business logic of blackjack, but it still provides enough information that you can handle the rules at the game level.

I know there is already an answer, but the question is a generic how to and everything is in java. Here is pseudocode logic for the answer.

calculate score with ace being equal to one

  1. set a variable score
  2. loop through the hand
  3. set a variable number equal to the card value
  4. if number is greater than 10 set equal to 10
  5. add new number to total score

Check for an ace

  1. set a variable for isThereAnAce to false
  2. loop through the deck and check to see if any cards are an ace if there is at least one set isThereAnAce to true

Perform the logic

  1. if isThereAnAce equals true and the current score is less than 12 add 10 to the score

One thing you could do also is in your card object or what ever you are calling it you could return the value depending of the current total you have. So you could just input the total you have into a get value from the card and if you have an ace it would determine whether or not to give you a 1 or 11

The other way and probably easiest is just to check the total in your get value function.

public int getValue(int currentVal)
{
    int value = 0;

    if (rank == 1 && currentVal < 11)
        value = 11;
    else if(rank == 1)
        value = 1;
    else if (rank == 11 || rank == 12 || rank == 13)
        value = 10;
    else
        value = rank;

    return value;
}

Both would work just depends of what way you want to do it.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top