Help with the DeckOfCards Class Below is a link for a sample blackjack game ( ht
ID: 3547297 • Letter: H
Question
Help with the DeckOfCards Class
Below is a link for a sample blackjack game
(http://zone.msn.com/gameplayer/gameplayer.aspx?game=blackjack&instance=default)
Premise of the Game
The basic premise of the game is that you want to have a hand value that is closer
to 21 than that of the dealer, without going over 21. Your hand is strictly played
out against the hand of the dealer. The rules of play for the dealer are strictly
dictated, leaving no decisions up to the dealer.
Values of the Cards
In blackjack, the cards are valued as follows:
An Ace can count as either 1 or 11, as explained below.
The cards from 2 through 9 are valued at their face value.
The 10, Jack, Queen, and King are all valued at 10.
The suits of the cards do not have any meaning in the game. The value of a hand
is simply the sum of the point counts of each card in the hand. For example, a
hand containing (5,7,9) has the value of 21. The Ace can be counted as either 1 or 11.
You need not specify which value the Ace has. It's assumed to always have the
value that makes the best hand. An example will illustrate: Suppose that you have
the beginning hand (Ace, 6). This hand can be either 7 or 17. If you stop there, it
will be 17. Let's assume that you draw another card to the hand and now have
(Ace, 6, 3). Your total hand is now 20, counting the Ace as 11. Let's backtrack and
assume that you had instead drawn a third card that was an 8. The hand is now
(Ace, 6, 8) which totals 15. Notice that now the Ace must be counted as only 1 to
avoid going over 21.
How the Dealer Plays His Hand
The dealer must play his hand in a specific way, with no choices allowed. There
are two popular rule variations that determine what total the dealer must draw to.
In any given casino, you can tell which rule is in effect by looking at the blackjack
tabletop. It should be clearly labeled with one of these rules:
"Dealer stands on all 17s": The dealer must continue to take cards ("hit") until his
total is 17 or greater. An Ace in the dealer's hand is always counted as 11 if
possible without the dealer going over 21. For example, (Ace,8) would be 19 and
the dealer would stop drawing cards ("stand"). Also, (Ace,6) is 17 and again the
dealer will stand. (Ace,5) is only 16, so the dealer would hit. He will continue to
draw cards until the hand's value is 17 or more. For example, (Ace,5,7) is only 13
so he hits again. (Ace,5,7,5) makes 18 so he would stop ("stand") at that point.
public class DeckOfCards {
public static void main(String[] args) {
int[] deck = new int[52];
String[] suits = {"Spades", "Hearts", "Diamonds", "Clubs"};
String[] ranks = {"Ace", "2", "3", "4", "5", "6", "7", "8", "9",
"10", "Jack", "Queen", "King"};
// Initialize cards
for (int i = 0; i < deck.length; i++)
deck[i] = i;
// Shuffle the cards
for (int i = 0; i < deck.length; i++) {
// Generate an index randomly
int index = (int)(Math.random() * deck.length);
int temp = deck[i];
deck[i] = deck[index];
deck[index] = temp;
}
// Display the first four cards
for (int i = 0; i < 4; i++) {
String suit = suits[deck[i] / 13];
String rank = ranks[deck[i] % 13];
System.out.println("Card number " + deck[i] + ": "
+ rank + " of " + suit);
}
}
}
(i) Modify the DeckOfCards class to include the following
methods:
/* Returns an integer representing a card in the deck.
* Between shuffles, the same card will never be returned twice */
(a) public static int drawCard()
/* Shuffle the "deck" of ints */
(b) public static void shuffle()
(ii) Create a Blackjack class to handle the logic of the game. At a minimum this
class will require the following methods:
/* Set up the game: perform a shuffle, "deal" 2 cards to both the
dealer & player */
(a) public static void setupNewGame()
/* Deal another card to the player */
(b) public static void hit()
/* Print the hand supplied as an int array to the console, e.g.:
* 7 of Spades, Ace of Hearts, 3 of Diamonds
* King of Clubs, 10 of Hearts
* */
(c) public static printHand(int[])
/* Expects an int array representing a blackjack hand as a
parameter.
* Returns the score from the card representation */
(d) public static int calculateScore(int[] hand)
/* Automates the dealer's play after the player stands. Dealer
draws a new card until he has 17 or greater */
(e) public static void finishDealersPlay()
/* Returns 1 if the player has won, -1 if the player
* has lost and 0 if it's a draw/push (both the player
* and the dealer have the same score)
*
*
* A player has won if:
* 1. His/her hand is <= 21 and his/her hand has a
* higher score than the dealer
* 2. His/her hand is <= 21 and the dealers goes bust
* (i.e., the dealer's score is >21)
* 3. The player gets Blackjack (21) with two cards:
* any 10 card and an Ace (except if the dealer also
* has a blackjack)
*
* Otherwise the dealer has won.
* */
(f) public int boolean calculateWinnings()
(iii) Create a TestBlackjack class that facilitates playing the game on the
command line. Users should be presented with the dealer
Explanation / Answer
//DeckOfCards.java
//BlackJack.java
//TestBlackJack.java
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.