简体   繁体   中英

How do I print out a random, two-dimensional array without duplicates?

My CompSci Class has an assignment that we're doing in which we have to print out a deck of cards as a two-dimensional, 6 row by 8 column array. Each "card" is basically a randomly generated number (1-12) and a randomly selected suit (Diamonds, Hearts, Spades, & Clubs); no card can be repeated anywhere in the array. Here's my code:

static Random random = new Random(1234567);

static int i = 1;

static int a;
static int d;

static List<String> suits = new LinkedList<String>();

static List<String> cards = new LinkedList<String>();

static int[][] grid = new int[6][8];

public static void main(String[]args)
{
    suits.add("Diamonds");
    suits.add("Clubs");
    suits.add("Hearts");
    suits.add("Spades");

    cards.add("1");
    cards.add("2");
    cards.add("3");
    cards.add("4");
    cards.add("5");
    cards.add("6");
    cards.add("7");
    cards.add("8");
    cards.add("9");
    cards.add("10");
    cards.add("11");
    cards.add("12");

    drawGrid();
}
private static void drawGrid()
{
    for(int b = 0; b < grid.length; b++)
    {
        for(int c = 0; c < grid[i].length; c++)
        {
            a = (int)(Math.floor(suits.size() * Math.random()));
            d = (int)(Math.floor(suits.size() * Math.random()));

            System.out.print(" |" + cards.get(d) + " " + suits.get(a) + "|");

            Collections.shuffle(suits);
            Collections.shuffle(cards);
        }
        System.out.println();
    }
}

There are different ways to do it. My first idea would be to create a list of int-arrays.

    ArrayList<int[]> list = new ArrayList<int []> ();

    for (int i=0; i<suits.size(); i++)
    {
        for(int j=0; j<cards.size(); j++)
        {
            int[] card = {i,j};
            list.add(card);
        }
    }

    Collections.shuffle(list);

Now every element represents a card. With the first 6 Elements you've got 6 randomized Cards. element[0]=suit and element[1]=cardnumber

Create a variable for remembering whether a card has been drawn or not

static boolean[][] drawn = new boolean[4][13];

Then replace the bit where you draw the card with:

do {
    a = (int)(Math.floor(suits.size() * Math.random()));
    d = (int)(Math.floor(cards.size() * Math.random()));
} while (drawn[a][d]);
drawn[a][d] = true;

Finally, remove the two lines where you are shuffling your two collections. Math.random will provide you a random card.

        Collections.shuffle(suits);
        Collections.shuffle(cards);

What you can do, is store someplace the cards that you have previously selected so you don't have duplicates. I am sure this will help:

Create a set, lets call it selectedCardMap :

static Set<String> selectedCardMap = new HashSet<String>();

Lets replace these two lines of code:

a = (int)(Math.floor(suits.size() * Math.random()));
d = (int)(Math.floor(suits.size() * Math.random()));

for a method which will give you a new unused card, lets call this method getNewCard , and inside, create the same random numbers you were trying to get, but this time, after we get it, we check if the card was selected, if it was, then repeat and check again until we get a non selected one, like this:

public static String getNewCard() {

while (true) {
    int a = (int)(Math.floor(suits.size() * Math.random()));
    int d = (int)(Math.floor(suits.size() * Math.random()));

    //Make a key for the map
    String key= d+"-"+a;


    //check if it was selected
    if (!selectedCardMap.contains(key)) {
       //This card is available, add it to selected and return it!
       selectedCardMap.add(key);
       return " |" + cards.get(d) + " " + suits.get(a) + "|";     
    }
  }
}

After this, continue with whatever you were doing!, Sorry if there is any code error, I just wrote this without compiling it. But this is the main idea of the code. Hope you understand and it is useful for you..

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM