I'm designing a Black Jack program. My issue is that, I use a switch case to generate a random card. But when it comes time to compare the value of the cards....lets say if(pCard1 + pCard2 > 21)
it's not allowing me to compare them because they are strings. However, they need to be strings because I have to be able to assign String "Queen"
but I also need "Queen" to have an int value of 10. I'm not sure how to go about this.
Here's what I have so far. I'm not including my entire code because its a couple hundreds of lines long. Only including the blackjack section.
System.out.println("---------------------Black Jack--------------------------");
System.out.println("Welcome to BlackJack!");
System.out.println("Available balance is $"+balance);
System.out.print("How much would you like to bet on this hand?: ");
int bet = input.nextInt();
balance -= bet;
System.out.println("You just bet $"+bet+"......Dealing cards!");
System.out.println("----------------------------------------------------------");
String pCard1 = dealCard();
String pCard2 = dealCard();
System.out.println("Your hand is a "+pCard1+" and "+pCard2);
System.out.print("Would you like to Hit or Stand? :");
String hOs = input.next();
if(hOs.equals("Hit")) {
String pCard3 = dealCard();
if(pCard1 + pCard2 + pCard3);
}
}
}
public static String dealCard() {
int value = (int)(Math.random() * 13);
String returnString = "";
switch ( value ) {
case 1: returnString = "Ace"; break;
case 2: returnString = "2"; break;
case 3: returnString = "3"; break;
case 4: returnString = "4"; break;
case 5: returnString = "5"; break;
case 6: returnString = "6"; break;
case 7: returnString = "7"; break;
case 8: returnString = "8"; break;
case 9: returnString = "9"; break;
case 10: returnString = "10"; break;
case 11: returnString = "Jack"; break;
case 12: returnString = "Queen"; break;
case 13: returnString = "King"; break;
}
return returnString;
}
}
I recommend using Enum, so you can compare the cards easily. your desired enum class would be:
then you can change your
dealCard()
method to:to compare card values use something like:
Ten, Jack, Queen, and King are all different ranks but share the same value of 10. Since they all have the same value, you need more than just a card's value to determine it's rank. Admittedly, you could represent Jack's value as 11, Queen as 12, etc and then code your methods to recognize any value over 10 as 10, but this is bad coding practice.
Instead, you should encode both the rank and the value in each card (Additionally, you may want to include suit; although that may not matter for blackjack).
Rather than represent your card as a string, I recommend making a Card class that has a String rank and an integer value. Additionally, this would be a good opportunity to explore enums in Java as others have commented.
I think enums are the best way to address this kind of issue.
If we define enums for Suit and Pips like so
And if we define a class Card:
Card
usestoString()
to convert Suit and Pips to strings, while a zero-index numeric value can be obtained usingordinal()
:This simple implementation has a couple of limitations. First, the name of the suit or pips can only ever be a string version of the enum name. What if you wanted all your enums in UPPER CASE as is good style? The second problem is that the ordinal is always zero-indexed. What if you need some other id system for the pips?
In this case we add maps to each enum to hold these mappings. Unfortunately we can't use inheritance to share code between enums and so the boiler plate gets a little wordy.
With these new enums, we modify
Card.toString()
to use the newgetName()
methods:And now when we work with Cards we can use any of the ID or the name or the enum and convert freely between them: