principlejavaModerate
Blackjack Strategy
Viewed 0 times
blackjackstrategystackoverflow
Problem
I am creating a Blackjack simulator in Java in order to better understand the language. I have written the basic strategy section for a player and it is terribly long.
Is there a better way to do this other than
Is there a better way to do this other than
if/else statements? I know a switch might work but that wouldn't make it much shorter. The reality is that there are quite a few situations and reactions when playing Blackjack.public void play(Card upCard){
//Use this block to check for situations that happen right after dealing
//like blackjack, doubledown and splits.
if (getHand().size() == 2){
if (getHandValue() == 21){
blackjack();
}
//scenario with ace as one of the cards.
else if (getHand().get(0).getName().equals("Ace") |
getHand().get(1).getName().equals("Ace")){
//Check if both cards are aces, always split aces
if (getHand().get(0).getName().equals("Ace") &
getHand().get(1).getName().equals("Ace")){
split();
}
//Double down on Ace, 2 against dealer 5 or 6
else if(getHand().get(0).getName().equals("Two") |
getHand().get(1).getName().equals("Two") &
upCard.getValue() >= 5 & upCard.getValue() = 5 & upCard.getValue() = 4 & upCard.getValue() = 4 & upCard.getValue() = 3 & upCard.getValue() = 3 & upCard.getValue() = 4 & upCard.getValue() = 4 & upCard.getValue() = 3 & upCard.getValue() = 2 & upCard.getValue() = 2 & upCard.getValue() = 8 & upCard.getValue() = 3 & upCard.getValue() = 2 & upCard.getValue() = 2 & upCard.getValue() = 7){
hit();
}
}
else if (getHandValue() >= 13 & getHandValue() = 7){
hit();
}
}
//hand is at least 17
else{
return;
}
}
}Solution
Two words.
Lookup tables.
Here's one I randomly found on the internet:
All you gotta do is make a 2D array, populate it with the right values (enum?), and alter your
That solves your complexity issue, your implicit data storage (you're trying to algorithmically map the values of 3 variables (card 1, card 2, dealer up card) to a lot of different values), and it should be a speedup too. Plus, what are blackjack explanations doing in your code?
Note that you might have to split your lookup table in three sections: One for the doubles, one for the case where you have an ace, and one for the last case, where you need to use the total of your hand.
That said, looking at your code, there's some things that could also look a bit differently:
Seems like this could get wrapped into a method called
Same thing here: this could be
This looks like it contains a bug.
Let's rephrase it:
Notice the problem? (I'm not sure if using
Lookup tables.
Here's one I randomly found on the internet:
All you gotta do is make a 2D array, populate it with the right values (enum?), and alter your
play(Card upCard) method to use the lookup table.That solves your complexity issue, your implicit data storage (you're trying to algorithmically map the values of 3 variables (card 1, card 2, dealer up card) to a lot of different values), and it should be a speedup too. Plus, what are blackjack explanations doing in your code?
Note that you might have to split your lookup table in three sections: One for the doubles, one for the case where you have an ace, and one for the last case, where you need to use the total of your hand.
That said, looking at your code, there's some things that could also look a bit differently:
getHand().get(0).getName().equals("Ace") |
getHand().get(1).getName().equals("Ace")Seems like this could get wrapped into a method called
containsCard(String name). I'm worried about a single pipe though - Isn't it ||?getHand().get(0).getName().equals("Ace") &
getHand().get(1).getName().equals("Ace")Same thing here: this could be
boolean hasPair(). Also, isn't it &&?getHand().get(0).getName().equals("Two") |
getHand().get(1).getName().equals("Two") &
upCard.getValue() >= 5 & upCard.getValue() <= 6This looks like it contains a bug.
Let's rephrase it:
firstCard is "Two" |
secondCard is "Two" &
(dealerCard is 5 or dealerCard is 6)Notice the problem? (I'm not sure if using
| compared to || prevents this from becoming a bug, but...) || short-circuit evaluates. Basically, if firstCard is "Two" is true, then the whole if statement is true. Whoops.Code Snippets
getHand().get(0).getName().equals("Ace") |
getHand().get(1).getName().equals("Ace")getHand().get(0).getName().equals("Ace") &
getHand().get(1).getName().equals("Ace")getHand().get(0).getName().equals("Two") |
getHand().get(1).getName().equals("Two") &
upCard.getValue() >= 5 & upCard.getValue() <= 6firstCard is "Two" |
secondCard is "Two" &
(dealerCard is 5 or dealerCard is 6)Context
StackExchange Code Review Q#57832, answer score: 16
Revisions (0)
No revisions yet.