Rules of Blackjack For this assignment, you will implement a video card game. Th
ID: 3764405 • Letter: R
Question
Rules of Blackjack For this assignment, you will implement a video card game. The game is a simplified version of blackjack, where some of the more complex rules have been eliminated. Blackjack is played with a "deck" (pile) of 52 cards. The front of each card shows symbols and numbers that identify the card. The back of each card is identical, so that you cannot tell which card it is by looking at the back. Each card has one of 4 possible "suits": spades, hearts, clubs, or diamonds. Each suit has its own special symbol. Spade and club cards have black symbols and numbers on the front, while heart and diamond cards have red symbols. Each card also has one of 13 possible "faces": ace (A), 2, 3, 4, 5, 6, 7, 8, 9, 10, jack (J), queen (Q), or king (K). The face of a card determines its numerical scoring value. Numbered cards are worth their number of points. Jacks, queens, and kings are all worth 10 points each. Aces can be worth 1 point or 11 points, depending on the circumstances. A player's "hand" is the set of cards the player currently holds. The score of the hand is the sum of the point values of its cards. The goal in blackjack is to acquire a hand whose score is as high as possible without going over 21. The point value of an ace card is whatever is most advantageous to the player: 1 or 11, whichever would bring the score of the player's hand closer to 21 without exceeding it. A game of blackjack may involve any number of players (but for our purposes, three or fewer), and always exactly one "dealer": the person who "deals" (hands out) the cards. Players compete against the dealer, not against each other. A player's goal in a round of blackjack is to have a higher score than the dealer, without going over 21 ("busting"). Each round of simplified blackjack proceeds as follows: The dealer shuffles the deck and deals two cards "face up" (with the front of the card showing) to each player. The dealer deals one card "face down" (with only the back showing) and one card "face up" to himself. The dealer asks the first player whether she wishes to "hit" (receive another card) or "stand" (not receive any more cards). If she chooses to stand, she will not receive any more cards this round, so her score is fixed. The game moves on to the next player. If she chooses to hit, the dealer will deal her another card, face up, and her score increases. She will then be given the option to hit or stand again. If her score exceeds 21, she has "busted" and immediately loses this round - play will continue with the next player. Essentially, the player can continue to hit until she either decides to stand, or busts. The hit-or-stand process is then repeated for each other player. Once all players are done, it is the dealer's turn to play. First, he turns his face-down card over. Then he hits until his score is 17 or higher. The dealer cannot decide to hit or stand arbitrarily. If his score is less than 17, he must hit. If it is 17 or higher, he must stand. If the dealer busts, any players who did not bust earlier win this round. Otherwise, each player's score is compared to that of the dealer. A player wins if he has a higher score than the dealer, loses if he has a lower score, and "pushes" (ties) if he has the same score. At the end of each round, the group of players can decide whether they would like to play again.
1. Complete the class definitions for Card.java and Deck.java. Write a main() method in a file called Blackjack.java that shows you have tested your Card.java and Deck.java code thoroughly. The description for the instance methods and fields are in the files attached to this assignment
2.Write a main method for blackjack.java that is the game and tests the Deck.java and Card.java. Also create a Player Class and Hand Class.
Card.java should be answered in this following format with no changes to the signatures of the methods but additional methods can be added to the .java files.
Deck.java should be formatted similarily.
Explanation / Answer
//Card.java
//Card class represents a playing card.
public class Card
{
//Declare two array of faces and suits
private String[] faces = {"Ace", "Deuce", "Three", "Four", "Five", "Six",
"Seven", "Eight", "Nine", "Ten", "Jack", "Queen", "King"};
private String[] suits = {"Hearts", "Diamonds", "Clubs", "Spades"};
private int face;
private int suit;
// two-argument constructor initializes card's face and suit
public Card(int face, int suit)
{
this.face = face;
this.suit = suit;
}
// This method retrieves the suit (spades, hearts, etc.) of this card.
public int getSuit()
{
return suit;
}
// This method retrieves the face (ace through king) of this card.
public int getFace()
{
return face;
}
//Returns face value
public int getValue()
{
return face;
}
// Return String representation of Card
public String toString()
{
return faces[face] + " of " + suits[suit];
}
} // end class Card
-----------------------------------------------
// Deckclass represents a deck of playing cards.
//Deck.java
import java.util.Random;
public class Deck
{
private Card[] deck; // array of Card objects
private int currentCard; // index of next Card to be dealt (0-51)
private static final int NUMBER_OF_CARDS = 52; // constant # of Cards
// random number generator
private static final Random randomNumbers = new Random();
// constructor fills deck of Cards
public Deck()
{
deck = new Card[NUMBER_OF_CARDS];
// create array of Card objects
currentCard = 0; // first Card dealt will be deck[0]
// fill cards with face and suit
for (int count = 0; count < deck.length; count++)
deck[count] =
new Card(count % 13, count%4);
}
// shuffle deck of Cards with one-pass algorithm
public void shuffle()
{
// next call to method dealCard should start at deck[0] again
currentCard = 0;
// for each Card, pick another random Card (0-51) and swap them
for (int first = 0; first < deck.length; first++)
{
// select a random number between 0 and 51
int second = randomNumbers.nextInt(NUMBER_OF_CARDS);
// swap current Card with random Card
Card temp = deck[first];
deck[first] = deck[second];
deck[second] = temp;
}
}
// Deal one Card
public Card dealCard()
{
// determine whether Cards remain to be dealt
if (currentCard < deck.length)
return deck[currentCard++]; // return current Card in array
else
return null; // return null to indicate that all Cards were dealt
}
//Method isEmpty that return true if the deck is empty
public boolean isEmpty()
{
return currentCard==deck.length-1;
}
} // end class DeckOfCards
-------------------------------------------
/**Tester program of Card and Deck class*/
//BlackJack.java
public class BlackJack
{
public static void main(String[] args)
{
//Create an instance of Deck class
Deck deck=new Deck();
System.out.println("Card :");
//Call dealCard
System.out.println(deck.dealCard());
//Call shuffle on deck
deck.shuffle();
System.out.println("Card :");
//Call dealCard
System.out.println(deck.dealCard());
//Call shuffle on deck
deck.shuffle();
System.out.println("Card :");
//Call dealCard
System.out.println(deck.dealCard());
System.out.println("Is Deck empty ?"+deck.isEmpty());
}
}
----------------------------------------------------
Sample Output
Card :
Ace of Hearts
Card :
King of Diamonds
Card :
Queen of Spades
Is Deck empty ?false
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.