Question

In: Computer Science

JAVA PROGRAM Overview : You will create a card deck program that allows the player to...

JAVA PROGRAM Overview :

You will create a card deck program that allows the player to shuffle a deck of cards and play a game that draws and plays cards until all cards in the deck have been used.

Instructions:

The application must be able to perform the following tasks :

1. At the start of a new game, it shuffles all cards using a deck shuffler

2. The player then draws X number of cards from the deck and 'plays' the cards by displaying them in the UI

3. The played cards are then added to a discard pile.

4. The player draws X new cards and repeats playing and discarding cards.

5. When there are no cards left in the deck, the game should ask the player of they want to play again.

-if the answer is yes, create a new deck with the discard pile and start again

-if the answer is no, end the game with a creative message to the player.

Requirements:

- The player must be able to continue to draw cards without errors.

- It should include different methods for drawing, shuffling and playing cards.

-It must include three arrays of a card, using a custom class that includes { Title: string, Description: String}

Solutions

Expert Solution

Constructor and instance methods in class Deck:

     /* Constructor.  Create an unshuffled deck of cards.*/
     public Deck()

     /*Put all the used cards back into the deck, and shuffle it into a random order.*/
     public void shuffle()

     /*
      * As cards are dealt from the deck, the number of 
      * cards left decreases.  This function returns the 
      * number of cards that are still left in the deck.
      */
     public int cardsLeft()

     /**
      * Deals one card from the deck and returns it.
      * @throws IllegalStateException if no more cards are left.
      */
     public Card dealCard()
Constructor and instance methods in class Hand:

    /**
     * Constructor. Create a Hand object that is initially empty.
     */
    public Hand()

    /**
     * Discard all cards from the hand, making the hand empty.
     */
    public void clear()

    /**
     * Add the card c to the hand.  c should be non-null.
     * @throws NullPointerException if c is null.
     */
    public void addCard(Card c)

    /**
     * If the specified card is in the hand, it is removed.
     */
    public void removeCard(Card c)

    /**
     * Remove the card in the specified position from the
     * hand.  Cards are numbered counting from zero.
     * @throws IllegalArgumentException if the specified 
     *    position does not exist in the hand.
     */
    public void removeCard(int position)

    /**
     * Return the number of cards in the hand.
     */
    public int getCardCount()

    /**
     * Get the card from the hand in given position, where 
     * positions are numbered starting from 0.
     * @throws IllegalArgumentException if the specified 
     *    position does not exist in the hand.
     */
    public Card getCard(int position)

    /**
     * Sorts the cards in the hand so that cards of the same 
     * suit are grouped together, and within a suit the cards 
     * are sorted by value.
     */
    public void sortBySuit()

    /**
     * Sorts the cards in the hand so that cards are sorted into
     * order of increasing value.  Cards with the same value 
     * are sorted by suit. Note that aces are considered
     * to have the lowest value.
     */
    public void sortByValue()

A Card object can be constructed knowing the value and the suit of the card. For example, we can call the constructor with statements such as:

card1 = new Card( Card.ACE, Card.SPADES );  // Construct ace of spades.
card2 = new Card( 10, Card.DIAMONDS );   // Construct 10 of diamonds.
card3 = new Card( v, s );  // This is OK, as long as v and s 
                           //               are integer expressions.
/**
 * An object of type Card represents a playing card from a
 * standard Poker deck, including Jokers.  The card has a suit, which
 * can be spades, hearts, diamonds, clubs, or joker.  A spade, heart,
 * diamond, or club has one of the 13 values: ace, 2, 3, 4, 5, 6, 7,
 * 8, 9, 10, jack, queen, or king.  Note that "ace" is considered to be
 * the smallest value.  A joker can also have an associated value; 
 * this value can be anything and can be used to keep track of several
 * different jokers.
 */
public class Card {
   
   public final static int SPADES = 0;   // Codes for the 4 suits, plus Joker.
   public final static int HEARTS = 1;
   public final static int DIAMONDS = 2;
   public final static int CLUBS = 3;
   public final static int JOKER = 4;
   
   public final static int ACE = 1;      // Codes for the non-numeric cards.
   public final static int JACK = 11;    //   Cards 2 through 10 have their 
   public final static int QUEEN = 12;   //   numerical values for their codes.
   public final static int KING = 13;
   
   /**
    * This card's suit, one of the constants SPADES, HEARTS, DIAMONDS,
    * CLUBS, or JOKER.  The suit cannot be changed after the card is
    * constructed.
    */
   private final int suit; 
   
   /**
    * The card's value.  For a normal card, this is one of the values
    * 1 through 13, with 1 representing ACE.  For a JOKER, the value
    * can be anything.  The value cannot be changed after the card
    * is constructed.
    */
   private final int value;
   
   /**
    * Creates a Joker, with 1 as the associated value.  (Note that
    * "new Card()" is equivalent to "new Card(1,Card.JOKER)".)
    */
   public Card() {
      suit = JOKER;
      value = 1;
   }
   
   /**
    * Creates a card with a specified suit and value.
    * @param theValue the value of the new card.  For a regular card (non-joker),
    * the value must be in the range 1 through 13, with 1 representing an Ace.
    * You can use the constants Card.ACE, Card.JACK, Card.QUEEN, and Card.KING.  
    * For a Joker, the value can be anything.
    * @param theSuit the suit of the new card.  This must be one of the values
    * Card.SPADES, Card.HEARTS, Card.DIAMONDS, Card.CLUBS, or Card.JOKER.
    * @throws IllegalArgumentException if the parameter values are not in the
    * permissible ranges
    */
   public Card(int theValue, int theSuit) {
      if (theSuit != SPADES && theSuit != HEARTS && theSuit != DIAMONDS && 
            theSuit != CLUBS && theSuit != JOKER)
         throw new IllegalArgumentException("Illegal playing card suit");
      if (theSuit != JOKER && (theValue < 1 || theValue > 13))
         throw new IllegalArgumentException("Illegal playing card value");
      value = theValue;
      suit = theSuit;
   }

   /**
    * Returns the suit of this card.
    * @returns the suit, which is one of the constants Card.SPADES, 
    * Card.HEARTS, Card.DIAMONDS, Card.CLUBS, or Card.JOKER
    */
   public int getSuit() {
      return suit;
   }
   
   /**
    * Returns the value of this card.
    * @return the value, which is one of the numbers 1 through 13, inclusive for
    * a regular card, and which can be any value for a Joker.
    */
   public int getValue() {
      return value;
   }
   
   /**
    * Returns a String representation of the card's suit.
    * @return one of the strings "Spades", "Hearts", "Diamonds", "Clubs"
    * or "Joker".
    */
   public String getSuitAsString() {
      switch ( suit ) {
      case SPADES:   return "Spades";
      case HEARTS:   return "Hearts";
      case DIAMONDS: return "Diamonds";
      case CLUBS:    return "Clubs";
      default:       return "Joker";
      }
   }
   
   /**
    * Returns a String representation of the card's value.
    * @return for a regular card, one of the strings "Ace", "2",
    * "3", ..., "10", "Jack", "Queen", or "King".  For a Joker, the 
    * string is always numerical.
    */
   public String getValueAsString() {
      if (suit == JOKER)
         return "" + value;
      else {
         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";
         default:  return "King";
         }
      }
   }
   
   /**
    * Returns a string representation of this card, including both
    * its suit and its value (except that for a Joker with value 1,
    * the return value is just "Joker").  Sample return values
    * are: "Queen of Hearts", "10 of Diamonds", "Ace of Spades",
    * "Joker", "Joker #2"
    */
   public String toString() {
      if (suit == JOKER) {
         if (value == 1)
            return "Joker";
         else
            return "Joker #" + value;
      }
      else
         return getValueAsString() + " of " + getSuitAsString();
   }
   

} // end class Card

Related Solutions

Create a java program. War is a card game for two players. A standard deck of...
Create a java program. War is a card game for two players. A standard deck of 52 cards is dealt so that both players have 26 cards. During each round of play (or "battle"), both players play a card from the top of their hand face up. The player who plays the card of the higher rank wins both cards and places them at the bottom of his stack of cards. If both cards played are of the same rank,...
Create a Java program that allows two players to play Rock, Paper, Scissors. Player 1 will...
Create a Java program that allows two players to play Rock, Paper, Scissors. Player 1 will enter an integer to determine whether they use rock, paper or scissors. Player 2 will also enter an integer to determine whether they use rock, paper or scissors. Use named constants for rock, paper and scissors and set their values as shown below. Use if-else statements to determine the results of the game. Use the named constants to compare with the player 1 and...
Create a java program that allows people to buy tickets to a concert. Using java create...
Create a java program that allows people to buy tickets to a concert. Using java create a program that asks for the users name, and if they want an adult or teen ticket. As long as the user wants to purchase a ticket the program with "yes" the program will continue. When the user inputs "no" the program will output the customer name, total amount of tickets, and the total price. The adult ticket is $60 and the child ticket...
Description: The purpose of the program is to create a Java ticket purchasing program that allows...
Description: The purpose of the program is to create a Java ticket purchasing program that allows for users to log in, purchase tickets, keep records of tickets purchased, and keep information about the user. Program Requirements: The following are the program requirements: Must be fully functioning including registration, log-in, view events, and purchase tickets Tickets should be purchased using “points”, no information should be provided via the user for payment method. Default each user to an allotted number of points...
Using Repl.it In this assignment you will create a Java program that allows a Bank customer...
Using Repl.it In this assignment you will create a Java program that allows a Bank customer to do the following: 1) Create a bank account by supplying a user id and password .2) Login using their id and password 3) Quit the program. Now if login was successful the user will be able to do the following: 1) Withdraw money. 2) Deposit money. 3) Request balance. 4) Quit the program. If login was not successful (for example the id or...
(JAVA) Create a program that creates a mini database of numbers that allows the user to:...
(JAVA) Create a program that creates a mini database of numbers that allows the user to: reset the database, print the database, add a number to the database, find the sum of the elements in the database, or quit. In main, you will declare an array of 10 integers (this is a requirement). Then you will define the following methods: • printArray (int[ ] arr) – this takes in an array and prints it • initArray (int[ ] arr) –...
Pitch Deck Overview You have been hired to create a website. In this assignment, you will...
Pitch Deck Overview You have been hired to create a website. In this assignment, you will begin to gather the necessary information to complete the work and present your ideas to your client. Instructions You will need to answer the following questions: What kind of domain names will you register (that is .com, .biz, .net, et cetera)? Why do you suggest this kind of domain? (Review the section in Week 1 on domain names.) Will you register the site using...
JAVA Exercise BasketBall Bar Chart Write a program that allows you to create a bar chart...
JAVA Exercise BasketBall Bar Chart Write a program that allows you to create a bar chart for the active members of a basketball team during a game. (Recall: there are only 5 active players on the court per team in a standard basketball game.) Your program should do the following tasks: • Prompt the user to store the first name of the five players • Prompt the user to enter the points scored by each player a. Use a while...
Create a C# application for Cricket Team that allows you to enter data for player of...
Create a C# application for Cricket Team that allows you to enter data for player of Cricket Team and saves the data to a file named Players.txt. Create a Player class that contains fields for of participant's first name, last name, age, salary and position (wicketkeeper, bawler, striker, midwicket), and To String() method. The fields of records in the file are separated with semicolon (. Expecting sentinel value for ending the process of writing to file is *. E.g. of...
C++ Question Create a class for a card in a deck of playing cards. The object...
C++ Question Create a class for a card in a deck of playing cards. The object must contain methods for setting and retrieving the suit and the type of card (type of card meaning 2,3,4,5,6,7,8,9,10,J,Q,K,A). Separate the declaration of the class from its implementation by using the declaration in a header file and the implementation in a .cpp file. Also create a driver program and a makefile which complies the code and makes it able to run. This is more...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT