Question
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
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 procedure.
*/
private static final int SHUFFLE_COUNT = 1; // Determines amount of times that the numbers will shuffle
private static final int[] VALUE_COUNT = {0,1,2,3}; // Determines amount of times that the numbers will shuffle
/**
* Tests shuffling methods.
* @param args is not used.
*/
public static void main(String[] args) {
System.out.println("Results of " + SHUFFLE_COUNT +
" consecutive perfect shuffles:");
int[] values1 = VALUE_COUNT; // This array has the numbers that will be shuffled. The numbers can be changed to anything. This particular array applies to the perfect shuffle.
for (int j = 1; j <= SHUFFLE_COUNT; j++) { // Prints out the perfect shuffle
perfectShuffle(values1);
System.out.print(" " + j + ":");
for (int k = 0; k < values1.length; k++) {
System.out.print(" " + values1[k]);
}
System.out.println();
}
System.out.println();
System.out.println("Results of " + SHUFFLE_COUNT +
" consecutive efficient selection shuffles:");
int[] values2 = VALUE_COUNT;// This array is used for the selection shuffle. The numbers in the array can be changed to anything
for (int j = 1; j <= SHUFFLE_COUNT; j++) {
selectionShuffle(values2);
System.out.print(" " + j + ":");
for (int k = 0; k < values2.length; k++) { // Prints out the numbers after they are shuffled
System.out.print(" " + values2[k]);
}
System.out.println();
}
System.out.println();
}
/**
* Apply a "perfect shuffle" to the argument.
* The perfect shuffle algorithm splits the deck in half, then interleaves
* the cards in one half with the cards in the other.
* @param values is an array of integers simulating cards to be shuffled.
*/
public static void perfectShuffle(int[] values) { // Intertwines all the cards in the deck by splitting deck in half and mixing the two halves.
/* *** TO BE IMPLEMENTED IN ACTIVITY 3 *** */
int [] hold = new int [values.length]; // Creates new array to hold the temporary values that the method will use when it shuffles
int left = 0; // The left side of the deck will start at the first card
int right = values.length/2; // Right side will be the entire deck split in half
for(int i = 0; i hold[i]= values[left]; // replaces the new shuffled value in the hold array hold[i+1] = values[right]; left++; // moves to next card right++; } for(int i = 0; i values[i] = hold[i]; // replaces all of the ordered cards into the newly shuffled array } } /** * Apply an "efficient selection shuffle" to the argument. * The selection shuffle algorithm conceptually maintains two sequences * of cards: the selected cards (initially empty) and the not-yet-selected * cards (initially the entire deck). It repeatedly does the following until * all cards have been selected: randomly remove a card from those not yet * selected and add it to the selected cards. * An efficient version of this algorithm makes use of arrays to avoid * searching for an as-yet-unselected card. * @param values is an array of integers simulating cards to be shuffled. */ public static void selectionShuffle(int[] values) { // Chooses a random card and puts it at the end of the list. Then takes another card with the remaining ones and puts that next to the previous one. /* *** TO BE IMPLEMENTED IN ACTIVITY 3 *** */ for(int i = values.length-1; i>0; i--){ // Runs through the loop for each of the cards int position = (int) (Math.random() * i); // Selects a random card from the pile int temp = values[position]; values[position] = values[i]; values [i] = temp; // replaces the temporary value with the card that was selected } } } public class Deck{ /** * * cards contains all the cards in the deck. * */ private List /** * * size is the number of not-yet-dealt cards. * * Cards are dealt from the top (highest index) down. * * The next card to be dealt is at size - 1. * */ private int size; /** * * Creates a new * * It pairs each element of ranks with each element of suits, * * and produces one of the corresponding card. * * @param ranks * is an array containing all of the card ranks. * * @param suits * is an array containing all of the card suits. * * @param values * is an array containing all of the card point values. * */ public Deck(String[] ranks, String[] suits, int[] values) { /* *** TO BE IMPLEMENTED IN ACTIVITY 2 *** */ cards = new ArrayList for (int i = 0; i < suits.length; i++) { for (int j = 0; j < ranks.length; j++) { cards.add(new Card(ranks[j], suits[i], values[j])); } } size = cards.size(); shuffle(); } /** * * Determines if this deck is empty (no undealt cards). * * @return true if this deck is empty, false otherwise. * */ public boolean isEmpty() { /* *** TO BE IMPLEMENTED IN ACTIVITY 2 *** */ if (size > 0) return false; else return true; } /** * * Accesses the number of undealt cards in this deck. * * @return the number of undealt cards in this deck. * */ public int size() { /* *** TO BE IMPLEMENTED IN ACTIVITY 2 *** */ return size; } /** * * Randomly permute the given collection of cards * * and reset the size to represent the entire deck. * */ public void shuffle() { /* *** TO BE IMPLEMENTED IN ACTIVITY 4 *** */ for (int i = cards.size() - 1; i > 0; i--) { int t = (int) Math.round(Math.random() * i); Card temp = cards.get(i); cards.set(i, cards.get(t)); cards.set(t, temp); } size = cards.size(); } /** * * Deals a card from this deck. * * @return the card just dealt, or null if all the cards have been * * previously dealt. * */ public Card deal() { /* *** TO BE IMPLEMENTED IN ACTIVITY 2 *** */ if (isEmpty()) return null; else { size--; return cards.get(size); } //if the deck is empty a null value is returned and if it is not then the cards are dealt again } /** * * Generates and returns a string representation of this deck. * * @return a string representation of this deck. * */ @Override public String toString() { String rtn = "size = " + size + " Undealt cards: "; //sets up a string to print out the number of undealt cards for (int k = size - 1; k >= 0; k--) { rtn = rtn + cards.get(k); //adds the cards to the string to be printed out if (k != 0) { rtn = rtn + ", "; } if ((size - k) % 2 == 0) { // Insert carriage returns so entire deck is visible on console. rtn = rtn + " "; } } rtn = rtn + " Dealt cards: "; for (int k = cards.size() - 1; k >= size; k--) { rtn = rtn + cards.get(k); if (k != size) { rtn = rtn + ", "; } if ((k - cards.size()) % 2 == 0) { // Insert carriage returns so entire deck is visible on console. rtn = rtn + " "; } } rtn = rtn + " "; return rtn; } } public class Card { /** * String value that holds the suit of the card */ private String suit; /** * String value that holds the rank of the card */ private String rank; /** * int value that holds the point value. */ private int pointValue; /** * Creates a new Card instance. * * @param cardRank * a String value containing the rank of the card * * @param cardSuit * a String value containing the suit of the card * * @param cardPointValue * an int value containing the point value of the card */ public Card(String cardRank, String cardSuit, int cardPointValue) { /* *** TO BE IMPLEMENTED IN ACTIVITY 1 *** */ this.rank = cardRank; this.suit = cardSuit; this.pointValue = cardPointValue; } /** * Accesses this Card's suit. * * @return this Card's suit. */ public String suit() { /* *** TO BE IMPLEMENTED IN ACTIVITY 1 *** */ return suit; } /** * Accesses this Card's rank. * * @return this Card's rank. */ public String rank() { /* *** TO BE IMPLEMENTED IN ACTIVITY 1 *** */ return rank; } /** * Accesses this Card's point value. * * @return this Card's point value. */ public int pointValue() { /* *** TO BE IMPLEMENTED IN ACTIVITY 1 *** */ return pointValue; } /** * Compare this card with the argument. * * @param otherCard * the other card to compare to this * * @return true if the rank, suit, and point value of this card are equal to * those of the argument; false otherwise. * */ public boolean matches(Card otherCard) { /* *** TO BE IMPLEMENTED IN ACTIVITY 1 *** */ if (this.pointValue == otherCard.pointValue && this.suit.equals(otherCard.suit) && this.rank.equals(otherCard.rank)) return true; else return false; } /** * Converts the rank, suit, and point value into a string in the format * "[Rank] of [Suit] (point value = [PointValue])". This provides a useful * way of printing the contents of a Deck in an easily readable format or * performing other similar functions. * * @return a String containing the rank, suit, and point value of the card. */ @Override public String toString() { /* *** TO BE IMPLEMENTED IN ACTIVITY 1 *** */ return rank + " of " + suit + " (point value = " + pointValue + ")"; } } -------------------------------------------------------------------------- import java.lang.reflect.Array; import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; import java.util.Scanner; public class Hand { static ArrayList hand = new ArrayList(); static String [] ranks = {"Ace", "2", "3","4","5","6","7","8","9","10","Jack","Queen","King"}; // an array with all of the ranks static String [] suits = {"spades", "hearts", "clubs", "diamonds"}; // can array with all of the suits static int [] values = {1,2,3,4,5,6,7,8,9,10,11,12,13}; // Puts a point value for each of the cards public static void main(String args[]) { Scanner scan = new Scanner(System.in); // Shuffles deck and ask user how many cards int cards = 52; Deck deck = new Deck(ranks, suits, values); // puts in the format of ranks suits and then values deck.shuffle(); // uses the shuffle method to shuffle the deck System.out.println("How many cards should be dealt?: "); cards = scan.nextInt(); //takes the users input and put it into a while loop to deal that many cards. int index = 0; while (index < cards) { hand.add(deck.deal()); // deals cards depending on what the user asks for index++; } //Shows the hand. System.out.println(""); System.out.print("Your Hand: "); // prints out the hand for(int j = 0; j <= hand.size()-1; j++){ System.out.print("\t"+hand.get(j) + " "); } // prints out the cards in the hand for(index = 0; index< hand.size(); index ++) { String spade; spade = hand.get(index).toString(); if(spade.contains("spades")) { System.out.println("\tPosition in Hand: " + (index+1) ); // adds one so position in hand doesn't begin with zero } } ArrayList ValueCompare = new ArrayList(); while (hand.size() > 0) { int place = 0; // place of card. Card c = (Card)hand.get(0); // Takes the first card and looks at each card after that and sorts it for (int j = 1; j < hand.size(); j++) { Card c1 = (Card)hand.get(j); if ( c1.pointValue() < c.pointValue() || (c1.pointValue() < c.pointValue() && c1.suit() == c.suit()) ) { // if the point values are different it orders them place = j; c = c1; } } hand.remove(place); // removes the place ValueCompare.add(c); // adds the ordered cards to the ValueCompare arraylist } hand = ValueCompare; // sets hand equal to the ValueCompare System.out.println(""); System.out.print("Ascending Order: \t");// prints it out in ascending order for(int j = 0; j <= hand.size()-1; j++){ System.out.print(hand.get(j) + " \t"); } Collections.reverse(hand); // reverses it using the collection class System.out.println(""); System.out.print("Descending Order: \t");// prints it out in ascending order for(int j = 0; j <= hand.size()-1; j++){ System.out.print(hand.get(j) + " \t"); } Collections.reverse(hand); // reverses it using the collection class ArrayList RedCards = new ArrayList(); ArrayList BlackCards = new ArrayList(); for (int j = 0; j < hand.size(); j++) { Card c1 = (Card)hand.get(j); if ( c1.suit().equals("hearts") || c1.suit().equals("diamonds")) { RedCards.add(c1); } else{ BlackCards.add(c1); } } System.out.println(""); System.out.print("Red Cards: \t"); for(int j = 0; j <= RedCards.size()-1; j++){ System.out.print(RedCards.get(j) + " \t"); } System.out.println(""); System.out.print("Black Cards: \t"); for(int j = 0; j <= BlackCards.size()-1; j++){ System.out.print(BlackCards.get(j) + " \t"); } System.out.println(""); System.out.println("What is the value of the pairs you want?"); int pairValue =scan.nextInt(); int numberOfPairs=0; for(int i =0; i < hand.size()-1;i++) { for(int j = (i+1); j < hand.size()-1; j++ ) { if (hand.get(i).pointValue() == hand.get(j).pointValue()) { if (pairValue == hand.get(i).pointValue()){ numberOfPairs ++; } } } } System.out.println("There are " + numberOfPairs + " pair(s) of cards with the value " +pairValue+"." ); ArrayList SpadeCards = new ArrayList(); ArrayList ClubCards = new ArrayList(); ArrayList HeartCards = new ArrayList(); ArrayList DiamondCards = new ArrayList(); int[] SpadeNums = new int[hand.size()]; for(int i =0;i if(hand.get(i).suit().equalsIgnoreCase("spades")){ SpadeCards.add(hand.get(i)); } } if(SpadeCards.size()<5){ } else{ for(int i =0;i SpadeNums[i]=SpadeCards.get(i).pointValue(); } Arrays.sort(SpadeNums); ArrayList SecondSpadeCards = new ArrayList(); for(int i =0;i if(hand.get(i).pointValue()==SpadeNums[4] || hand.get(i).pointValue()==SpadeNums[3]||hand.get(i).pointValue()==SpadeNums[2]||hand.get(i).pointValue()==SpadeNums[1]||hand.get(i).pointValue()==SpadeNums[0]){ SecondSpadeCards.add(hand.get(i)); } } for(int i =0;i System.out.println(SecondSpadeCards.get(i).toString()); } System.out.println("Your flush is"); // adds one so position in hand doesn't begin with zero } } }Deck
instance.
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