2014-11-25 4 views
0

CompSci 클래스에는 2 차원, 6 행 8 열 배열로 카드 덱을 인쇄해야하는 과제가 있습니다. 각 "카드"는 기본적으로 임의로 생성 된 번호 (1-12)와 무작위로 선택된 수트 (Diamonds, Hearts, Spades, & 클럽)입니다. 어떤 카드도 배열의 아무 곳에서나 반복 될 수 없습니다. 내 코드는 다음과 같습니다.무작위로 2 차원 배열을 중복없이 인쇄하려면 어떻게해야합니까?

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

+2

각 소송에서 13 개가 있다는 것을 알고 있습니다. 에이스 -> 10 Jack Queen King –

+0

왜 정적 인'Random' 객체를 가지고 있고 어디에서 Math.random()을 사용합니까? –

답변

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를 호출 줄 것이다 방법에 대한

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

:

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

코드의 두 라인을 대체 할 수 있습니다 당신에게 새로운 미사용 카드를주고,이 방법을 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) + "|";  
    } 
    } 
} 

: D, 그것이 있다면, 반복 우리는이 같은 하나를 선택 비를 얻을 때까지 다시 확인 그것을 컴파일하지 않고 썼다. 그러나 이것은 코드의 주요 아이디어입니다. 희망을 이해하고 유용합니다 ..

관련 문제