Click here to Skip to main content
15,210,798 members
Rate this:
Please Sign up or sign in to vote.
See more:
Basically i am trying to make a Blackjack game in c#, a problem i'm having is that the Ace has two possible values, 1 and 11. I as wondering how i could give two values to A single, "int Count variable"

What I have tried:

I Found code for a black jack game online playing cards - C# Singleplayer Blackjack Game - Code Review Stack Exchange[^]

This basically used this
switch (Face)
            {
                case Ten:
                case Jack:
                case Queen:
                case King:
                    Value = 10;
                    break;
                case Ace:
                    Value = 11;
                    break;
                default:
                    Value = (int)Face + 1;
                    break;
            }


Firstly i was wondering how they converted a non numerical enum into an integer, and secondly how would this give it two values.

What i did was
if (Settings.FirstCard == "A" && Settings.SecondCard == "A")
                        {
                            Console.WriteLine("Your Cards are A and A");
                            Console.WriteLine("Your Count is 12");
                            Settings.FirstCount = 12;

                        }
                        else
                        {
                            if (Settings.FirstCard == "A")
                            {
                                Console.WriteLine("Your Cards are 'A' and {0}", Settings.SecondCard);
                                Console.WriteLine("Your Possible counts are {0} and {1}", Settings.SecondCount + 1, Settings.SecondCount + 11);
                            }
                            else
                            {
                                Console.WriteLine("Your Cards are {0} and A", Settings.FirstCard);
                                Console.WriteLine("Your Possible counts are {0} and {1}", Settings.FirstCount + 1, Settings.FirstCount + 11);
                            }
                        }

Although this worked fine for the initial deal, when a person would hit, it would fall apart
Posted
Updated 28-May-20 22:18pm
Rate this:
Please Sign up or sign in to vote.

Solution 1

You have to code it as a "special case" and check for Ace when you are doing counts everywhere you are using it.
Think about it: Ace and Ace have three possible values, which have to be checked as four:
 1 +  1  ==  2
 1 + 11  == 12
11 +  1  == 12
11 + 11  == 22
   
Comments
NotAComputerScienceStudent 29-May-20 4:21am
   
Ok, so i check if 11 leads to a bust and if it does ignore it but if it does not use it
OriginalGriff 29-May-20 4:28am
   
That should work.
Rate this:
Please Sign up or sign in to vote.

Solution 2

You could use an enum with bit flags, see example here: Enumeration types - C# reference | Microsoft Docs[^]
   

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100