Back to the Blackjack Game

And then after I make this decision the game will eat up some cards. So the advantage of this is that the code is a lot smaller, right? So the hard part is still knowing what the state is. A true take on gambling. Even if you didn't write the code yourself, I think you should get credit for picking the right code to copy. So what's the sequence length if I just stay there?

More Guides

You have a lot of multiline print statements that could probably be joined together; System. Jivings 2, 9 Since you asked about the validity of the comment in shuffleArray … This is what you wrote, ignoring the initial noise: This function shuffles its elements in place. A better comment would be: Thank you for this, I did a fair amount of research to see what shuffling method would be good.

Just an additional question, my comments explaining the fisher-yates method is that correct? I don't like using code I don't understand. Indeed, your comment has some problems.

Sign up using Facebook. Sign up using Email and Password. Post as a guest Name. The first part of this method checks to see if any card in the blackjack hand has a value attribute equal to None which would mean that the card is face down.

If so, the method returns None. The next part of the method simply sums the point values of all the cards in the hand. The next part determines if the hand contains an ace. If so, the last part of the method determines if the card's point value should be 11 or 1. The last line of this section creates the property total. It returns True if the object's total property is greater than Otherwise, it returns False.

Notice that in this method, I return the result of the condition self. You can create this kind of return statement with any condition any expression actually and it often results in a more elegant method. This kind of method, which returns either True or False , is pretty common.

It's often used like here to represent a condition of an object with two possibilities, such as "on" or "off," for example.

The bust method announces that a player busts and invokes the object's lose method. The lose method announces that a player loses. The win method announces that a player wins. And the push method announces that a player pushes.

The bust , lose , win , and push methods are so simple that you may wonder why they exist. I put them in the class because they form a great skeleton structure to handle the more complex issues that arise when players are allowed to bet which they will, when you complete one of the chapter challenges at the end of the chapter. Since a dealer must hit on any hand totaling 17 or less, the method returns True if the object's total property is less than 17, otherwise it returns False.

The bust method announces that the dealer busts. The class contains the code for the main game loop in its play method. The method also creates a dealer and a deck. Polymorphism is at work here in two method calls.

The same is true in the line player. The play Method The play method is where the game loop is defined and bares a striking resemblance to the pseudocode I introduced earlier:. Each player and dealer is dealt the initial two cards. The dealer's first card is flipped to hide its value. Next, all of the hands are displayed. Let money and bet be variables of type int to represent these quantities. Then, we can write an algorithm for the main program:. Since the Blackjack game will take place in a subroutine, we need some way for the main routine to find out whether the user won.

The exercise says that the subroutine should be a function that returns a boolean value with this information. We should record the return value and test it to see whether the user won. The other point that needs some refinement is inputting the user's bet. 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. So, the algorithm can be refined.

Of course, the major part of the problem is to write the playBlackjack routine. Fortunately, the exercise gives what amounts to a fairly detailed outline of the algorithm. Things are a little complicated because the game can end at various points along the way. When this happens, the subroutine ends immediately, and any remaining steps in the algorithm are skipped.

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.

The user can draw several cards, so we need a loop. The loop ends when the user wants to "stand". In the loop, if the value of userHand goes over 21, then the whole subroutine ends.

The dealer simply draws cards in a loop as long as the value of dealerHand is 16 or less. Again, if the value goes over 21, the whole subroutine ends. In the last step, we determine the winner by comparing the values of the two hands. With these refinements, the algorithm becomes. This is ready to be translated into Java.

One point of coding is the question of how to deal a card to the user or to the dealer. If deck refers to the object of type Deck , then the function call deck. We can add the card to a hand with the addCard instance method from the Hand class.

We can do this in one step, if we want. For example, to deal two cards into each hand, we just have to say. Of course, a lot of output statements have to be added to the algorithm to keep the user informed about what is going on.