Calculating a players hand value in Blackjack using C#

I am a recent graduate at the beginning of my software development career. I enjoy documenting my learnings through my blogs
I now had the main areas of my Blackjack c# program complete for my college assessment. My Blackjack game would create a deck and shuffle it and I could deal cards to players of the game. My next step in this program was to find the values of the cards in the player's hands. My first step was to simply get the card values of the cards in for each player of the game.
Basic hand value
To start my method for getting the added values of the cards in the hand, I began by creating a hand value variable and set this to 0. With this, I then created a for loop to go through each card in the hand and then these card values would be added to the hand value variable which was created at the start of the method.
for (int i = 0; i < Hand.Length; i++)
{
if (Hand[i] == null)
continue;
handValue += Hand[i].CardValue;
}
With this first step finished and the cards now being totalled for the player's scores, I moved on to the next step in my hand values method which relates specifically to the ace card.
Hand value with Ace card
In the game Blackjack, Ace cards can be valued at 11 or 1, this can be helpful to players if their hand is over 21 but have an Ace because they can still be in the game. Just to note before continuing the value of the Ace card defaults to 11. To create the code to alternate between these values, I started with a diagram of what exactly is needed from this method to decide when the value is changed.

As you can see from my diagram above, Step 1 is to have the hand value, this is already functioning. Step 2, is to check if the hand value is ≤21, if it is then the game will continue and ask the player if they want to gamble and take another card. Step 3, if the hand value is >21, does the hand has any Ace cards? For my method, it does not matter how many just if there are. If there are no Aces in the hand, and they are >21, the player loses. Step 4, there is an Ace in the hand, the value of the Ace can change to 1 by subtracting 10 from the hand value. Step 5, check the hand value again to see if it is ≤21, if it is still >21 I want to check again for another Ace card, otherwise I want to return the hand value.
Is there an Ace card?
My code starts with a method to check if there is any Ace card in the hand of the player, I did this by using a boolean return type. This method of code checks if there is a card in the hand and then checks if there is a card type of Ace, this is done by creating a loop to go through each card in the hand. If there is an Ace in the hand it will return true, if not, it will return a false.
foreach (Card card in cards)
{
if (card == null)
continue;
if (card.CardType == "Ace")
return true;
}
return false
This method is then set to a variable in the hand value method and used in conjunction with an if statement.
if (handValue <= 21)
{
return handValue;
}
else
{
if (handHasAce)
{
for (int ace = 0; ace < numOfAces; ace++)
{
handValue -= 10;
if (handValue <= 21)
break;
}
}
}
return handValue;
As you can see in the code snippet above I need to find the number of Aces in the hand because there can be more than one.
How many Ace cards?
Finding the number of Aces within the hand has its own method as well, this method checks each of the cards in the hand by using a foreach loop. If the card object is null it will be skipped, and if there is an ace at any index in the array it will count this and move to the next card. The result will be the number of Aces in the hand, if there are none it will default to 0 through a variable created at the beginning of the method. This method is then utilised in the hand value method, shown earlier.
foreach (Card card in cards)
{
if (card == null)
continue;
if (card.CardType == "Ace")
aceCount++;
}
return aceCount;
To summarise, firstly the hand value is calculated, and the hand value is then checked whether it is ≤ 21 or > 21. If > 21 it will then check if there are any Ace cards in the hand, the next goes is a for loop that will loop for the number of Aces in the hand. The first Ace found will guide the code to reduce the hand value by 10. The hand value is checked again and if it is > 21, the hand will be checked for another Ace card. This will repeat either until the hand value is ≤ 21 or if there are no more Ace cards. The hand value will then be displayed to the console.
My learning...
- Problem-solving: I learned to break down a problem into sections while calculating the hand values, mainly by deciding when to alternate the Ace value. I simulated a game using an actual deck of cards and broke down the reasons why an Ace's value would need to be changed. This helped a significant amount, as well as drawing out a step-by-step guide, in creating the individual methods used.




