Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

JAVA Program: You will make a simple game of Blackjack (also called ‘21’) for th

ID: 3602206 • Letter: J

Question

JAVA Program: You will make a simple game of Blackjack (also called ‘21’) for this Programming Assignment. Carefully read all the instructions before beginning to code. Here are the instructions: First, the dealer and player each start with two cards drawn. The goal of the game is to get as close to 21 card points as possible.Having a card total of 21 points results in a win (unless the dealer also has a total of 21 points, in which case, it is a tie). If anyone goes over 21 points, they automatically lose. If both the dealer and player get over 21 card points, it is a tie. Otherwise, whoever is closest to 21 is deemed winner. After the first two cards are drawn, the player then gets to pick whether to draw another card. This may go on for as long as the player wants, until he or she decides to quit. Make sure to also display the card to the player every time a new one is drawn, so that they may use that when making their next decision. Once the player has finished drawing cards, it’s the dealer’s turn. The dealer must draw cards until their hand’s total is at least 17. After their hand reaches a total of 17 or greater, they stop drawing cards. Before displaying the final totals for both the player and dealer, allow the user to pick whether any of their Aces drawn will count as a 1 or an 11. Finally, display the totals for each, as well as the winner of that round. Allow the player to play another round, or quit. Once the player quits, display their number of wins. Card Values: Ace 1 or 11 points 2 through 10 Their respective value Jacks, Queens, and King 10 points Please note that each type of card has an equal chance of being drawn (i.e. 1 out of 13, since there are 13 types of possible cards/values). You must have the following methods created and used within your program: 1. displayInstructions() This will be called back in the main method to simply display the instructions of Blackjack to the player. This should only be called once, at the very beginning of the game. 2. drawCard() This will be used to randomly draw a card from Ace, 2 through 10, Jack, Queen, or King. You do not have to keep track of the type of card (hearts, spades, etc.), only the value. You also don’t need to worry about having cards drawn without replacement (for example, it’s okay to draw five Jacks in this program, even though it wouldn’t be possible with a normal deck of cards). Please remember that all cards have an equal chance of being drawn. 3. determineWinner() This should return a value symbolizing the winner of that round. You may use integers, Strings, or whatever you prefer – for example, if using Strings, the three values returned could be “player”, “dealer”, or “tie”. Use this back in the main method to display the winner, and keep track of the player’s winning count if appropriate. You are also free to use any additional methods if you choose to. Sample Run: First, the dealer and player each gets two cards. The goal of the game is to get as close, or equal, to 21 as possible without going over. Whoever is closest to 21 is the winner. If anyone goes over 21, they automatically lose. After the first two cards are drawn, you may decide whether or not to draw again. Aces may either be 1 or 11. Good luck! Player draws two cards: Card 1: 6 Card 2: Ace Dealer draws two cards: Card 1: 10 Card 2: Jack Would you like to draw another card (yes or no)? yes You drew a 5. Would you like to draw another card (yes or no)? no You have 1 aces. Would you like Ace 1 to be counted as 1 or 11? 11 Your total is: 22 The dealer's total is: 20 The dealer won this turn. :( Do you want to play again (yes or no)? yes Player draws two cards: Card 1: 6 Card 2: 9 Dealer draws two cards: Card 1: 2 Card 2: 4 Would you like to draw another card (yes or no)? no The dealer drew a Jack. The dealer drew a 10. You have 0 aces. Your total is: 15 The dealer's total is: 26 The player won! Do you want to play again (yes or no)? no You have 1 wins.

Explanation / Answer

def card_value(card): """Returns the integer value of a single card.""" rank = card[0] if rank in ranks[0:-4]: return int(rank) elif rank is 'ACE': return 11 else: return 10 def hand_value(hand): """Returns the integer value of a set of cards.""" # Naively sum up the cards in the deck. tmp_value = sum(card_value(_) for _ in hand) # Count the number of Aces in the hand. num_aces = len([_ for _ in hand if _[0] is 'ACE']) # Aces can count for 1, or 11. If it is possible to bring the value of #the hand under 21 by making 11 -> 1 substitutions, do so. while num_aces > 0: if tmp_value > 21 and 'ACE' in ranks: tmp_value -= 10 num_aces -= 1 else: break # Return a string and an integer representing the value of the hand. If # the hand is bust, return 100. if tmp_value < 21: return [str(tmp_value), tmp_value] elif tmp_value == 21: return ['Blackjack!', 21] else: return ['Bust!', 100]