I am working on a project to simulate the first deal in a game of blackjack. So far, the program creates two cards of random rank (ACE to KING) and random suit. I am struggling with created a switch table or if-else ladder assign the added value of the two cards as a variable score. The code below represents conceptually what I am trying to do, I just want to know how to make it simpler. Thanks!
public int Score()
{
int score = 0;
if (card1.rank() == TWO && card2.rank() == TWO){
score = 4;
} else if (card1.rank() == THREE && card2.rank() == TWO){
score = 5;
} else if (card1.rank() == FOUR && card2.rank() == TWO){
score = 6;
} else if (card1.rank() == FIVE && card2.rank() == TWO){
score = 7;
} else if (card1.rank() == SIX && card2.rank() == TWO){
score = 8;
} else if (card1.rank() == SEVEN && card2.rank() == TWO){
score = 9;
} else if (card1.rank() == EIGHT && card2.rank() == TWO){
score = 10;
} else if (card1.rank() == NINE && card2.rank() == TWO){
score = 11;
} else if (card1.rank() == TEN && card2.rank() == TWO){
score = 12;
return score;
}
The rank() method calls the integer value of the rank of each card, however, the card ranks are assigned in a different class as TWO = 0, THREE = 1, FOUR = 2... QUEEN = 10, KING = 11, ACE = 12 so I am trying to assign "score" to equal the actual value of the two cards, added together. Sorry if this is confusing I will answer any questions.
Something like this:
If you don't want to deal with enums, you can represent ranks as simple integers. It is less safe, but okay for simple and short code.
But whichever representation you choose, you need to pay attention to aces - if you have an ace, there will be another sum that is
10
larger; if you have two, there will be an additional sum that is20
larger.