2014-11-25 113 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) + "|";  
    } 
    } 
} 

在此之後,繼續無論你在做!很抱歉,如果有任何代碼錯誤,我只是寫這個沒有編譯它。但這是代碼的主要思想。希望你明白,這對你有用..