I am implementing a little Black Jack game in C# and I have the following problem calculating the player’s hand value. Aces may 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 value of 11, otherwise it will have a value of 1.
Now lets assume I do not know how many aces the player has got and the game is implemented giving the possibility to the dealer to use more than one deck of cards. The user may have in one hand even 5, 6, 7, 8… aces.
What is the best way (possibly using Linq) to evaluate all the aces the player has got to get the closest combination to 21 (in addition to the other cards)?
I know the players’ cards and I want to calculate their values, using the aces to reach the closest value to 21.
Add up the value of all the 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 feels like homework, this is intentionally not the complete answer but should guide you along.