Question

Create a simplified Blackjack game using the Deck and Card classes (download the attached files to...

Create a simplified Blackjack game using the Deck and Card classes (download the attached files to start).  There is also a "TestCard" class that creates a Deck and runs some simple code. You may choose to use this file to help you get started with your game program.

1) Deal 2 cards to the "player" and 2 cards to the "dealer".  Print both the player's cards, print one of the dealer's cards. Print the total value of the player's hand.

2) Ask the player if they want to "hit", i.e., if they want another card dealt to them.  If they say yes, deal another card to the player, print the new "hand" of cards, print the new value of their hand, and check to make sure the player has not "busted" (gone over a total of 21).  If the player busts, the game is over immediately and the dealer wins.  

3) Repeatedly ask the player if they want to "hit" until they either bust or they decide to "stay" (no more cards dealt). To make the game a little simpler, if the player manages to get five cards in their hand, without busting, they automatically win ("Five Card Charlie"), end the game.

4) If the player stays without busting and fewer than five cards, print out all the dealer's cards and then repeatedly hit the dealer until the dealer's total is at least 17 or the dealer busts.  If the dealer busts, the game is over immediately and the player wins.  If the dealer's total reaches 17 or higher without busting, calculate the dealer's total and the player's total.  The player closest to 21 wins.  If there is a tie, just print that it is a tie.

Note: The "face cards", Jack, Queen, King are all worth 10 points.  The Ace is worth either 1 or 11 points. You will need to handle the totaling the Aces last after you've totaled the other cards to be able to determine if they should count as 1 or 11. They should count as 11 if they can do so without busting the hand.

Note: Just make the game with the rules as described above. Don't worry about additional Blackjack rules like splitting, doubling-down, insurance, etc. You also don't need to implement any kind of "betting", just a one-hand game and who wins the hand.

Java language

0 0
Add a comment Improve this question Transcribed image text
Answer #1

The require BlackJack Program is given below, with all the requirements.In case of any query please ask me in comments. Please Thumbs Up.

BlackJackHand.java

/**
*
* @author Vaibhav
*/

import java.util.Scanner;
import java.util.Vector;


class Deck {

private Card[] d;   
private int cUsed;
  
  
public void shuffle() {
for ( int i = 51; i > 0; i-- ) {
int rand = (int)(Math.random()*(i+1));
Card temp = d[i];
d[i] = d[rand];
d[rand] = temp;
}
cUsed = 0;
}
  
  
  
public int cardsLeft() {
return 52 - cUsed;
}
  
public Deck() {
// Create unshuffled deck
d = new Card[52];
int cardsCreated = 0;
for ( int suit = 0; suit <= 3; suit++ ) {
for ( int value = 1; value <= 13; value++ ) {
d[cardsCreated] = new Card(value,suit);
cardsCreated++;
}
}
cUsed = 0;
}
public Card dealCard() {
if (cUsed == 52)
shuffle();
cUsed++;
return d[cUsed - 1];
}
}


class Card {

//suits
public final static int SPADES = 0,HEARTS = 1,DIAMONDS = 2,CLUBS = 3;
//face cards
public final static int ACE = 1,JACK = 11,QUEEN = 12,KING = 13;
  
private final int suit;
private final int val;

public Card(int theValue, int theSuit) {
//constructing a card
val = theValue;
suit = theSuit;
}
public int getSuit() {
return suit;
}
public int getValue() {
return val;
}
  
public String getSuitAsString() {
switch ( suit ) {
case SPADES:   
return "Spades";
case HEARTS:   
return "Hearts";
case DIAMONDS:
return "Diamonds";
case CLUBS:
return "Clubs";
default:   
return "??";
}
}
  
public String getValueAsString() {
// Return a String representing the card's val.
// If the card's val is invalid, "??" is returned.
switch ( val ) {
case 1:   
return "Ace";
case 2:   
return "2";
case 3:   
return "3";
case 4:   
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 "??";
}
}
public String toString() {
return getValueAsString() + " of " + getSuitAsString();
}
}

class Hand {

private Vector h; // The cards in the h.
public Hand() {
h = new Vector();
}

public void clear() {
h.removeAllElements();
}

public void addCard(Card c) {
if (c != null)
h.addElement(c);
}

public void removeCard(Card c) {
h.removeElement(c);
}

public void removeCard(int position) {
if (position >= 0 && position < h.size())
h.removeElementAt(position);
}

public int getCardCount() {
return h.size();
}

public Card getCard(int position) {
if (position >= 0 && position < h.size())
return (Card)h.elementAt(position);
else
return null;
}

public void sortBySuit() {
Vector newH = new Vector();
while (h.size() > 0) {
int pos = 0;
Card c = (Card)h.elementAt(0);
for (int i = 1; i < h.size(); i++) {
Card c1 = (Card)h.elementAt(i);
if ( c1.getSuit() < c.getSuit() ||
(c1.getSuit() == c.getSuit() && c1.getValue() < c.getValue()) ) {
pos = i;
c = c1;
}
}
h.removeElementAt(pos);
newH.addElement(c);
}
h = newH;
}

public void sortByValue() {
Vector newH = new Vector();
while (h.size() > 0) {
int pos = 0;
Card c = (Card)h.elementAt(0);
for (int i = 1; i < h.size(); i++) {
Card c1 = (Card)h.elementAt(i);
if ( c1.getValue() < c.getValue() ||
(c1.getValue() == c.getValue() && c1.getSuit() < c.getSuit()) ) {
pos = i;
c = c1;
}
}
h.removeElementAt(pos);
newH.addElement(c);
}
h = newH;
}
public int getValue() {
  
int val;   
boolean ace;
int cards;   

val = 0;
ace = false;
cards = getCardCount();

for ( int i = 0; i < cards; i++ ) {

Card card;
int cardVal;
card = getCard(i);
cardVal = card.getValue();
if (cardVal > 10) {
cardVal = 10;
}
if (cardVal == 1) {
ace = true;   
}
val = val + cardVal;
}

if ( ace == true && val + 10 <= 21 )
val = val + 10;

return val;
}
}

public class BlackJackHand {

public static void main(String[] args) {
//creating variables
int cP,cD;
Deck d; // A deck of cards.
Card card; // A card dealt from the deck.
Hand handD; //Dealer hand.
Hand handP; // Player Hand.
int cardInHand; // Number or cards .
String flag = "hit"; //to continue.
// Create the d.
d = new Deck();
Scanner sc = new Scanner(System.in);
d.shuffle();
handP = new Hand();
cardInHand = 1;
System.out.println();
card = d.dealCard();
handP.addCard(card);
  
handD = new Hand();
card = d.dealCard();
handD.addCard(card);
  
System.out.println("\nDealer's Card : " + handD.getCard(0));
while (flag.equalsIgnoreCase("hit")) {
card = d.dealCard();
handP.addCard(card);
cardInHand++;
  
  
System.out.println("Player's Hand contains:");
for (int i = 0; i < cardInHand; i++) {
System.out.println(" " + handP.getCard(i));
}
System.out.println("Value of Player's hand is " + handP.getValue());
if (handP.getValue() > 21) {
System.out.println("Sorry! You Busted Game Over");
return;
} else {
if(cardInHand >= 5){
System.out.println("Five card Charlie.");
return;
}else{
System.out.println("\n");
System.out.println("Do you want to hit or stand?");
flag = sc.nextLine();
while (!(flag.equalsIgnoreCase("hit") || flag.equalsIgnoreCase("stand"))) {
System.out.println("Invalid Choice. Please Choose again :");
flag = sc.nextLine();
}}
}
  
}
  
cardInHand=1;
System.out.println("Now Dealer Turn");
while (handP.getValue() >= handD.getValue()) {
card = d.dealCard();
handD.addCard(card);
cardInHand++;
System.out.println("Hand contains:");
for (int i = 0; i < cardInHand; i++) {

System.out.println(" " + handD.getCard(i));
}
System.out.println("Value of hand is " + handD.getValue());
//17 value as point 4 says
if(handD.getValue() >= 17 && handD.getValue() <= 21 )
{
System.out.println("Value of Player's hand is " + handP.getValue());
System.out.println("Value of Dealer's hand is " + handD.getValue());
//closest to 21 wins
cP = 21 - handP.getValue();
cD = 21 - handD.getValue();
if(cP>cD){
System.out.println("Dealer won.");
}
else
if(cP < cD)
{
System.out.println("Player won.");
}
else
{
System.out.println("It's a TIE.");
}
return;
}
if (handD.getValue() > 21) {
System.out.println("Dealer Lost the game and Busted!.");
return;
}
}
  
sc.close();
}

}

Output

Add a comment
Know the answer?
Add Answer to:
Create a simplified Blackjack game using the Deck and Card classes (download the attached files to...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Using C++ Create a Blackjack program with the following features: Single player game against the dealer....

    Using C++ Create a Blackjack program with the following features: Single player game against the dealer. Numbered cards count as the number they represent (example: 2 of any suit counts as 2) except the Aces which can count as either 1 or 11, at the discretion of the player holding the card. Face cards count as 10. Player starts with $500. Player places bet before being dealt a card in a new game. New game: Deal 2 cards to each...

  • Need a blackjack code for my assignment its due in 3 hours: it has to include...

    Need a blackjack code for my assignment its due in 3 hours: it has to include classes and object C++. Create a fully functioning Blackjack game in three separate phases. A text based version with no graphics, a text based object oriented version and lastly an object oriented 2D graphical version. Credits (money) is kept track of throughout the game by placing a number next to the player name. Everything shown to the user will be in plain text. No...

  • 4. In one version of the game played by a dealer and a player, 2 cards...

    4. In one version of the game played by a dealer and a player, 2 cards of a standard 52-card bridge deck are dealt to the player and 2 cards to the dealer. For this exercise, assume that drawing an ace and a face card (Jack, Queen and King for each shape) is called blackjack. If the dealer does not draw a blackjack and the player does, the player wins. If both the dealer and player draw blackjack, a tie...

  • In the game of blackjack, the player is initially dealt two cards from a deck of...

    In the game of blackjack, the player is initially dealt two cards from a deck of ordinary playing cards. Without going into all the details of the game, it will suffice to say here that the best possible hand one could receive on the initial dear is a combination of an ace of any suit and any face card or 10. What is the probability that the player will be dealt this combination?

  • 2. (10 points) Suppose you are playing blackjack against a dealer. In a freshly shuffled deck, what is the probabil...

    2. (10 points) Suppose you are playing blackjack against a dealer. In a freshly shuffled deck, what is the probability that either you or the dealer is dealt a blackjack? (Recall the game of blackjack is played with each player obtaining two cards at random from a regular deck. We say these two cards form a blackjack if one card is an ace and the other card is either a 10, Jack, Queen or King) 2. (10 points) Suppose you...

  • C++ program This program involves writing a VERY simplified version of the card game War. You...

    C++ program This program involves writing a VERY simplified version of the card game War. You may know this game or not but my rules are these 1. Split the deck between player1 and player2. Only the face values matter (2-14) and not the suits 2. Each player puts a card down on the table. The higher face value wins that hand. If the card values match, you will simply indicate tie and neither player wins.The original rules would require...

  • This is a question for a stats course for science students... In one version of the...

    This is a question for a stats course for science students... In one version of the game played by a dealer and a player, 2 cards of a standard 52-card bridge deck are dealt to the player and 2 cards to the dealer. For this exercise, assume that drawing an ace and a face card (Jack, Queen and King for each shape) is called blackjack. If the dealer does not draw a blackjack and the player does, the player wins....

  • How do I start this c++ code homework? Write a code in C++ for a BlackJack...

    How do I start this c++ code homework? Write a code in C++ for a BlackJack card game using the following simplified rules: Each card has a numerical value. Numbered cards are counted at their face value (two counts as 2 points, three, 3 points, and so on) An Ace count as either 1 point or 11 points (whichever suits the player best) Jack, queen and king count 10 points each The player will compete against the computer which represents...

  • Player Class Represents a participant in the game of blackjack. Must meet the following requirements: Stores...

    Player Class Represents a participant in the game of blackjack. Must meet the following requirements: Stores the individual cards that are dealt (i.e. cannot just store the sum of the cards, you need to track which specific cards you receive) Provided methods: decide_hit(self): decides whether hit or stand by randomly selecting one of the two options. Parameters: None Returns: True to indicate a hit, False to indicate a stand Must implement the following methods: Hi!! i just need help with...

  • Please write the program in python: 3. Design and implement a simulation of the game of...

    Please write the program in python: 3. Design and implement a simulation of the game of volleyball. Normal volleyball is played like racquetball, in that a team can only score points when it is serving. Games are played to 15, but must be won by at least two points. 7. Craps is a dice game played at many casinos. A player rolls a pair of normal six-sided dice. If the initial roll is 2, 3, or 12, the player loses....

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT