2014-11-25 119 views
0

我的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(); 
    } 
} 
+2

当前代码有什么问题?它工作与否? – SMA 2014-11-25 14:38:05

+2

只要你知道每件衣服有13件。 Ace - > 10 Jack Queen King – 2014-11-25 14:42:29

+0

为什么你有一个静态的'Random'对象,然后到处使用'Math.random()'? – 2014-11-25 14:42:49

回答

0

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

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] = cardnumber

0

的记忆卡是否已被绘制或不

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

你可以做什么,是存储你之前选择的卡片,所以你没有重复。我相信这将帮助:

创建一组,让我们把它叫做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,并在里面,创建你想要得到的相同的随机数,但这一次,我们得到它后,我们检查卡是否被选中d,如果是,则重复并再次检查,直到我们得到一个选择非,像这样:

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

在此之后,继续无论你在做!很抱歉,如果有任何代码错误,我只是写这个没有编译它。但这是代码的主要思想。希望你明白,这对你有用..