Question
JAVA Code: Create a Blackjack game using the Deck and Card classes. There is also a TestCard class that creates a Deck and runs some
JAVA Code:
Create a Blackjack game using the Deck and Card classes. 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.
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.
2) Ask the player if they want to "hit" i.e. if they want antother card dealet to them. If they say yes, deal another card to the player, print the new "hand" of cards, 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 deal).
4) If the player stays without busting, print out all the dealer's card and then repeatdly 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 it as a tie.
Note: The "face cards", Jack, Queen, King are all worth points. The Ace is worth either 1 or 11 points.
Note: It is impossible for either the player or the dealer to get more than 13 cards because the most cards you can have without going over 21 would be 4 aces (4 one's), 4 two's, and 3 three's which is (4*1)+(4*2)+(3*3) = 21. So you can make an array of Card objects of size 13 for the player's cards and one for the dealer's cards without worrying about "filling up" the arryas. Most likely the game will end long before either the player or the dealer gets nearly this many cards.
TestCard Starter Code:
/* * a simple test program to use the Card class */
import java.util.Arrays;
public class TestCard { public static void main(String[] args) { //create a new Card and print it out // Card x = new Card(2,3); //3 of Hearts // System.out.println("x is the card: " + x); // System.out.println("suit of x is " + x.getSuit()); // System.out.println("rank of x is " + x.getRank()); //Card y = new Card(5, 1); //this is invalid because the suit is not in 0-3 range Deck d = new Deck(); //create a Deck of Card objects System.out.println(d); //shuffle the deck and print it out again d.shuffle(); System.out.println(d); Card[] player1 = new Card[5]; for (int i=0; i Card Starter Code: /* * A class to represent a playing card * Each card has a suit and a rank (using a standard deck) */ public class Card { //create global constants in this class to list all possible suits and ranks //static means the array is kept and shared at the class level, not in each object //final means the array cannot be changed after it is created, i.e., constant private static final String[] SUITS = {"Clubs", "Diamonds", "Hearts", "Spades"}; private static final String[] RANKS = {null, "A", "2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K"}; //instance variables for a specific Card object //these are final so they are NOT changed once they are created private final int suit; //integer between 0-3 representing the suit from the list above private final int rank; //integer 1-13 representing the rank from the list above /* * constructor to created the specified card using the appropriate integers * s is the suit * r is the rank */ public Card(int s, int r) { if (s >= 0 && s<=3) { suit = s; } else { throw new IllegalArgumentException("invalid suit"); } if (r >= 1 && r<=13) { rank = r; } else { throw new IllegalArgumentException("invalid rank"); } } /* * a method to return a printable representation of this Card */ public String toString() { return RANKS[rank] + " of " + SUITS[suit]; } /* * a method to return this Card's suit, as an integer * this is a standard "getter" or "accessor" method */ public int getSuit() { return suit; } /* * a method to return this Card's rank, as an integer * this is a standard "getter" or "accessor" method */ public int getRank() { return rank; } } Deck Starter Code: /* * this class represents a Deck of playing cards * uses the Card class to represent individual Card objects * the Deck will consist of the standard 52 playing cards */ import java.util.Arrays; import java.util.Random; public class Deck { private Card[] standard = new Card[52]; //create an array with 52 spots for Card objects private int nextCardLocation = 0; //location of next card that can be dealt private int numOfCardsInDeck = 52; //constructor method - initializes all 52 cards in the array //no parameters - "default" constructor" public Deck() { for (int i=0; i<=3; i++) { //loop over possible suits 0 to 3 for (int j=1; j<=13; j++) { //loop over possible ranks 1 to 13 standard[i*13+j-1] = new Card(i, j); } } } /* * return a printable representation of the Deck */ public String toString() { return Arrays.toString(standard); } /* * this method shuffles the current deck, i.e., * puts the Card objects into random order in the array * method does not return any other value (void) */ public void shuffle() { //loop over the entire deck //randomly select another card to swap with the current card Random randGen = new Random(); for (int i=0; i
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started