But instead of this, I just tell the user to enter a bet amount of 0 if she wants to quit. A new deck of cards is used for each game. The user gets to decide again whether to Hit or Stand. End the program when the user wants to quit or when she runs out of money.
If the user loses, subtract the bet from the user's money. Break out of the loop. In outline, the game goes like this: Create and shuffle a deck of cards Create two BlackjackHands, userHand and dealerHand Deal two cards into each hand Check if dealer has blackjack if so, game ends Check if user has blackjack if so, game ends User draws cards if user goes over 21, game ends Dealer draws cards Check for winner The last three steps need to be expanded, again using the information stated in the exercise.
Discussion Let's start by designing the main program. Of course, to produce code like this, you have to make sure that you are familiar with the methods in the classes that you are using. We should record the return value and test it to see whether the user won. This is the longest and most complex program that has come up so far in the exercises.
The dealer hits on a total of 16 or less and stands on a total of 17 or more. The game goes like this. So, the algorithm can be refined. For example, I expanded the step where it says "Ask whether user wants to hit or stand" to Display all the cards in the user's hand Display the user's total Display the dealers face-up card, i.
We can add the card to a hand with the addCard instance method from the Hand class. Then, we can write an algorithm for the main program: Give user a card if userHand. I encourage you to read the entire program below and make sure that you understand it.
Give the user a card. Ask whether user wants to hit or stand if user stands: The function call userHand. Royal revolt 2 slots a casino, the dealer deals himself one card face sky casino orbit and one card face down.
The game will be a simplified gambling board funding of Blackjack as it is played in a casino. Otherwise, if the dealer's total is greater than or equal to the user's total, then the dealer wins.
The loop ends when the user wants to "stand".
The computer will act as the dealer. We need variables to represent the amount of money that the user has and the amount that the user bets on a given game.
Note that the dealer wins on a tie, so if both players have Blackjack, then the dealer wins. This algorithm can be translated into the main routine in the program given below.
To avoid having an overabundance of variables in your subroutine, remember that a function call such as userHand. The rule is that as long as the value of the dealer's hand is less than or equal to 16, the dealer Hits that is, takes another card.
If the dealer's hand has a value of 21 at this point, then the dealer wins. The exercise says that the subroutine should be a function that returns a boolean value with this information. In the last step, we determine the winner by comparing the values of the two hands. We better make sure that the user's bet is a reasonable amount, that is, something between 0 and the amount of money the user has.
We can do this in one step, if we want. When this happens, the subroutine ends immediately, and any remaining steps in the algorithm are skipped. All the user's cards are dealt face up.
The computer acts as the dealer. Write a main program that lets the user play several games of Blackjack.