Creating a deck of cards with two variations: 52 and infinite

434 views Asked by At

I'm trying to create a deck of cards with two variations: one which can choose and return a card from a standard 52 deck of cards with no cards repeating. And then another "infinite" deck of cards in which its okay to repeat cards.

public Deck(int a) {

    if (a == 0) {
        deckOf52();
    } else {
        infiniteDeck();
    }

}

public void deckOf52() {

}

public void infiniteDeck() {
    cards = new ArrayList<Card>(52);
    for (int a = 0; a <= 3; a++) {
        for (int b = 0; b <= 12; b++) {
            cards.add(new Card(a, b));
        }
    }
}

public Card getCard() {
    Random generator = new Random();
    int index = generator.nextInt(cards.size());
    return cards.remove(index);
}

The infiniteDeck can do exactly as I need, but I'm unsure of how to get the deckOf52 method to do the same and be restricted solely to the 52 cards with none repeating.

Does anyone have any ideas or anything to point me in the right direction?

3

There are 3 answers

0
BevynQ On

Each card in infinite deck is unique and getCard removes each card it uses, so each card will only ever appear once.

I am not sure what you are trying to do but this will satisfy the description you have given us.

private int deckType;

public Deck(int a) {
    this.deckType = a;
    createDeck();
}

// create deck of 52 unique cards
public void createDeck() {
    cards = new ArrayList<Card>(52);
    for (int a = 0; a <= 3; a++) {
        for (int b = 0; b <= 12; b++) {
            cards.add(new Card(a, b));
        }
    }
}

// infinite deck never runs out of cards so do not remove card
public Card getCard() {
    Random generator = new Random();
    int index = generator.nextInt(cards.size());
    if(this.deckType == 0){
        return cards.remove(index);
    } else {
        return cards.get(index);
    }
}
0
user1071840 On

How about this?

 public void deckOf52() {
    // Your definition of card list. 
    cards = new ArrayList<Card>(52);
    Random RNG = new Random();
    for (int i = 0; i < 52; ++i) {
        // Pick a card from 0 to position (52 - i).
        int cardPicked = RNG.nextInt() % (52 - i);
        // In your list swap cards at position 52 - i and cardPicked.
        swapCards(cards, cardPicked, 52 - i);
    }
    // You'll have a shuffled deck of cards after this loop.
 }
0
Menelaos On

You can shuffle your collection at the beginning in order to have a random selection of cards available:

import java.util.ArrayList;
import java.util.Collections;
import java.util.Random;


public class Deck {

    ArrayList<Card> cards = new ArrayList<Card>(52);
    private final Random generator = new Random();
    private int previous;
    boolean infiniteDeck = false;

    public static void main(String args[])
    {
        Deck infinite_deck = new Deck(true);
        Deck limited_deck = new Deck(false);

        System.out.println("Pulling cards from infinite deck:");
        for(int i=0; i<100; i++)
        {
            System.out.println("InfiniteDeck " + i + ":\r\n Pulling card :\r\n"+infinite_deck.getCard());
        }

        System.out.println("Pulling cards from finite/limited deck:");
        for(int i=0; i<100; i++)
        {
            System.out.println("FiniteDeck " + i + ":\r\n Pulling card :\r\n"+limited_deck.getCard());
        }


    }

    public Deck(boolean infiniteDeck) {

        this.infiniteDeck = infiniteDeck;

        cards = new ArrayList<Card>(52);
        for (int a = 0; a <= 3; a++) {
            for (int b = 0; b <= 12; b++) {
                cards.add(new Card(a, b));
            }
        }

        if(!infiniteDeck)//Shuffle the deck randomly
        Collections.shuffle(cards, new Random(100));
    }

    public Card getCard() {

        if(infiniteDeck)
        {
            return cards.get(generator.nextInt(52));
        }
        else
        {
            if(cards.size() > 0)
            return cards.remove(cards.size()-1);
            else
            return null;
        }   
    }

    class Card
    {

        int type, number;

        public Card(int a, int b)
        {
            type = a;
            number = b;
        }   

        public String toString()
        {
            return "CARD{\r\n\ttype:" + type + ",\r\n\tnumber:" + number+"\r\n}";
        }
    }

}