Write a Java application that allows a user to play Blackjack against the comput
ID: 3654600 • Letter: W
Question
Write a Java application that allows a user to play Blackjack against the computer. That is, the computer will act as the house, dealing the cards and paying when you win. There is a wealth of information on-line about Blackjack, two sites among many that may be useful here are: http://www.blackjackinfo.com and http://casinogambling.about.com/od/blackjack/a/Blackjack101.htm Now, Blackjack comes in many flavors. There is a general set of rules along with optional plays for both the player and dealer. You can implement many possible variations but you must be clear in stating exactly what is and what is not allowed. At the very least you MUST use the following rules: General:Explanation / Answer
/** * This program lets the user play Blackjack. The computer * acts as the dealer. The user has a stake of $100, and * makes a bet on each game. The user can leave at any time, * or will be kicked out when he loses all the money. * House rules: The dealer hits on a total of 16 or less * and stands on a total of 17 or more. Dealer wins ties. * A new deck of cards is used for each game. */ 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 run out of money!"); break; } } TextIO.putln(); TextIO.putln("You leave with $" + money + '.'); } // end main() /** * Let the user play one game of Blackjack, with the computer as dealer. * @return true if the user wins the game, false if the user loses. */ static boolean playBlackjack() { 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; iRelated Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.