繁体   English   中英

如何打印没有重复的随机二维数组?

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

我的CompSci类有一个正在做的作业,其中我们必须将一副纸牌打印为二维,6行8列的数组。 每个“卡片”基本上都是随机产生的数字(1-12)和随机选择的衣服(钻石,红心,黑桃和棍棒); 阵列中的任何位置都不能重复存储卡。 这是我的代码:

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();
    }
}

有不同的方法可以做到这一点。 我的第一个想法是创建一个整数数组列表。

    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);

现在,每个元素都代表一张卡片。 使用前6个元素,您将获得6张随机卡片。 元素[0] =西装和元素[1] =卡号

创建一个变量以记住是否已抽签

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

然后将您抽出卡片的位置替换为:

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;

最后,删除要拖曳两个集合的两行。 Math.random将为您提供随机卡。

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

您可以做的是将以前选择的卡存储在某个地方,以免重复。 我相信这会有所帮助:

创建一个集合,将其称为selectedCardMap

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

让我们替换这两行代码:

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

对于将为您提供新的未使用卡的方法,请调用此方法getNewCard ,并在内部创建与您尝试获取的随机数相同的随机数,但是这次,在获得卡之后,我们检查是否选择了卡,是否是的,然后重复并再次检查,直到得到未选中的对象,如下所示:

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) + "|";     
    }
  }
}

此后,继续您所做的一切!,对不起,如果有任何代码错误,我只是编写了此代码而没有对其进行编译。 但这是代码的主要思想。 希望您能理解,对您有用。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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