Can someone please provide the code for the ff blackjack project? You have been
ID: 3719974 • Letter: C
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 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 " 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<Card>
{
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<Card> deck;
protected Iterator<Card> deal;
public CardDeck()
{
deck = new ArrayList<Card>(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();
}
}
Explanation / Answer
Below I had solved the code for blackjack and it is working fine.
Hope this helps....
Thankyou..... :)
import java.util.List; import java.util.Random; import java.util.Scanner; import java.util.ArrayList; public class blackjack { public static final int INITIAL_MONEY = 100; public static final String[] suites = {"Hearts", "Spades", "Clubs", "Diamonds"}; public static final String[] cards = {"Ace", "2", "3", "4", "5", "6", "7", "8", "9", "10", "Jack", "Queen", "King"}; public static void main(String[] arg) { Scanner console = new Scanner(System.in); List<Card> newDeck = new ArrayList<Card>(); newDeck = buildDeck(suites, cards); int money = INITIAL_MONEY; intro(money); boolean play = true; while (money > 0 && play == true) { //Running card score int playerTotal = 0; int dealerTotal = 0; //For future development (Split, Double Down, etc) List<Card> playersCards = new ArrayList<Card>(); List<Card> dealersCards = new ArrayList<Card>(); //deck setup newDeck = shuffleDeck(newDeck); int roundBet = betAmount(money, console); //Initial Deal System.out.print("First card: "); playerTotal += drawCard(newDeck, playerTotal, playersCards); System.out.print("Second card: "); playerTotal += drawCard(newDeck, playerTotal, playersCards); System.out.println(); System.out.println("Dealer showing: "); dealerTotal += drawCard(newDeck, dealerTotal, dealersCards); System.out.println("Dealer has: " + dealerTotal); //Player play portion boolean another_card = true; while (playerTotal < 21 && another_card){ another_card = Hit(playerTotal, console); if (playerTotal > 21 || playerTotal == 21 || !another_card) { break; } else { playerTotal += drawCard(newDeck, dealerTotal, dealersCards); } for(int i = 0; i < playersCards.size(); i++){ if (playersCards.get(i).isAce() && playerTotal > 21) { playerTotal -= 10; } } } //Dealer play portion while (dealerTotal < 17 && playerTotal < 21) { System.out.println("Dealer showing: " + dealerTotal); Card dealerCard = newDeck.remove(0); System.out.println("Dealer gets: "); dealerCard.printCard(); dealerTotal += dealerCard.giveValue(dealerTotal); dealersCards.add(dealerCard); //doesn't work for(int i = 0; i < dealersCards.size(); i++){ if (dealersCards.get(i).isAce() && dealerTotal > 21) { playerTotal -= 10; } } } //Decide who wins and whether to play another round System.out.println(); money += winCheck(playerTotal, dealerTotal, roundBet); play = playAgain(console, money); } } public static int drawCard(List<Card> newDeck, int playerTotal, List<Card> playersCards){ int total = 0; Card playerCard1 = newDeck.remove(0); playerCard1.printCard(); total += playerCard1.giveValue(playerTotal); playersCards.add(playerCard1); return total; } /** * Constructs a deck given the suites and cards. * @param suites * @param name * @return a constructed deck */ public static List<Card> buildDeck(String[] suites, String[] name){ List<Card> deck = new ArrayList<Card>(); for (int i = 0; i < suites.length; i++){ for (int j = 0; j < name.length; j++){ Card k = new Card(name[j], suites[i]); deck.add(k); } } return deck; } /** * Shuffles the cards so that they are in random order * @param deck of cards * @return a new shuffled deck */ public static List<Card> shuffleDeck(List<Card> deck){ List<Card> shuffledDeck = new ArrayList<Card>(); int r = 0; while (deck.size() > 0){ Random card = new Random(); r = card.nextInt(deck.size()); Card temp = deck.remove(r); shuffledDeck.add(temp); } return shuffledDeck; } /** * Checks to see if the player or the dealer won * @param total running total of player's cards * @param dealer running total of dealer's cards * @param to_play * @return the money amount that the player has */ public static int winCheck(int total, int dealer, int to_play) { int gains_losses = 0; if (total == 21) { System.out.println("You have: " + total); System.out.println("You got BlackJack! You win!"); gains_losses = 2 * to_play; } else if (total > 21) { System.out.println("You have: " + total); System.out.println("You bust"); gains_losses = -1 * to_play; } else if (total == dealer) { System.out.println("You have: " + total); System.out.println("Dealer has: " + dealer); System.out.println("Push."); gains_losses = 0; } else if (dealer > 21) { System.out.println("Dealer has: " + dealer); System.out.println("Dealer busts. You win!"); gains_losses = 2 * to_play; } else if (total < dealer) { System.out.println("You have: " + total); System.out.println("Dealer has: " + dealer); System.out.println("Dealer wins."); gains_losses = -1 * to_play; } else { System.out.println("You have: " + total); System.out.println("Dealer has: " + dealer); System.out.println("You beat the dealer!"); System.out.println("You win!"); gains_losses = 2 * to_play; } return gains_losses; } /** * The bet amount for each round, subtracting from total * @param money money for each round * @param console allow for user interaction * @return the bet on the game */ public static int betAmount(int money, Scanner console) { System.out.println("How much would you like to bet?"); int bet = Math.abs(console.nextInt()); while(bet > money || bet < 10){ if (bet < 10) { System.out.println("There is a minimum bet of $10"); } else { System.out.println("You don't have that much money."); } System.out.println("How much would you like to bet?"); bet = console.nextInt(); } return bet; } /** * To decide if they want to hit or not * @param running total of player's cards * @param console user interaction * @return true or false if the player wants to hit */ public static boolean Hit(int total, Scanner console){ boolean ans = false; System.out.println(); System.out.println("You have: " + total); System.out.println("Do you want to hit?"); String answer = console.next(); if (answer.indexOf("y") == 0 || answer.indexOf("Y") == 0) { ans = true; } else if (answer.indexOf("n") == 0 || answer.indexOf("N") == 0) { ans = false; } else { System.out.println(); ans = false; } return ans; } /** * Allows the user to decide if they are going to play again * @param console user interaction * @param money total money the player has * @return true or false if the user wants to play again */ public static boolean playAgain(Scanner console, int money){ boolean ans; System.out.println("You have: $" + money); if (money == 0) { System.out.println("You're out of money. House wins."); ans = false; return ans; } System.out.println("Do you want to play again?"); String answer = console.next(); if (answer.indexOf("y") == 0 || answer.indexOf("Y") == 0) { ans = true; return ans; } else if (answer.indexOf("n") == 0 || answer.indexOf("N") == 0) { ans = false; if (money > 100) { System.out.println("Congratulations! You won: $" + (money - 100)); } else { System.out.println("You lost: $" + (100 - money)); } return ans; } else { System.out.println(); ans = false; } return ans; } /** * Prints the introduction to the game * @param money total money that the user is given */ public static void intro(int money) { System.out.println("Welcome to BlackJack!"); System.out.println(); System.out.println("You have: $" + money); } /** * Card Object * @author acknapp */ public static class Card{ private int value; private String name; private String suite; private boolean Ace; /** * Card Constructor * @param name of card * @param suite of card */ public Card(String name, String suite){ this.name = name; this.suite = suite; this.value = determineCardValue(name); } /** * Prints the name of the card */ public void printCard(){ System.out.println(this.name + " of " + this.suite); } /** * Gives the value of the card * @return value */ public int giveValue(int playerTotal){ return this.value; } /** * Check if this card is an Ace * @return Ace */ public boolean isAce(){ return Ace; } /** * Given the name of a card, determines the value of that card * @param name the given card * @return value of the card */ private int determineCardValue(String name) throws NumberFormatException{ int value = 0; try{ value = Integer.parseInt(name.substring(0,1)); return value; } catch (NumberFormatException e){ if (name.charAt(0) == 'K' || name.charAt(0) == 'J' || name.charAt(0) == 'Q' || name.charAt(0) == '0'){ value = 10; } else if (name.charAt(0) =='A'){ value = 11; this.Ace = true; } else { value = Integer.parseInt(name.substring(0, 1)); } return value; } } } }