lucifer
lucifer

Reputation: 11

i need to make i program that prints out a shuffled deck of cards and a sorted deck of cards

Create a array of random card. Output this array in a formatted print statement using a toString method (unsorted). Use the selection sort to sort your array. Sort from highest card in the deck to the lowest. Ascending alphabetical order: clubs (lowest), followed by diamonds, hearts, and spades (highest) The highest card in the deck is the ACE of Spades, the lowest is the 2 of Clubs. Output a sorted array using the toString method. Also print out the number iterations

i am a starter coder and need help with this here is some of what i started.

public class DeckOfCards2 {
  public static void main(String[] args) {
    int[] deck = new int[52];
    String[] suits = {"Spades", "Hearts", "Diamonds", "Clubs"};
    String[] ranks = {"Ace", "2", "3", "4", "5", "6", "7", "8", "9", "10", "Jack", "Queen", "King"};

    // Initialize cards
    for (int i = 0; i < deck.length; i++) {
      deck[i] = i;
    }

    // Shuffle the cards
    for (int i = 0; i < deck.length; i++) {
      int index = (int)(Math.random() * deck.length);
      int temp = deck[i];
      deck[i] = deck[index];
      deck[index] = temp;
    }

    // Display the all the cards
    for (int i = 0; i < 52; i++) {
      String suit = suits[deck[i] / 13];
      String rank = ranks[deck[i] % 13];
      System.out.println( rank + " of " + suit);
    }
  }
}

Upvotes: 1

Views: 1986

Answers (2)

Connor Bailey
Connor Bailey

Reputation: 69

This seems like an assignment of sorts so I'm not going to flat out give you the answer, but I can point you in the right direction for most of this so you can figure it out on your own.

First thing is first, you're going to need a toString method in your class. These typically look like the following:

public String toString(array[]){

    // your code here
    return text;

}

The method takes in an array parameter (which is the data you need to print). Inside this, you probably want a for-loop or while-loop of some kind just to loop through the array and add each element to a string instance variable using something like

text = text + array[i];

That way you can add every value in your array to the string, and then just return that string.

Next, the problem states that you have to create a "random array of random card". You already have the code here that generates a random card. Instead, you need to add the results to your new random array instead of printing them out. This is now your master unsorted array. In your main method, you can do something along the lines of

// prints out unsorted array
System.out.println(toString(array));

where array is the name for your random unsorted array.

Next begins the process of creating your sorted array. Read more here on example code for a selection sort: http://www.javatpoint.com/selection-sort-in-java

As the problem says above, make sure to count your iterations. This can be easily accomplished with an incrementing counting variable.

count++;

Once the array is sorted, just send it to the toString() method like we did before, and then just make sure to print out your counting variable.

Upvotes: 0

Markus
Markus

Reputation: 1171

Your method to randomize works :) You just need to add the sorting. Try something like this:

   public static void main(String[] args) {
        int[] deck = new int[52];
        String[] suits = {"Spades", "Hearts", "Diamonds", "Clubs"};
        String[] ranks = {"2", "3", "4", "5", "6", "7", "8", "9", "10", "Jack", "Queen", "King","Ace"};

        // Initialize cards
        for (int i = 0; i < deck.length; i++) {
            deck[i] = i;
        }

        // Shuffle the cards
        for (int i = 0; i < deck.length; i++) {
            int index = (int)(Math.random() * deck.length);
            int temp = deck[i];
            deck[i] = deck[index];
            deck[index] = temp;
        }

        // Display the all the cards
        for (int i = 0; i < 52; i++) {
            String suit = suits[deck[i] / 13];
            String rank = ranks[deck[i] % 13];
            System.out.println( rank + " of " + suit);
        }


        System.out.println("------------------------------");
        for(int i=0;i<deck.length;i++){
            for(int j=i;j<deck.length;j++){
                if(deck[j]==51-i){
                    int temp = deck[i];
                    deck[i] = deck[j];
                    deck[j] = temp;
                }
            }
        }

        // Display the all the cards
        for (int i = 0; i < 52; i++) {
            String suit = suits[deck[i] / 13];
            String rank = ranks[deck[i] % 13];
            System.out.println( rank + " of " + suit);
        }
    }

Upvotes: 0

Related Questions