Shuffling JAVA Write an object oriented program with the fol

Shuffling JAVA

Write an object oriented program with the following classes.

=== Deck ===

The Deck class represents a standard 52-card deck; Ace high

Each card is in one of two states -- dealt (D) and not-yet-dealt (ND)

Cards are ordered. The D and ND cards each have their own order

When created, all 52 cards are ND, and are in order, by suit (clubs 2 through A, diamonds 2 through A, hearts 2 through A, spades 2 through A)

methods:

dealOne() -- moves the top card from ND to D and returns the card

print() -- prints non-dealt cards and dealt-cards (in order, as separate lists)

  shuffle(goodness) -- goodness is a floating point number between 0 and 1.

Goodness of 0 is perfect shuffle (cards are in a completely random order). Goodness of 1 is no shuffling (deck is unchanged). As you change goodness from 0 to 1 the shuffle gets progressively worse. You can make up your own shuffle algorithm. Include a description of your shuffle algorithm in the README file.

=== Card ===

The Card class represents a single card

methods:

print() -- prints the type of card

=== Hand ===  

The Hand class represents a set of cards. From 0 to 52 cards, total

Cards are always in a definite order

methods:

print() -- prints the hand (in order)

addCard(card) -- adds a card to the hand

sortBySuit() -- sorts the cards by suit, and then by value

sortByValue() -- sorts by value, then by suit

hasStraight(len, sameSuit) -- returns true if hand contains a straight of the given length. If sameSuit is true, counts only straight with cards in the same suit (flushes); If sameSuit if not true, any straight is counted. A \"straight\" is simply when you have cards of consecutive values. For ex: A 3-card straight with sameSuit = false. The three cards may be of different suits, and it still counts as a 3-card straight if there are more than three consecutive cards. This hand has a 3-card straight:

   4 of clubs, 5 of spades, 6 of diamonds 7 of clubs, K of clubs

This hand does not:

   4 of clubs, 5 of clubs, 7 of clubs, 8 of spades, K of clubs

Write a test program called CardDealer.java that takes three commandline arguments <number of runs> <number of players> <goodness>

for ex: java CardDealer 100 5 0.5

will run the following steps 100 times

1. Create a deck of cards

2. Shuffle using the goodness factor

3. Deal 5 hands (going in order player1, player2, ... player5, player1....etc)

4. Count the number of 3 card straights.

After 100 runs, compute the chances of getting a 3 card straight (this is defined as <number of 3 card straights>/<total number of hands>). For this run total number of hands will be 500.

Run your program for goodness values of (0, 0.3, 0.5, 1, 0.9). Tabulate your results in the README file.

Solution

//Deck Class

class Deck {
private String[] SUITS = {
\"Clubs\", \"Diamonds\", \"Hearts\", \"Spades\"
};

private String[] RANKS = {
\"2\", \"3\", \"4\", \"5\", \"6\", \"7\", \"8\", \"9\", \"10\",
\"Jack\", \"Queen\", \"King\", \"Ace\"
};
  
  
private int dealtCounter;

// initialize deck
private int n = SUITS.length * RANKS.length;
private int []D = new int[52]; // D = 1 then dealt D = 0 then no-dealt
public Card[] deck = new Card[52];;
  
public Deck()
{
int cardCt = 0; // How many cards have been created so far.
for ( int suit = 0; suit <= 3; suit++ ) {
for ( int value = 1; value <= 13; value++ ) {
deck[cardCt] = new Card(value,suit);
D[cardCt] = 0;
cardCt++;
  
}
}
dealtCounter = 0;
}

// shuffle
public void shuffle(double goodness)
{
for (int i = 0; i < n; i++) {
int r = i + (int) (Math.random() * (goodness));
Card temp = deck[r];
deck[r] = deck[i];
deck[i] = temp;
}
}
public Card dealOne() {
if (dealtCounter == deck.length)
throw new IllegalStateException(\"No cards are left in the deck.\");
dealtCounter++;
return deck[dealtCounter - 1];
}

// print shuffled deck
public void print(){
for (int i = 0; i < n; i++) {
System.out.println(deck[i]);
}
}
  

}

-----------------------------------------------------------------------------------------------------------------------

//Card Class


/*
An object of class card represents one of the 52 cards in a
standard deck of playing cards. Each card has a suit and
a value.
*/


public class Card {

public final static int SPADES = 0, // Codes for the 4 suits.
HEARTS = 1,
DIAMONDS = 2,
CLUBS = 3;
  
public final static int ACE = 1, // Codes for the non-numeric cards.
JACK = 11, // Cards 2 through 10 have their
QUEEN = 12, // numerical values for their codes.
KING = 13;
  
private final int suit; // The suit of this card, one of the constants
// SPADES, HEARTS, DIAMONDS, CLUBS.
  
private final int value; // The value of this card, from 1 to 11.

public Card(int theValue, int theSuit) {
// Construct a card with the specified value and suit.
// Value must be between 1 and 13. Suit must be between
// 0 and 3. If the parameters are outside these ranges,
// the constructed card object will be invalid.
value = theValue;
suit = theSuit;
}
  
public int getSuit() {
// Return the int that codes for this card\'s suit.
return suit;
}
  
public int getValue() {
// Return the int that codes for this card\'s value.
return value;
}
  
public String getSuitAsString() {
// Return a String representing the card\'s suit.
// (If the card\'s suit is invalid, \"??\" is returned.)
switch ( suit ) {
case SPADES: return \"Spades\";
case HEARTS: return \"Hearts\";
case DIAMONDS: return \"Diamonds\";
case CLUBS: return \"Clubs\";
default: return \"??\";
}
}
  
public String getValueAsString() {
// Return a String representing the card\'s value.
// If the card\'s value is invalid, \"??\" is returned.
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 a String representation of this card, such as
// \"10 of Hearts\" or \"Queen of Spades\".
return getValueAsString() + \" of \" + getSuitAsString();
}
public void printCardType() {
System.out.println(\"Type of card \"+ getSuitAsString());
}


} // end class Card

---------------------------------------------------------------------------------------------------------------

//Hand class


/**
* An object of type Hand represents a hand of cards. The
* cards belong to the class Card. A hand is empty when it
* is created, and any number of cards can be added to it.
*/

import java.util.ArrayList;

public class Hand {

private ArrayList<Card> hand; // The cards in the hand.

/**
* Create a hand that is initially empty.
*/
public Hand() {
hand = new ArrayList<Card>();
}

/**
* Remove all cards from the hand, leaving it empty.
*/
public void clear() {
hand.clear();
}
  
//print hand
public void print() {
  
for (int i = 0; i < hand.size(); i++) {
           System.out.println(hand.get(i));
       }
  
}

/**
* Add a card to the hand. It is added at the end of the current hand.
* @param c the non-null card to be added.
* @throws NullPointerException if the parameter c is null.
*/
public void addCard(Card c) {
if (c == null)
throw new NullPointerException(\"Can\'t add a null card to a hand.\");
hand.add(c);
}

/**
* Remove a card from the hand, if present.
* @param c the card to be removed. If c is null or if the card is not in
* the hand, then nothing is done.
*/
public void removeCard(Card c) {
hand.remove(c);
}

/**
* Remove the card in a specified position from the hand.
* @param position the position of the card that is to be removed, where
* positions are starting from zero.
* @throws IllegalArgumentException if the position does not exist in
* the hand, that is if the position is less than 0 or greater than
* or equal to the number of cards in the hand.
*/
public void removeCard(int position) {
if (position < 0 || position >= hand.size())
throw new IllegalArgumentException(\"Position does not exist in hand: \"
+ position);
hand.remove(position);
}

/**
* Returns the number of cards in the hand.
*/
public int getCardCount() {
return hand.size();
}

/**
* Gets the card in a specified position in the hand. (Note that this card
* is not removed from the hand!)
* @param position the position of the card that is to be returned
* @throws IllegalArgumentException if position does not exist in the hand
*/
public Card getCard(int position) {
if (position < 0 || position >= hand.size())
throw new IllegalArgumentException(\"Position does not exist in hand: \"
+ position);
return hand.get(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.
* Note that aces are considered to have the lowest value, 1.
*/
public void sortBySuit() {
ArrayList<Card> newHand = new ArrayList<Card>();
while (hand.size() > 0) {
int pos = 0; // Position of minimal card.
Card c = hand.get(0); // Minimal card.
for (int i = 1; i < hand.size(); i++) {
Card c1 = hand.get(i);
if ( c1.getSuit() < c.getSuit() ||
(c1.getSuit() == c.getSuit() && c1.getValue() < c.getValue()) ) {
pos = i;
c = c1;
}
}
hand.remove(pos);
newHand.add(c);
}
hand = newHand;
}

/**
* Sorts the cards in the hand so that cards of the same value are
* grouped together. Cards with the same value are sorted by suit.
* Note that aces are considered to have the lowest value, 1.
*/
public void sortByValue() {
ArrayList<Card> newHand = new ArrayList<Card>();
while (hand.size() > 0) {
int pos = 0; // Position of minimal card.
Card c = hand.get(0); // Minimal card.
for (int i = 1; i < hand.size(); i++) {
Card c1 = hand.get(i);
if ( c1.getValue() < c.getValue() ||
(c1.getValue() == c.getValue() && c1.getSuit() < c.getSuit()) ) {
pos = i;
c = c1;
}
}
hand.remove(pos);
newHand.add(c);
}
hand = newHand;
}
  
public boolean hasStraight(int len, boolean sameSuit) {
if(sameSuit) {
if(hand.size() < len)
return false;
else{
for(int i=0;i<hand.size()-2;i++) {
int suit = hand.get(i).getSuit();
int j;
for(j=i+1;j<2;j++) {
if(hand.get(j).getSuit() != suit)
break;
}
if(j==2)
return true;
}
return false;
}
  
}
else {
for(int i=0;i<hand.size()-2;i++) {
int value = hand.get(i).getValue();
int j;
for(j=i+1;j<2;j++) {
if(hand.get(j).getValue() != value + 1)
break;
else
value++;
}
if(j==2)
return true;
}
return false;
}
  
}

}

-----------------------------------------------------------------------------------------------------------------------------

//CardDealer class

import java.util.Scanner;
public class CardDealer {


public static void main(String[] args) {

Scanner sc=new Scanner(System.in);
int noOfRun=sc.nextInt();
int noOfPlayers=sc.nextInt();
double goodness=sc.nextDouble();
int N=noOfRun;
int M=noOfPlayers;
int totalHands = M*N;
  
Hand hands[] = new Hand[noOfPlayers];
  
while(noOfRun > 0) {
Deck deck = new Deck(); //create Deck of cards
deck.shuffle(goodness); //deck based on goodness factor
int i=0;
while(noOfPlayers > 0)
{
hands[i] = new Hand();
hands[i].addCard(deck.deck[(int)Math.random()%52]);
Card c = deck.dealOne();
i++;
noOfPlayers--;
}
  
noOfRun--;
}
  
int count =0;
for(int i=0;i<5;i++)
{
if(hands[i].hasStraight(3,false))
count++;
}
  
System.out.println(\"chances of getting a 3 card straight: \"+ count/totalHands);

}   

} // end class CardDealer

Shuffling JAVA Write an object oriented program with the following classes. === Deck === The Deck class represents a standard 52-card deck; Ace high Each card i
Shuffling JAVA Write an object oriented program with the following classes. === Deck === The Deck class represents a standard 52-card deck; Ace high Each card i
Shuffling JAVA Write an object oriented program with the following classes. === Deck === The Deck class represents a standard 52-card deck; Ace high Each card i
Shuffling JAVA Write an object oriented program with the following classes. === Deck === The Deck class represents a standard 52-card deck; Ace high Each card i
Shuffling JAVA Write an object oriented program with the following classes. === Deck === The Deck class represents a standard 52-card deck; Ace high Each card i
Shuffling JAVA Write an object oriented program with the following classes. === Deck === The Deck class represents a standard 52-card deck; Ace high Each card i
Shuffling JAVA Write an object oriented program with the following classes. === Deck === The Deck class represents a standard 52-card deck; Ace high Each card i
Shuffling JAVA Write an object oriented program with the following classes. === Deck === The Deck class represents a standard 52-card deck; Ace high Each card i

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site