BLACKJACK Here is a program for all you High Rollers: In this assignment you wil
ID: 3590065 • Letter: B
Question
BLACKJACK Here is a program for all you High Rollers: In this assignment you will simulate a Blackjack game (also known as "21"). Your menu should look like this: 1. New Deck 2. Shuffle cards in Deck 3. Display all cards remaining in the deck. 4. Play Black Jack. 5. Exit Option 1 will create a new deck of playing cards. This deck of cards will be an array (please use an ArrayList - not an array) of 52 cards. The order of the cards will be the same each time option one is selected. Option 2 will shuffle the cards in preparation for playing the game of Black Jack. This shuffle should mix the cards up in a random order. Also, it should shuffle however many cards are in the deck. After selecting a "New Deck" there will be 52 cards. After playing one hand of Black Jack, there will be fewer cards in the deck. You should be able to shuffle just the remaining cards. Option 3 will display the all cards that are still in the deck. Option 4 will play the game. Here are the rules of BlackJack The game is played with two players which are the computer against the player. We will refer to the computer as the "Dealer". The object is to get as close as possible to 21 without going over except for face cards which have the following values: d also try to beat the dealer's hand. The value of each card is the numeric value of the card Jack 10 Queen 10 King 11 or Ace 11 or 1 Rules of the game: . The dealer deals one card to the player, face up and one card to himself face down. Then he deals one card to the player face up and one card to himself face up. Then he offers a card to the player. The player may accept cards from the dealer, or he may "stand" (stand means that he takes no more cards). If the player gets more than 21 points, he loses immediately. Whern the player stops taking cards, the dealer turns over his face down card and begins to lay down cards for himself. The dealer must continue to take cards until he gets 17 points or more, at which time he stops. If the dealer goes over 21, the player wins. If both the player and the dealer are under 21, then the one who is closest to 21 wins. If there is a tie, the player wins. . .Explanation / Answer
CardDeck is class is initialized for the card and deck.
public class CardDeck {
public Card(){
public final static int SPADES = 0,
HEARTS = 1,
DIAMONDS = 2,
CLUBS = 3;
public final static int ACE = 1,JACK = 11,QUEEN = 12, KING = 13; //constant values should be declared as Final
private final int suit;
private final int value;
public Card(int theValue, int theSuit) {
value = theValue;
suit = theSuit;
}
public int getSuit() {
return suit;
}
public int getValue() {
return value;
}
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() {
switch ( value ) {
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();
}
}
public Deck() {
private Card[] deck;
private int cardsUsed;
public Deck() {
deck = new Card[52];
int cardCt = 0;
for ( int suit = 0; suit <= 3; suit++ ) {
for ( int value = 1; value <= 13; value++ ) {
deck[cardCt] = new Card(value,suit);
cardCt++;
}
}
cardsUsed = 0;
}
public void shuffle() {
for ( int i = 51; i > 0; i-- ) {
int rand = (int)(Math.random()*(i+1));
Card temp = deck[i];
deck[i] = deck[rand];
deck[rand] = temp;
}
cardsUsed = 0;
}
public int cardsLeft() {
return 52 - cardsUsed;
}
public Card dealCard() {
if (cardsUsed == 52)
shuffle();
cardsUsed++;
return deck[cardsUsed - 1];
}
}
}
-----------------------------
Hand class is for deck in the hand and position wise arrang the cards.
import java.util.Vector;
public class PlayHand {
private Vector hand;
public Hand() {
hand = new Vector();
}
public void clear() {
hand.removeAllElements();
}
public void addCard(Card c) {
if (c != null)
hand.addElement(c);
}
public void removeCard(Card c) {
hand.removeElement(c);
}
public void removeCard(int position) {
if (position >= 0 && position < hand.size())
hand.removeElementAt(position);
}
public int getCardCount() {
return hand.size();
}
public Card getCard(int position) {
if (position >= 0 && position < hand.size())
return (Card)hand.elementAt(position);
else
return null;
}
public void sortBySuit() {
Vector newHand = new Vector();
while (hand.size() > 0) {
int pos = 0;
Card c = (Card)hand.elementAt(0);
for (int i = 1; i < hand.size(); i++) {
Card c1 = (Card)hand.elementAt(i);
if ( c1.getSuit() < c.getSuit() ||
(c1.getSuit() == c.getSuit() && c1.getValue() < c.getValue()) ) {
pos = i;
c = c1;
}
}
hand.removeElementAt(pos);
newHand.addElement(c);
}
hand = newHand;
}
public void sortByValue() {
Vector newHand = new Vector();
while (hand.size() > 0) {
int pos = 0;
Card c = (Card)hand.elementAt(0);
for (int i = 1; i < hand.size(); i++) {
Card c1 = (Card)hand.elementAt(i);
if ( c1.getValue() < c.getValue() ||
(c1.getValue() == c.getValue() && c1.getSuit() < c.getSuit()) ) {
pos = i;
c = c1;
}
}
hand.removeElementAt(pos);
newHand.addElement(c);
}
hand = newHand;
}
}
-------------------------------------------------------------------
public class Blackjack extends PlayHand {
public int getBlackjackValue() {
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;
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.