Question: Can someone please provide the code for the ff blackjack project? You have been given two classes from the textbook which have been slightly modi

Can someone please provide the code for the ff blackjack project?

You have been given two classes from the textbook which have been slightly modi ed to use Java ArrayLists instead of the textbook implementations of lists (you will also use Java library lists in your project if your implementation requires them). Your job is to simulate a hand of Blackjack according to the following rules. Implement your program in the main() method of a class Blackjack.java. You will NOT need to modify Card or CardDeck:

1. The goal of the game is to get the sum of cards in your hand higher than the dealer's without going over 21. Each card is worth the rank value, where face cards are worth 10 and aces are worth either 1 or 11. 2. The game begins by shuing and dealing two cards to you and two cards to the dealer. The program should reveal both cards in your hand but only the rst card of the dealer's hand (the other card, called the \hole" card, remains hidden). 3. After the initial cards are dealt, you will repeatedly ask the player if they would like to \hit" or \stay". If the player hits, the program deals them another card and repeats the query. If the player stays, this phase of the game ends. If, when the player asks to hit, the sum value of the cards in their hand exceeds 21, the game immediately ends and the player loses. 4. Once the player stays and has a hand of 21 or less, the dealer plays their hand. The rules for the dealer are that they must \hit" until the sum value in their hand is greater than or equal to 17. 5. If the player's hand is greater than the dealer's OR if the dealer goes over 21, the player wins. If the dealer's hand is greater than the player's the dealer wins. If both hands are equal, the game is a tie. Here are some tips to help you with your project: 1. Implement your code in phases and test before moving on to the next phase

2. Print out the the player's hand and the value of the hand every time it changes.

3. Clearly comment each phase of the game so that I will be able to award full credit for your last project

4. A simple way to terminate your program immediately is with the System.exit(0) command. The \0" input parameter indicates normal program termination.

5. You will have to use the enum construction as implemented in the class Card. Remember, these enumerated types can be treated just like objects, with method calls on the enum to learn more information about it.

6. Because aces may be 1 or 11, determining the value of a hand will be challenging. You should implement a separate method countHand() that takes your current hand and calculates the highest possible sum that does not exceed 21 (if all possible sums exceed 21, return the lowest, you've gone over). Think carefully about how many possible combinations of aces exist and how many you need to check in countHand().

7. Note that the Card class is designed to use graphical representations of the cards. You will not need to use this feature as this project is a command line program.

The Card.java and CardDeck.java code is below:

Card.java

package blackjack;

import javax.swing.ImageIcon;

public class Card implements Comparable

{

public enum Rank {Two(2), Three(3), Four(4), Five(5), Six(6), Seven(7), Eight(8), Nine(9),

Ten(10), Jack(10), Queen(10), King(10), Ace(11);

private int value;

private Rank(int value){

this.value = value;

}

public int getValue(){

return value;

}

}

public enum Suit {Club, Diamond, Heart, Spade}

protected final Rank rank;

protected final Suit suit;

protected ImageIcon image;

Card(Rank rank, Suit suit, ImageIcon image)

{

this.rank = rank; this.suit = suit; this.image = image;

}

public Rank getRank() { return rank; }

public Suit getSuit() { return suit; }

public ImageIcon getImage() {return image;}

@Override

public boolean equals(Object obj)

// Returns true if 'obj' is a Card with same rank

// as this Card, otherwise returns false.

{

if (obj == this)

return true;

else

if (obj == null || obj.getClass() != this.getClass())

return false;

else

{

Card c = (Card) obj;

return (this.rank == c.rank);

}

}

public int compareTo(Card other)

// Compares this Card with 'other' for order. Returns a

// negative integer, zero, or a positive integer as this object

// is less than, equal to, or greater than 'other'.

{

return this.rank.compareTo(other.rank);

}

@Override

public String toString() { return suit + " " + rank; }

}

CardDeck.Java

import java.util.Random; import java.util.ArrayList; import java.util.Iterator; import javax.swing.ImageIcon;

public class CardDeck { public static final int NUMCARDS = 52; protected ArrayList deck; protected Iterator deal; public CardDeck() { deck = new ArrayList(NUMCARDS); ImageIcon image; for (Card.Suit suit : Card.Suit.values()) for (Card.Rank rank : Card.Rank.values()) { image = new ImageIcon("support/cards/" + suit + "_" + rank + "_RA.gif"); deck.add(new Card(rank, suit, image)); } deal = deck.iterator(); }

public void shuffle() // Randomizes the order of the cards in the deck. // Resets the current deal. { Random rand = new Random(); // to generate random numbers int randLoc; // random location in card deck Card temp; // for swap of cards for (int i = (NUMCARDS - 1); i > 0; i--) { randLoc = rand.nextInt(i); // random integer between 0 and i - 1 temp = deck.get(randLoc); deck.set(randLoc, deck.get(i)); deck.set(i, temp); } deal = deck.iterator(); } public boolean hasNextCard() // Returns true if there are still cards left to be dealt; // otherwise, returns false. { return (deal.hasNext()); } public Card nextCard() // Precondition: this.hasNextCard() == true // // Returns the next card for the current 'deal'. { return deal.next(); } }

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!