Calculating Blackjack Hand Values

I am implementing a small Black Jack game in C # and I have the following problem calculating the value of a player’s hand. Aces can have a value of 1 or 11 based on the player’s hand. If the player has three cards and one ace, then if the sum of the cards is <= 10, the ace will have the value 11, otherwise it will have the value 1.

Now let's assume that I do not know how many aces the player has, and the game is implemented, enabling the dealer to use more than one deck of cards. The user can have even 5, 6, 7, 8 ... aces in one hand.

What is the best way (possibly using Linq) to evaluate all the aces a player has received in order to get the closest hand to 21 (in addition to other cards)?

I know the cards of the players, and I want to calculate their values ​​using aces to reach the closest value to 21.

+3
source share
5 answers

Add the value of all non-aces and add the number of aces: 2, Q, A, A = 2 + 10 + (2) = 14

Then subtract that from 21: 21 - 14 = 7

Is this number less than 10 (if only 1 ace == 11)? Less than 20 (if both aces == 11)?

Since this is like homework, this is intentionally not a complete answer, but it should guide you.

+3
source

It doesn't seem so complicated

  • you just add all the other cards and find the smaller value that the hand has
  • You now have two cases:
    • sum <= 10: , , 11 : sum + 11 + (numAces-1)*1 <= 21, 21, sum + numAces*1 ( 11 )
    • sum > 10: 1, sum + numAces*1

( #, )

+3

, Aggregate extenstion method.

, :

IEnumerable<int> cards = // card values
var f = (int i1, int i2) => // implement algorithm here
var result = cards.Aggregate(f);
0

, , , , . , 21, , .

, , , 16, 5, 16, 5, . , 6 16.

, . , . , ace-one-eleven , , 1. , ace + 10 , .

- , . , ; , , , .

public interface Hand
{
   IEnumerable<int> PossibleValues { get; set; }
}
public interface Card
{
   CardValues PossibleValues { get; set; }
}
public interface CardValues
{
    int Value1 { get; }
    int Value2 { get; }
}

public class BlackjackHand : Hand
{
    IList<Card> cards;

   public IEnumerable<int> PossibleValues
   {
        IList<int> possible_values = new List<int>();

        int initial_hand_value = cards.Sum(c => c.Value1);
        if(initial_hand_value <= 21)
        {
            possible_values.Add(initial_hand_value);
            yield return initial_hand_value;
        }

        foreach(Card card in cards.Where(c => c.Value2 > 0))
        {
            IList<int> new_possible_values = new List<int>();
            foreach(int value in possible_values)
            {
                var alternate_value = value + card.Value2;
                if(alternate_value <= 21) 
                {
                    new_possible_values.Add(alternate_value);
                    yield return alternate_value;
                }
            }
            possible_values.AddRange(new_possible_values);
        }

        yield break;
   }
}
0

: 11. , , 10. :

int total = 0;
...
int hit = GetHit();
total = total + hit;

if (total > 21 && hit == 11)
{
  total = total - 10
}
// check if player busted, etc

In your initial transaction, you can either "deal" with cards using GetHit (), or deal with Ace-Ace (only a problem case with two cards) yourself. This will always give you the highest hand. I would have thought it would be nice to assume that the player knows that the ace can be 1 or 11, so a hit with Ace-5 will never make the player go broke.

0
source

Source: https://habr.com/ru/post/1735948/


All Articles