Question

Using a representation of the 52 cards, shuffle them 3 times. then do a radix sort...

Using a representation of the 52 cards, shuffle them 3 times. then do a radix sort base 2 to get the cards back in order. the order is ace, two,..., king with a suit order of club, diamond, spade, heart. Repeat this time using a radix base 3 sort. Please give the answer in java

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

package poker;

import java.util.Arrays;

public class PokerHandEvaluator {
  
   //use if checks (ternery operators) whitin the loops and outside you just return
   //check for that 1 edge case where A can be index 0 or 12?
   //change variable names in the for loops
  
   //Pair:
   public static boolean hasPair(Card[] cards) {
       boolean a1, a2, a3, a4;

   /*if ( cards.length != 5 )
   return(false);

   if ( hasFourOfAKind(cards) || hasFullHouse(cards) || hasThreeOfAKind(cards) || hasTwoPair(cards) )
   return(false); // The hand is not one pair (but better)   
*/
   sortByRank(cards);

   /* --------------------------------
   Checking: a a x y z
       -------------------------------- */
   a1 = cards[0].getValue() == cards[1].getValue() ;

   /* --------------------------------
   Checking: x a a y z
       -------------------------------- */
   a2 = cards[1].getValue() == cards[2].getValue() ;

   /* --------------------------------
   Checking: x y a a z
       -------------------------------- */
   a3 = cards[2].getValue() == cards[3].getValue() ;

   /* --------------------------------
   Checking: x y z a a
       -------------------------------- */
   a4 = cards[3].getValue() == cards[4].getValue();

   return( a1 || a2 || a3 || a4 );
   }
  
   //TwoPair:
   public static boolean hasTwoPair(Card[] cards) {
       boolean a1, a2, a3;

   /* if ( cards.length != 5 )
   return(false);
*/
   if ( hasFourOfAKind(cards) || hasFullHouse(cards) || hasThreeOfAKind(cards) )
   return(false); // The hand is not 2 pairs (but better)

   sortByRank(cards);

   /* --------------------------------
   Checking: a a b b x
       -------------------------------- */   
   a1 = cards[0].getValue() == cards[1].getValue() &&
       cards[2].getValue() == cards[3].getValue() ;

   /* ------------------------------
   Checking: a a x b b
       ------------------------------ */
   a2 = cards[0].getValue() == cards[1].getValue() &&
       cards[3].getValue() == cards[4].getValue() ;

   /* ------------------------------
   Checking: x a a b b
       ------------------------------ */
   a3 = cards[1].getValue() == cards[2].getValue() &&
       cards[3].getValue() == cards[4].getValue() ;

   return( a1 || a2 || a3 );
   }
  
   //ThreeOfAKind:
   public static boolean hasThreeOfAKind(Card[] cards) {
      
       boolean a1, a2, a3;

//   if ( cards.length != 5 )
//   return(false);

   sortByRank(cards); // Sort by rank first

   /* ------------------------------------------------------
   Check for: x x x a b
       ------------------------------------------------------- */
   a1 = cards[0].getValue() == cards[1].getValue() &&
       cards[1].getValue() == cards[2].getValue() &&
       cards[3].getValue() != cards[0].getValue() &&
       cards[4].getValue() != cards[0].getValue() &&
       cards[3].getValue() != cards[4].getValue() ;

   /* ------------------------------------------------------
   Check for: a x x x b
       ------------------------------------------------------- */   
   a2 = cards[1].getValue() == cards[2].getValue() &&
   cards[2].getValue() == cards[3].getValue() &&
   cards[0].getValue() != cards[1].getValue() &&
   cards[4].getValue() != cards[1].getValue() &&
   cards[0].getValue() != cards[4].getValue() ;

   /* ------------------------------------------------------
   Check for: a b x x x
       ------------------------------------------------------- */   
   a3 = cards[2].getValue() == cards[3].getValue() &&
       cards[3].getValue() == cards[4].getValue() &&
       cards[0].getValue() != cards[2].getValue() &&
       cards[1].getValue() != cards[2].getValue() &&
       cards[0].getValue() != cards[1].getValue() ;

   return( a1 || a2 || a3 );

   }
  
   //Straight:
   public static boolean hasStraight(Card [] cards) {
       int i, testRank;

/*   if ( cards.length != 5 )
   return(false);
*/
   sortByRank(cards); // Sort the poker hand by the rank of each card

   /* ===========================
   Check if hand has an Ace
   =========================== */
   if ( cards[4].getValue() == 14 )
   {
   /* =================================
   Check straight using an Ace
   ================================= */
   boolean a = cards[0].getValue() == 2 && cards[1].getValue() == 3 &&
           cards[2].getValue() == 4 && cards[3].getValue() == 5 ;
   boolean b = cards[0].getValue() == 10 && cards[1].getValue() == 11 &&
           cards[2].getValue() == 12 && cards[3].getValue() == 13 ;

   return ( a || b );
   }
   else
   {
   /* ===========================================
   General case: check for increasing values
   =========================================== */
   testRank = cards[0].getValue() + 1;

   for ( i = 1; i < 5; i++ )
   {
   if ( cards[i].getValue() != testRank )
   return(false); // Straight failed...

   testRank++; // Next card in hand
   }

   return(true); // Straight found !
   }
   }
  
   //Flush:
   public static boolean hasFlush(Card[] cards) {
/*       if ( cards.length != 5 )
   return(false); // Make sure we have 5 cards....
*/
   sortBySuit(cards); // Sort the cards by the suit values

   return( cards[0].getSuit() == cards[4].getSuit() );
  
   }
  
   //FullHouse:
   public static boolean hasFullHouse(Card[] cards) {
       boolean a1, a2;

/*   if ( cards.length != 5 )
   return(false);
*/
   sortByRank(cards); // Sort by rank first

   /* ------------------------------------------------------
   Check for: x x x y y
       ------------------------------------------------------- */
   a1 = cards[0].getValue() == cards[1].getValue() &&
       cards[1].getValue() == cards[2].getValue() &&
   cards[3].getValue() == cards[4].getValue();

   /* ------------------------------------------------------
   Check for: x x y y y
       ------------------------------------------------------- */
   a2 = cards[0].getValue() == cards[1].getValue() &&
       cards[2].getValue() == cards[3].getValue() &&
       cards[3].getValue() == cards[4].getValue();

   return ( a1 || a2 );
   }
  
   //FourOfAKind:
   public static boolean hasFourOfAKind(Card[] cards) {
       boolean a1, a2;

/*   if ( cards.length != 5 )
   return(false);
*/
   sortByRank(cards); // Sort by rank first

   /* ------------------------------------------------------
   Check for: 4 cards of the same rank
       + higher ranked unmatched card
       ------------------------------------------------------- */   
   a1 = cards[0].getValue() == cards[1].getValue() &&
       cards[1].getValue() == cards[2].getValue() &&
       cards[2].getValue() == cards[3].getValue();


   /* ------------------------------------------------------
   Check for: lower ranked unmatched card
       + 4 cards of the same rank
       ------------------------------------------------------- */   
   a2 = cards[1].getValue() == cards[2].getValue() &&
       cards[2].getValue() == cards[3].getValue() &&
       cards[3].getValue() == cards[4].getValue() ;

   return( a1 || a2 );
   }
  
   //StraightFlush:
   public static boolean hasStraightFlush(Card[] cards) {
       return hasStraight(cards) && hasFlush(cards);
   }
  
  
   //Helper Method
   public static void sortBySuit( Card[] h )
   {
   int i, j, min_j;

   /* ---------------------------------------------------
   The selection sort algorithm
   --------------------------------------------------- */
   for ( i = 0 ; i < h.length ; i ++ )
   {
   /* ---------------------------------------------------
   Find array element with min. value among
   h[i], h[i+1], ..., h[n-1]
   --------------------------------------------------- */
   min_j = i; // Assume elem i (h[i]) is the minimum

   for ( j = i+1 ; j < h.length ; j++ )
   {
   if ( h[j].getSuit() < h[min_j].getSuit())
   {
   min_j = j; // We found a smaller suit value, update min_j   
   }
   }

   /* ---------------------------------------------------
   Swap a[i] and a[min_j]
   --------------------------------------------------- */
   Card help = h[i];
   h[i] = h[min_j];
   h[min_j] = help;
   }
   }
  

   //Sort By Rank
   public static void sortByRank( Card[] h )
   {
   int i, j, min_j;

   /* ---------------------------------------------------
   The selection sort algorithm
   --------------------------------------------------- */
   for ( i = 0 ; i < h.length ; i ++ )
   {
   /* ---------------------------------------------------
   Find array element with min. value among
   h[i], h[i+1], ..., h[n-1]
   --------------------------------------------------- */
   min_j = i; // Assume elem i (h[i]) is the minimum

   for ( j = i+1 ; j < h.length ; j++ )
   {
   if ( h[j].getValue() < h[min_j].getValue() )
   {
   min_j = j; // We found a smaller rank value, update min_j   
   }
   }

   /* ---------------------------------------------------
   Swap a[i] and a[min_j]
   --------------------------------------------------- */
   Card help = h[i];
   h[i] = h[min_j];
   h[min_j] = help;
   }
   }
  
  
   // A utility function to get maximum value in arr[]
static int getMax(int arr[], int n)
{
int mx = arr[0];
for (int i = 1; i < n; i++)
if (arr[i] > mx)
mx = arr[i];
return mx;
}
  
// A function to do counting sort of arr[] according to
// the digit represented by exp.
static void countSort(int arr[], int n, int exp)
{
int output[] = new int[n]; // output array
int i;
int count[] = new int[10];
Arrays.fill(count,0);
  
// Store count of occurrences in count[]
for (i = 0; i < n; i++)
count[ (arr[i]/exp)%10 ]++;
  
// Change count[i] so that count[i] now contains
// actual position of this digit in output[]
for (i = 1; i < 10; i++)
count[i] += count[i - 1];
  
// Build the output array
for (i = n - 1; i >= 0; i--)
{
output[count[ (arr[i]/exp)%10 ] - 1] = arr[i];
count[ (arr[i]/exp)%10 ]--;
}
  
// Copy the output array to arr[], so that arr[] now
// contains sorted numbers according to curent digit
for (i = 0; i < n; i++)
arr[i] = output[i];
}
  
// The main function to that sorts arr[] of size n using
// Radix Sort
static void radixsort(int arr[], int n)
{
// Find the maximum number to know number of digits
int m = getMax(arr, n);
  
// Do counting sort for every digit. Note that instead
// of passing digit number, exp is passed. exp is 10^i
// where i is current digit number
for (int exp = 1; m/exp > 0; exp *= 10)
countSort(arr, n, exp);
}
}

Add a comment
Know the answer?
Add Answer to:
Using a representation of the 52 cards, shuffle them 3 times. then do a radix sort...
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
  • 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...

  • Give answer in typed format Question( 13.) If you are dealt two cards from a 52-card...

    Give answer in typed format Question( 13.) If you are dealt two cards from a 52-card deck without replacement, find the probability of being dealt a card that is a. A Heart. b. A King or a Diamond. c. A Queen and a Diamond. d. A Face card and a Spade. e. A red King. f. A black card g. A numeric Spade or Club. h. A red card followed by a black card i. A red card followed by...

  • Consider a standard 52-card deck of cards. In particular (for those unfamiliar with playing cards), the...

    Consider a standard 52-card deck of cards. In particular (for those unfamiliar with playing cards), the deck contains 4 aces, 4 kings, 4 queens, 4 Jacks, 4 10's, 4 94, 4 84, 4 7's, 4 6's, 4 5's, 4 4's, 4 3, and 4 2's, where for each type of card (for example ace), one of the 4 copies is of suit club, one is of suit heart, one is of suit spade, and one is of suit diamond. Consider...

  • You draw a card from a standard deck of 52 cards, seeing what the suit is...

    You draw a card from a standard deck of 52 cards, seeing what the suit is (club, diamond, heart or spade), returning the card to the deck and drawing again. What is the probability that I need exactly 5 draws to get a club for the second time?

  • 13. [3] You shuffle a deck of playing cards and the chance you get a diamond...

    13. [3] You shuffle a deck of playing cards and the chance you get a diamond card is 0.25. You repeat this process 200 times, putting the card back each time, and out of those 200 attempts you get 57 diamond cards. This demonstrates a. The law of large numbers b. The law of averages 14. [3] You shuffle a deck of playing cards and the chance you get a diamond card is 0.25. You repeat this process times, putting...

  • 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...

  • ing Tool Tools Help ModeDelay 3. Shuffle a standard 52-card deck of cards and select three...

    ing Tool Tools Help ModeDelay 3. Shuffle a standard 52-card deck of cards and select three cards. (a) What is the probability of getting at least one ace or one face card? A face card is a king (K), queen (Q), or Jack (J) in any of the four suits: &, O. 7, or ds. (b) What is the probability of getting at least one red face card or the ace of spades? Since MATLAB can repeat experiments very quickly,...

  • Suppose a standard deck of 52 playing cards is thoroughly shuffled and a single card is...

    Suppose a standard deck of 52 playing cards is thoroughly shuffled and a single card is drawn. Suppose an ace has value 1, a jack has value 11, a queen has value 12, and a king has value 13. (a) Compute P(X = x) for every real number x, when X is the value of the card drawn. (b) Suppose that Y = 1, 2, 3, or 4 when a diamond, heart, club, or spade is drawn. Compute P(Y =...

  • Question 2 [20 marks A casino is analysing a poker game where 3 cards are dealt...

    Question 2 [20 marks A casino is analysing a poker game where 3 cards are dealt to the table by the dealer and two to a player, from a standard deck of 52 cards. Suppose 2 of the 3 cards on the table are Hearts and the player is holding 2 Hearts in his hand. The dealer's next action is to deal two more cards from the deck to the table. The player is interested in getting a flush which...

  • Using a standard 52-card deck, find the following probabilities. Leave your answer as a reduced fraction....

    Using a standard 52-card deck, find the following probabilities. Leave your answer as a reduced fraction. If a card is randomly selected, what is the probability that the card is any one suit, e.g., a diamond, heart, spade, or club? If two cards are randomly selected, what is the probability of drawing a face card first, followed by drawing a numbered card? If two cards are randomly selected, what is the probability of drawing any one suit first, followed by...

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