Question

Write a program called ShuffleDeck. This will simulate randomly shuffling a deck of cards. The cards...

Write a program called ShuffleDeck. This will simulate randomly shuffling a deck of cards. The cards will be represented by strings found in the file deckofcards.txt. Begin by reading these strings into a queue of strings. Call a method with the signature:

public static void shuffle(Queue deck)

This method performs the following steps:

  • Compute a value called the shuffle count that is the base 2 log of the size of the deck
  • In a loop that executes shuffle count times:
    • Create two queues left and right
    • While the deck queue is not empty:
      • Dequeue a card from the deck
      • Flip a coin
      • If heads comes up enqueue that card on left otherwise enqueue it on right
    • Dequeue every card from left and enqueue it on deck
    • Do the same for right
  • return

Upon returning from the method, print out the deck, one card to a line.

deckofcards.txt is the following:

5_of_Clubs
6_of_Clubs
7_of_Clubs
8_of_Clubs
9_of_Clubs
10_of_Clubs
Jack_of_Clubs
Queen_of_Clubs
King_of_Clubs
Ace_of_Diamonds
2_of_Diamonds
3_of_Diamonds
4_of_Diamonds
5_of_Diamonds
6_of_Diamonds
7_of_Diamonds
8_of_Diamonds
9_of_Diamonds
10_of_Diamonds
Jack_of_Diamonds
Queen_of_Diamonds
King_of_Diamonds
Ace_of_Spades
2_of_Spades
3_of_Spades
4_of_Spades
5_of_Spades
6_of_Spades
7_of_Spades
8_of_Spades
9_of_Spades
10_of_Spades
Jack_of_Spades
Queen_of_Spades
King_of_Spades

JAVA, PLEASE

0 0
Add a comment Improve this question Transcribed image text
Answer #1

import java.io.File;
import java.io.FileNotFoundException;
import java.util.Queue;
import java.util.Random;
import java.util.LinkedList;
import java.util.Scanner;

public class ShuffleDeck {

   /**
   * This method simulates randomly shuffling of a deck of cards.
   *
   * @param deck
   * - Queue of cards
   */
   public static void shuffle(Queue<String> deck) {
       // Compute shuffle count
       int count = (int) (Math.log(deck.size()) / Math.log(2));

       // Random object to simulate flipping of a coin
       Random r = new Random(100);

       // Shuffle deck
       for (int i = 0; i < count; i++) {
           // Create 2 queues
           Queue<String> left = new LinkedList<String>();
           Queue<String> right = new LinkedList<String>();

           // Put cards in deck on left or right queue
           while (!deck.isEmpty()) {
               // Simulate flipping of coin
               if ((r.nextInt() % 2) == 0)
                   left.add(deck.remove());
               else
                   right.add(deck.remove());
           }

           // Dequeue every cards of left on to the deck
           while (!left.isEmpty())
               deck.add(left.remove());

           // Dequeue every cards of right on to the deck
           while (!right.isEmpty())
               deck.add(right.remove());
       }
   }

   public static void main(String[] args) {
       // Input file name
       final String fileName = "deckofcards.txt";

       try {
           // Scanner to read from the file
           Scanner inFile = new Scanner(new File(fileName));

           // Create a queue to hold strings read from the file
           Queue<String> cards = new LinkedList<String>();

           // Read file
           while (inFile.hasNextLine()) {
               // Read a line and add it to cards
               cards.add(inFile.nextLine().trim());
           }

           // Close scanner
           inFile.close();

           // Shuffle cards
           shuffle(cards);

           // Print cards
           for (String card: cards)
               System.out.println(card);

       } catch (FileNotFoundException fnfe) {
           System.out.println("File not found: " + fileName);
       }
   }
}

CODE SCREENSHOTS:

import java.io.File; import java.io.FileNotFoundException; import java.util.Queue; import java.util. Random; import java.utilwhile (!right.isEmpty()) deck.add(right.remove(); public static void main(String[] args) { // Input file name final String fi

SAMPLE OUTPUT:

10_of_Clubs Jack_of_Diamonds 7_of_Spades 5_of_Spades 9_of_Clubs Queen_of_Clubs 9_of_Spades King_of_Diamonds 5_of_Clubs 10_of_

Add a comment
Know the answer?
Add Answer to:
Write a program called ShuffleDeck. This will simulate randomly shuffling a deck of cards. The cards...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Write a class named Card which will represent a card from a deck of cards. A...

    Write a class named Card which will represent a card from a deck of cards. A card has a suit and a face value. Suits are in order from low to high: Clubs, Diamonds, Hearts and Spades. The card values from low to high: 2, 3, 4, 5, 6, 7, 8, 9, 10, Jack, Queen, King, and Ace. Write a Deck class that contains 52 cards. The class needs a method named shuffle that randomly shuffles the cards in the...

  • Deck of Cards Program I need help printing a flush, which is showing the top 5...

    Deck of Cards Program I need help printing a flush, which is showing the top 5 cards of the same suite. Below is the code I already have that answers other objectives, such as dealing the cards, and finding pairs. Towards the end I have attempted printing a flush, but I cannot figure it out. public class Shuffler {    /**    * The number of consecutive shuffle steps to be performed in each call    * to each sorting...

  • Create A Header file and A CPP File for this we want to simulate drawing cards...

    Create A Header file and A CPP File for this we want to simulate drawing cards from a deck,with or without replacement. With replacement means that the card is placed back in the deck after having been drawn. You will want to design a class that represents a deck of card (52 cards. 13 cards: Ace, 2, 3, 4, 5, 6, 7, 8, 9, 10, Jack, Queen, King) The class is to be called “aDeckOfCards”. And it should generate a...

  • Play Your Cards Right Obtain a standard deck of 52 playing cards. Mix them well and count out 25 cards WITHOUT LOOKING AT THEM. Put aside the remaining cards. You are going to perform an experime...

    Play Your Cards Right Obtain a standard deck of 52 playing cards. Mix them well and count out 25 cards WITHOUT LOOKING AT THEM. Put aside the remaining cards. You are going to perform an experiment to estimate the probablity of drawing a club, a diamond, a heart, and a spade from your deck of 25 cards. A. Mix the 25 cards well Draw one card. Record its occurrence in the approprlate box. below B. Replace the card and shuffle...

  • I have to write a program where the program prints a deck of cards. instead of having your regula...

    I have to write a program where the program prints a deck of cards. instead of having your regular suits and numbers the program will use a value for a number, id will be either rock, paper, or scissors, and the coin will be heads or tails. print example: 2 of rock heads. If the user is enters 1 the program will print out 30 of the print example and arrange them by there values. if the user enters 2...

  • Write a Java program to simulate Blackjack. The program should be as basic as possible while...

    Write a Java program to simulate Blackjack. The program should be as basic as possible while following the rules below. Thanks a. There is one deck of cards b. Cards are drawn randomly using (Shuffle() method) c. The value of a hand is computed by adding the values of the cards in hand d. The value of a numeric card such as four is its numerical value e. The value of a face card is 10 f. Ace is either...

  • help can't display the cardds Problem Description: Write a program that displays three cards randomly selected...

    help can't display the cardds Problem Description: Write a program that displays three cards randomly selected from a deck of 52, as shown in the Fig.1 below. The card image files are named 1.png, 2.png, ..., 52.png and stored in the image/card directory. All three cards are distinct and selected randomly. The image icons used in the exercises can be obtained from the book source code www.cs.armstrong.edu/liang/intro10e/book.zip file under the image folder (i.e., image/card/1.png) Fig. 1 Hint: You can select...

  • Specifications: C++ each class should have a header file all files must create a console application...

    Specifications: C++ each class should have a header file all files must create a console application in Visual Studio We want to design our die class (to be named aDie) such that we can have statements such as: aDie D; // To instantiate a Die object int rolled = D; // To get the value rolled by D rolled = D + D; // To get the value of the sum of 2 rolls of the die aDie d1, d2;...

  • We want to design our die class (to be named aDie) such that we can have...

    We want to design our die class (to be named aDie) such that we can have statements such as: aDie D; // To instantiate a Die object int rolled = D; // To get the value rolled by D rolled = D + D; // To get the value of the sum of 2 rolls of the die aDie d1, d2; // to instantiate 2 dice rolled = d1 + d2; // To get the value of the sum of...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT