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

First, the dealer and player each start with two cards drawn. The goal of the ga

ID: 3606250 • Letter: F

Question

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.

You must have the following methods created and used within your program:

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.

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.

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.

Make your program determine uniqueness in cards while drawing it. That means, that you cannot draw a card again. If you draw the same card again, you are not eligible for extra credit. To do this, you will have to find a way to get access to all the previous drawn cards.

Explanation / Answer

public class Blackjack { public static void main(String[] args) { int money; // Amount of money the user has. int bet; // Amount user bets on a game. boolean userWins; // Did the user win the game? TextIO.putln("Welcome to the game of blackjack."); TextIO.putln(); money = 100; // User starts with $100. while (true) { TextIO.putln("You have " + money + " dollars."); do { TextIO.putln("How many dollars do you want to bet? (Enter 0 to end.)"); TextIO.put("? "); bet = TextIO.getlnInt(); if (bet < 0 || bet > money) TextIO.putln("Your answer must be between 0 and " + money + '.'); } while (bet < 0 || bet > money); if (bet == 0) break; userWins = playBlackjack(); if (userWins) money = money + bet; else money = money - bet; TextIO.putln(); if (money == 0) { TextIO.putln("Looks like you've are out of money!"); break; } } TextIO.putln(); TextIO.putln("You leave with $" + money + '.'); } // end main() static boolean playBlackjack() { // Let the user play one game of Blackjack. // Return true if the user wins, false if the user loses. Deck deck; // A deck of cards. A new deck for each game. BlackjackHand dealerHand; // The dealer's hand. BlackjackHand userHand; // The user's hand. deck = new Deck(); dealerHand = new BlackjackHand(); userHand = new BlackjackHand(); /* Shuffle the deck, then deal two cards to each player. */ deck.shuffle(); dealerHand.addCard( deck.dealCard() ); dealerHand.addCard( deck.dealCard() ); userHand.addCard( deck.dealCard() ); userHand.addCard( deck.dealCard() ); TextIO.putln(); TextIO.putln(); /* Check if one of the players has Blackjack (two cards totaling to 21). The player with Blackjack wins the game. Dealer wins ties. */ if (dealerHand.getBlackjackValue() == 21) { TextIO.putln("Dealer has the " + dealerHand.getCard(0) + " and the " + dealerHand.getCard(1) + "."); TextIO.putln("User has the " + userHand.getCard(0) + " and the " + userHand.getCard(1) + "."); TextIO.putln(); TextIO.putln("Dealer has Blackjack. Dealer wins."); return false; } if (userHand.getBlackjackValue() == 21) { TextIO.putln("Dealer has the " + dealerHand.getCard(0) + " and the " + dealerHand.getCard(1) + "."); TextIO.putln("User has the " + userHand.getCard(0) + " and the " + userHand.getCard(1) + "."); TextIO.putln(); TextIO.putln("You have Blackjack. You win."); return true; } /* If neither player has Blackjack, play the game. First the user gets a chance to draw cards (i.e., to "Hit"). The while loop ends when the user chooses to "Stand". If the user goes over 21, the user loses immediately. */ while (true) { /* Display user's cards, and let user decide to Hit or Stand. */ TextIO.putln(); TextIO.putln(); TextIO.putln("Your cards are:"); for ( int i = 0; i