PurposeIn this homework, you will gain more experience using loops by implementing a simplified version of the card game Blackjack. If you are unfamiliar with Blackjack, you may want to read Wikipedia's Blackjack page. We also provide a short introduction to the game below. For those who are familiar with Blackjack, there are a number of rules in blackjack that we are ignoring: splits, doubling down, insurance, an Ace counting as 1 or 11 (and thus "soft" hands), and all issues dealing with money and bets. The version you submit should not have these parts included. Background -- Cards and BlackjackIf you know Blackjack, you can skip this section. A standard deck of cards has 52 cards, the cards are split evenly into four suits: spades (♠), hearts (♥), diamonds (♦) and clubs (♣). Note that two of the suits are colored red (hearts and diamonds) and two are colored black (spades and clubs). In this assignment, we will be abbreviating the suits by their first letter: "S", "H", "D", and "C", respectively. Each suit has 13 "face cards", values 1 through 13. 1 is called an Ace, 11 is called a Jack, 12 is a Queen, and 13 is a King. The picture above shows an Ace of hearts and a Jack of clubs. Blackjack is a card game, where you are the player, and the computer is the dealer. The goal of the game is to get as high a score as possible without going over 21 (going "bust"). At the beginning of each hand, the dealer shuffles the deck. Then, during the game, the dealer draws each card from the top of the deck. Card faces 2 through 10 count 2 through 10 points, respectively. Face cards (Jack, Queen, and King) count 10 points, and an Ace counts 11. (In the real game aces count 1 or 11, but that makes things too complicated, so we assume here Aces count only 11.) The dealer begins by dealing two cards to you, the player, and then two cards to itself. It shows you both of your cards, but it only shows you one of its cards. Your current score is the sum of your two cards. It then repeatedly gives you the option of drawing another card, until either you decline, hit 21 points, or you go bust (over 21 points). It then deals cards to itself. The computer must draw until it reaches at least 17 points, and must not draw any cards once its total reaches 17 (it "hits" on 16 (i.e. takes another card) and "stands'' on 17 (i.e. doesn't take another card)). If neither you nor the dealer go bust, the winner of the game is the one with the most points. If both point totals are the same, the game is a tie. You lose immediately if you go bust. This is the "house advantage", and is why casinos make money. In this case no more cards are dealt. If you do not go bust but the computer does, then you win. DesignNot surprisingly, there will need to be a number of loops in this homework -- many of the steps below require one or more loops. The game must allow the user to play multiple hands, so there is an outer loop as well. Within the program, a number of things must happen:
Your code must be in a public class named Blackjack, in a file named Blackjack.java. Our solutions required about 100 lines of code. Note that you should only ask for input (via the YesNoExtractor class) for two things: asking the the player if s/he wants another card (this may happen multiple times in a round), and asking if s/he wants to play another round. The good programming practices from HW J1 need to be in this homework as well. However, you do not have to echo input that was read via the askUser() method in the YesNoExtractor class. Class CardTo assist you a class Card has implemented. Right click on that link, select "save as", and save it to the SAME directory that your Blackjack.java file is in. The class has the following public methods.
Class YesNoExtractorThe YesNoExtractor class should be used to get user input, and supports the following methods. Right click on that link, select "save as", and save it to the SAME directory that your Blackjack.java file is in. This is the same class used in HW J3.
HintsThere are a few parts of this homework that will be a bit challenging, and some of them are addressed here. The easiest way to keep your "deck" of cards is in a Vector (if you forgot about Vectors, see HW J2). Given an int variable i, you can create a Card object via new Card(i) -- then add that to your Vector. To shuffle your deck, you can use the shuffle() method in the Collections class. Assuming your Vector is named deck, to shuffle it you would use the following statement. Collections.shuffle(deck); To verify that the deck is shuffled, you can use System.out.println (deck.toString());. Note that this and other similar debugging statements should NOT execute in your final version of the code (leaving them in and commenting them out might be a good approach here). Then you can pick cards right out of the top of the deck (meaning the front of the Vector). Recall that when you remove something from a Vector, you need to cast it back to whatever class it is (in HW J2, it was a String; here, it's a Card). Thus, to get the first card from the deck (and also remove it from the deck), you use (Card) v.remove(0), assuming v is your Vector. Also recall that you will get some compiler warnings ("uses unchecked or unsafe operations") -- you can safely ignore these. You don't have to keep track of all the cards that were dealt to the player or the dealer. Once they are printed out to the screen, you just have to update the player's total (and the dealer's total), and can then forget about the card dealt. Where to start: Start by implementing each step in succession, and testing those steps out. You can put in extra print statements to do such. For example, after you write the code to create and shuffle the deck, print out the deck and run it a few times to make sure it works. The big loop that allows the player to play multiple hands can be added in at the end. Sample Execution RunThe text in red is what was entered by the user. Note that your output doesn't have to precisely match ours, but the general idea should still be there. Let's play some blackjack! SubmissionWhen you are finished, submit the Blackjack.java file. |