Question
Create a BlackjackApp that allows the user to play Blackjack against the computer, also serving as the dealer. The game of blackjack is a popular casino card game that is often referred to as twenty-one. To simplify this assignment,please ignore betting and complicated rules. The objective of this assignment is to implement the basic rules of the game itself.
The Game
The game starts by dealing two cards to the player and the dealer from the top of the deck.
The player’s cards are both revealed to the player. The dealer received the first card face down (hidden from the player) and the second card is face-up.
To win the game, the player can add cards to his hand by typing “hit”. The goal is for the player to get a hand value as close to a total of 21 as possible, but do not exceed 21, and not less than the value of the dealer’s hand. If the player’s hand value exceeds 21, he or she immediately loses. The player signals he is done adding cards to his hand by typing “stay”.
Once the player has ended his turn, and the dealer then takes his turn by adding extra cards to his hand to get as close to 21 as possible. If the sum of the dealer’s cards exceeds 21, the player wins. If the player and the dealer tie, the hand is a draw.
The following is an example of a game execution: GAME #1 B LACK J A CK Player Hand of 2 cards: QUEEN of CLUBS SIX of HEARTS facedown FIVE of SPADES Dealer's 1st card: Dealer's 2nd card: Player, your hand value is 16. Would you like to hit or stay? hit Card added: QUEEN of HEARTS CARD PLAY OVER Player: With a hand value of 26, the cards in your hand are: QUEEN of CLUBS SIX of HEARTS QUEEN of HEARTS Dealer:Hand value of 23 with cards: NINE of SPADES FIVE of SPADES NINE of DIAMONDS You exceeded 21. You lose Would you like to play another game? Enter yes to continue. yes BLA C K J A C K GAME #2 Player Hand of 2 cards: SEVEN of SPADES THREE of SPADES facedown JACK of CLUBS Dealer's 1st card: Dealer's 2nd card: Player, your hand value is 10. Would you like to hit or stay? hit Card added: TEN of DIAMONDS Player, your hand value is 20. Would you like to hit or stay? stay CARD PLAY OVER Player: With a hand value of 20, the cards in your hand are: SEVEN of SPADES THREE of SPADES TEN of DIAMONDS Dealer: Hand value of 20 with cards: QUEEN of HEARTS ACK of CLUBS Player, you have tied with the dealer Would you like to play another game? Enter yes to continue. ho
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