0

Sorry if this is long but i'm writing a program that draws a poker hand(5 different cards) from a standard deck of 52. The only part Im still struggling with is getting different cards. The code I have now is as simple as it gets and works for the most part but sometimes can draw the same card more than once. I want the card to be removed from the deck once its drawn and i'm stuck on that part.

Card[] hand = new Card[5];
        for (int i = 0; i < 5; i += 1)
        {
          int index = rand.nextInt(52);
          hand[i] = cards[index];
        }
    return hand;
  • Since it is an array you can't really "remove" it since the index is still there; instead, once you draw it, try setting that index to null. – SomeStudent Oct 26 '16 at 02:45
  • You may get inspired by http://stackoverflow.com/questions/8115722/generating-unique-random-numbers-in-java – SomeDude Oct 26 '16 at 02:45
  • as mentioned by @SomeStudent, are you allowed to use something else than an array ? Like an ArrayList for instance. You could then remove the object as your randomly choose it, and then it won't be available for the next draws (if you do this, don't forget to call nextInt with a variable: rand.nextInt(numberOfCardsRemaining) ). – alexbt Oct 26 '16 at 02:53
  • I can use whatever I want. The thing is im not familiar with array lists at all thats why I use arrays... – suck_at_coding18 Oct 26 '16 at 02:54
  • So an arrayList is an implementation of the List interface. Under the hood it uses arrays, but it offers you simple ways of adding and removing values. So, the most basic example, you know how with an array you have to define its size? don't have to with a list, you can just call .add and it will automatically size it. same thing with .remove, it will remove the object and decrease the size. – SomeStudent Oct 26 '16 at 03:02
  • Thank you very much @SomeStudent – suck_at_coding18 Oct 26 '16 at 03:10
  • no problem, trust me, as a student myself I know the feeling you are going through right now. – SomeStudent Oct 26 '16 at 03:12

4 Answers4

2

Use List and Collections.shuffle().

List<Card> cards = new ArrayList<>(52);
for (int i = 0; i < 52; i++) {
  cards.add(new Card(i)); // or so
}
Collections.shuffle(cards);

Card[] hand = new Card[5];
for (int i = 0; i < 5; i += 1) {
  hand[i] = cards.remove(0);
}

return hand;
kaitoy
  • 1,545
  • 9
  • 16
1

You can create a ArrayList like

List<Card> cards = new ArrayList<>();

// add 52 cards to cards

Card[] hand = new Card[5];
for (int i = 0; i < 5; i ++) {
    int index = rand.nextInt(cards.size());
    hand[i] = cards.remove(index);
}
T.Tony
  • 495
  • 3
  • 15
0

You could literally do what you do with a deck of cards:

class Deck
{
    private LinkedList<Card> cards = new LinkedList<Card>();

    Deck()
    {
          for (i=0; i<52; ++i) {
              // or however you want to correctly create a card
              cards.add(new Card(i))
          }
    }

    public Card takeRandomCard()
    {
          int takeCard = rand.nextInt(cards.size());
          return cards.remove(takeCard);
    }
}

int handSize = 5;
Deck deck = new Deck();
Card[] hand = new Card[handSize];
for (int i=0; i<handSize; ++i) {
    hand[i] = deck.takeRandomCard();
}

This may not be the most efficient method but it's hopefully pretty clear what it's doing.

Pulling random cards may or may not be faster than shuffling the entire deck first. LinkedList is faster than ArrayList when removing random entries. Probably irrelevant though really.

0

Just create a List of Integers ranging from 1 to 50. I've demonstrated this example using Java 1.8.

public class NumUtility {
    public static List<Integer> shuffle() {
        List<Integer> range = IntStream.range(1, 53).boxed()
                .collect(Collectors.toCollection(ArrayList::new));
        Collections.shuffle(range);
        return range;
    }
}

Now you can iterate through index 1 to 5 and whenever you want shuffled numbers, just call the above method.

Card[] hand = new Card[5];

//call this method whereever you want random integers.
List<Integer> range = NumUtility.shuffle();

for (int i = 0; i < 5; i += 1) {
    hand[i] = range.get(i);
}
return hand;
Raman Sahasi
  • 30,180
  • 9
  • 58
  • 71