Blackjack, also known as twenty-one, is the most widely played casino game in the world.
The basic rules of Blackjack are (there are several variations/extensions of these rules)
- The goal of blackjack is to beat the dealer's hand without going over 21.
- Face cards are worth 10. Aces are worth 1 or 11, whichever makes a better hand. All other cards are worth their numeric value.
- Each player starts with two cards, one of the dealer's cards is hidden until the end. The player and dealer add the value of their two cards to figure out the value they have.
- To 'Hit' is to ask for another card. To 'Stand' is to hold your total and end your turn.
- If a player goes over 21 he/she busts, and the dealer wins regardless of the dealer's hand.
- If a player scores lower than the dealer then he/she loses his/her bet.
- If the player scores higher than the dealer or the dealer goes over 21, and the player does not go over 21, then the player wins his bet (in addition to keeping his original wager).
- If the player is dealt 21 from the start (Ace & 10), he/she has a blackjack.
- Blackjack usually means the player wins 1.5 the amount of his/her bet. Depends on the casino.
- If both dealer and player have a blackjack, then the hand is a push. Note that if the dealer has a blackjack, and the player has a 21 (but does not have blackjack), then the dealer wins, and the player loses his wager.
- Dealer will hit until his/her cards total 17 or higher.
Imagine we would like to know the amount of money a player wins or loses in a hand. We know the bet the player put on, the dealer and player's score and whether the dealer and the player have a blackjack or not.
There are only four possibilities based on the rules above:
- If the player has a blackjack and the dealer does not have a blackjack the player wins 3/2*bet.
- If the player and the dealer both have a blackjack the player keeps his wager and neither side loses, so the player wins 0.
- If the player goes over 21, he or she loses -bet.
- If the player scores higher than the dealer or the dealer goes over 21 (but the player does not go over 21) the player wins +bet.
To avoid rounding issues when the player wins with blackjack, lets assume that the bet is always an even number of dollars. Then the four possible outcomes can be translated to a set of if statements in Python as follows
def blackjackWinsAndLosses(self, bet, dealerScore, playerScore, dealerHasBlackjack, playerHasBlackjack): # player loses if playerScore > 21 or (playerScore < dealerScore and dealerScore<=21): return -bet # neither player nor dealer win if dealerHasBlackjack and playerHasBlackjack: return 0 # dealer has blackjack but player does not, player loses if dealerHasBlackjack and not playerHasBlackjack: return -bet # player has blackjack but dealer does not if playerHasBlackjack and not dealerHasBlackjack: return 3/2.0*bet # dealer has more than 21 or player's score is higher than dealer if dealerScore > 21 or playerScore > dealerScore: return bet return 0The only tricky part of this problem is taking into account all the conditions correctly.
For example, if we exchanged the last if statement with the first if statement we would get an incorrect result if the dealer had a blackjack (resulting in a loss for the player instead of a win).
Another potential source of confusion is if both player and dealer have more than 21. In that case the player loses even if its score is higher than the dealer's score.
The following Python code gathers all the pieces and tests the method above.
I am always searching online for articles that can help me. There is obviously a lot to know about this. I think you made some good points in Features also. Keep working, great job!
ReplyDeletem88 blackjack
I am glad you liked the post. Thanks for the compliments.
DeleteJenny