Now its the dealers chance to draw Dealer draws cards until the dealers total

Now its the dealers chance to draw dealer draws cards

This preview shows page 13 - 17 out of 27 pages.

Now, it\'s the dealer\'s chance to draw. Dealer draws cards until the dealer\'s total is > 16. */ System.out.println(); System.out.println(\"User stands.\"); System.out.println(\"Dealer\'s cards are\"); System.out.println(\" \" + showCard(getCard(dealerHand, 0))); System.out.println(\" \" + showCard(getCard(dealerHand, 1))); while (value(dealerHand) <= 16) { int newCard = dealCard(); System.out.println(\"Dealer hits and gets the \" + showCard(newCard)); dealerHand.addElement(newCard); } System.out.println(\"Dealer\'s total is \" + value(dealerHand)); /* Now, the winner can be declared. */ System.out.println(); if (value(dealerHand) > 21) { System.out.println(\"Dealer busted by going over
Background image
21. You win.\"); return true; } else { if (value(dealerHand) == value(userHand)) { System.out.println(\"Dealer wins on a tie. You lose.\"); return false; } else { if (value(dealerHand) > value(userHand)) { System.out.println(\"Dealer wins, \" + value(dealerHand) + \"points to \" + value(userHand) + \".\"); return false; } else { System.out.println(\"You win, \" + value(userHand) + \" points to \" + value(dealerHand) + \".\"); return true; } } } } // end playBlackjack() public int dealCard() { // Deals one card from the deck and returns it. if (currentPosition == 52) { shuffle(); } currentPosition++; return deck[currentPosition - 1]; }public void shuffle() { // Put all the used cards back into the deck, and shuffle it into // a random order. for (int i = 51; i > 0; i--) { int rand = (int) (Math.random() * (i + 1)); int temp = deck[i]; deck[i] = deck[rand]; deck[rand] = temp; } currentPosition = 0; } public int getCard(Vector hand, int position) { // Get the card from the hand in given position, where positions // are numbered starting from 0. If the specified position is // not the position numberof a card in the hand, then null // is returned. if (position >= 0 && position < hand.size()) { return ((Integer)hand.elementAt(position)).intValue(); } else
Background image
{ return 0; } } public int value(Vector hand) { // Returns the value of this hand for the // game of Blackjack. int val; // The value computed for the hand. boolean ace; // This will be set to true if the // hand contains an ace. int cards; // Number of cards in the hand. val = 0; ace = false; cards = hand.size(); for (int i = 0; i < cards; i++) { // Add the value of the i-th card inthe hand. int card; // The i-th card; int cardVal; // The blackjack value of the i-th card. card = getCard(hand, i); cardVal = getCardValue(card); // The normal value, 1 to 13. if (cardVal > 10) { cardVal = 10; // For a Jack, Queen, or King. } if (cardVal == 1) { ace = true; // There is at least one ace. } val = val + cardVal; } // Now, val is the value of the hand, counting any ace as 1. // If there is an ace, and if changing its value from 1 to // 11 would leave the score less than or equal to 21, // then do so by adding the extra 10 points to val. if (ace == true && val + 10 <= 21) { val = val + 10; } return val; } public int getCardValue(int card) { int result = card; switch (card) { case 11: case12: case 13: result = 10; } return result; } public String showCard(int card) { switch (card) { case 1: return \"Ace\"; case 2: return \"2\"; case 3: return \"3\"; case 4:
Background image
return \"4\"; case 5: return \"5\"; case 6: return \"6\"; case 7: return \"7\"; case 8: return \"8\"; case 9: return \"9\"; case 10: return \"10\"; case 11: return \"Jack\"; case 12: return \"Queen\"; case 13: return \"King\"; default: return \"??\"; } } } // end class SolutionHere is my code about this program. If not this let me know will send you the other one toopublic class BlackJackGame {// Contains the players for comparing who won.
Background image
Image of page 17

You've reached the end of your free preview.

Want to read all 27 pages?

  • Fall '19
  • Val, Playing card

  • Left Quote Icon

    Student Picture

  • Left Quote Icon

    Student Picture

  • Left Quote Icon

    Student Picture