3

I have my primary class running, and I wanted to run a separate class to shuffle numbers, then return the shuffled numbers into my primary class. In my shuffle class I have the return statement... but now what do I do? How do I use the random order of my int array in my primary class?

Here is my shuffle class:

public class Shuffle {
public static int[] getShuffle() {
    int[] cards = new int[52];
    ArrayList<Integer> cards_objs = new ArrayList<Integer>();

    for (int i = 0; i < cards.length; i++) {
        cards_objs.add(i);
    }

    Collections.shuffle(cards_objs);

    for (int i = 0; i < cards.length; i++) {
        cards[i] = cards_objs.get(i);

    }

    return cards;
}

}

I am making a card game(if you cant tell);

I wanted to use this shuffle class so that the cards are shuffled... but no card appears more than once.

when I return cards, how do I use them in my game class? for example if the first number in the array is 1, then the card is Ace of clubs, if the number is 2, then the card is Ace of diamonds. and so on... I apologize for not posting enough information... I am new to java (as you can tell)

all help will be greatly appreciated,

-Steve

EDIT: I found out what my problem was, I don't think I made it clear enough what my question was. Nonetheless thank you all for your help, it gave me ideas on different ways to approach this project.

1
  • 5
    It's not very clear what you're trying to do. Can you post some relevant code? Commented Aug 27, 2012 at 6:44

5 Answers 5

3

I think using enum you can implement Card Game in a better way.

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

// This is just sample example  and it is just to show an approach. I had not enough time    to make code perfect but it works fine.
public enum Deck
{
DECK;
enum Rank
{
    DEUCE(2), THREE(3), FOUR(4), FIVE(5), SIX(6),
    SEVEN(7), EIGHT(8), NINE(9), TEN(10), JACK(10), QUEEN(10), KING(10), ACE(11);

    Rank(int rank)
    {
        this.rank = rank;
    }

    private final int rank;

}

enum Type
{
    SPADES,
    HEARTS,
    DIAMONDS,
    CLUBS;
}

class Card
{
    private final Rank rank;
    private final Type type;

    Card(Rank rank, Type type)
    {
        this.rank = rank;
        this.type = type;
    }

    @Override
    public String toString()
    {
        return type.name() + rank.name();
    }

}

static List<Deck.Card> cards = new ArrayList<Deck.Card>();
static
{
    for (Rank rank : Deck.Rank.values())
    {
        for (Type type : Deck.Type.values())
        {
            cards.add(DECK.new Card(rank, type));
        }
    }

}

List<Deck.Card> shuffle()
{
    Collections.shuffle(cards);
    System.out.println(cards);
    System.out.println(cards.size());
    return Collections.unmodifiableList(cards);
}

public static void main(String[] args)
{
    DECK.shuffle();
}
}
Sign up to request clarification or add additional context in comments.

1 Comment

@O.D Changed the solution I guess I come to conclusion too fast :(
1

No need to define seperate class i feel, Just create an arraylist of numbers, pass it to Colections class of java

public static void shuffle(List list)

Your list will be shuffled directly

1 Comment

Steven is using Collections.shuffle(List<?> list) in his method.
0

Either you change the array order so you can iterate the normal order in the array in your main class or you return an array back from your shuffle methode which contains the order (index) desired then access these up to this order.

Option A is to change the roder of the elemtns of your main array and Option B is to read the array and decide determin the new order, and return the order as ana rray backt, then access the main array up tot he new order.

Comments

0

You can simply have a method in you main class where you can do the shuffling and return the new set of shuffled numbers as an array.

If you do want to use another class, then you can do that using the instance of the class, and then call that reShuffleNumbers(), whereas reShuffleNumbers() will return array of numbers as you've wanted.

Comments

0

Try this -

public static void shuffleArray(int[] a) {
    int n = a.length;
    Random random = new Random();
    random.nextInt();
    for (int i = 0; i < n; i++) {
      int change = i + random.nextInt(n - i);
      swap(a, i, change);
    }
  }

  private static void swap(int[] a, int i, int change) {
    int helper = a[i];
    a[i] = a[change];
    a[change] = helper;
  }

Pass the array to method shuffleArray() and you will get your array elements modified by position.

I hope this is what you are looking for.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.