2014-11-09 2 views
0

배열 배열을 뒤섞어 쓰는 데 문제가 있습니다. 내 프로그램에 약 3 개의 셔플 메서드가 있으며 그 중 아무 것도 작동하지 않는 것 같습니다. 누구든지이 일을 도와 주면 좋을 것입니다. 나는 실제로 다른 셔플 클래스를 작성하려고카드 배열을 뒤섞기

public class CardButton extends JButton{ 

    ImageIcon front, back; 
    boolean flipped; 

    public CardButton(ImageIcon front, ImageIcon back) 
    { 
     this.front = front; 
     this.back = back; 
     flipped = true; 
     flip(); 
    } 

    public void flip() 
    { 
     flipped = !flipped; 
     if (flipped) 
      this.setIcon(front); 
     else 
      this.setIcon(back); 
    } 
} 

public class CardButtonPanel extends JPanel { 

    CardButton b, b1, b2, b3; 
    ImageIcon back, card1, card2, card3; 
    int count; 
    ActionListener taskPerformer; 
    Timer t1; 

    // Instantiating a new array 
    CardButton[] array; 

    public CardButtonPanel() { 
     int w = 72, h = 86; 

     back = new ImageIcon(getClass().getResource("b2fv.png")); 
     Image i1 = back.getImage(); 
     Image i2 = i1.getScaledInstance(w, h, Image.SCALE_DEFAULT); 
     back.setImage(i2); 

     array = new CardButton[53]; 
     List list = Arrays.asList(array); 
     Collections.shuffle(list); 
     for (int i = 1; i < array.length; i++) { 
      // int j = shuffle(); 
      card1 = new ImageIcon(getClass().getResource(i + ".png")); 
      i1 = card1.getImage(); 
      i2 = i1.getScaledInstance(w, h, Image.SCALE_DEFAULT); 
      card1.setImage(i2); 
      b1 = new CardButton(card1, back); 
      b1.addActionListener(new ButtonHandler1()); 
      add(b1); 
      array[i] = b1; 
     } 
     taskPerformer = new ActionListener() { 
      public void actionPerformed(ActionEvent evt) { 
       b1.flipped = true; 
       b1.flip(); 
      } 
     }; 
     t1 = new Timer(200, taskPerformer); 
     t1.setRepeats(false); 
    } 

    /* 
    * public void shuffle() { currentCard = 1; for (int i = 1; i<array.length; 
    * i++) { int second = randomNumbers.nextInt(52); 
    * 
    * 
    * } } 
    * 
    * public int randomInteger(int x, int y) { Random rInteger = new Random(); 
    * // int IntegerRandom = rInteger.nextInt((x-y) +1) + x; int IntegerRandom 
    * = rInteger.nextInt(52); return IntegerRandom; } 
    * 
    * public void swapCards(int i, int j) { CardButton temp = array[i]; 
    * array[i] = array[j]; array[j] = temp; } 
    * 
    * public void shuffle() { for (int i = 0; i < array.length; i++) { int j = 
    * randomInteger(i, array.length - 1); } } 
    */ 

    /* 
    * public static int[][] randomize(int rows, int cols) { int[][] temp = new 
    * int[4][13]; Random randomize = new Random(); // int stuff; for (int i = 
    * 0; i < temp.length; i++) { // temp[i] = new int[cols]; for (int j = 0; j 
    * < temp[i].length; j++) temp[i][j] = (int) ((Math.random()) * (rows * 
    * cols)); // stuff = randomize.nextInt(52); } return temp; } 
    */ 

    private class ButtonHandler1 implements ActionListener { 
     public void actionPerformed(ActionEvent e) { 
      // Need to create for loop for printing out how many attempts 
      int i = 0; 
      System.out.println(i + 1); 

      CardButton tempCB = (CardButton) e.getSource(); 
      if (!tempCB.flipped && !t1.isRunning()) { 
       tempCB.flip(); 
       count++; 
      } 
      if (count > 1) { 
       t1.start(); 
       count = 0; 
      } 
     } 
    } 
} 

52.png 1.png, 2.png라는 카드의 전체 폴더를 .... 있습니다.

public class Shuffle { 
    public static int[] shuffleCards(int[] cards) { 
     for (int i = 1; i < cards.length; i++) { 
      int rand = new Random().nextInt(cards.length-i)+i; 
      int temp = cards[i]; 
      cards[i] = cards[rand]; 
      cards[rand] = temp; 
     } 
     return cards; 
    } 
} 

이 코드를 내 CardButtonPanel에 구현하는 방법을 모르겠으므로 제대로 작동하는지 확실하지 않습니다.

+0

당신이 할 수있는 최선의 방법은 디버거를 사용하여 단계별로 수행하는 것이므로 예상 한대로 작동하지 않는 이유를 알 수 있습니다. –

+1

매번 새로운 무작위 개체를 만들지 마십시오. 프로그램을 시작할 때 하나만 만듭니다. 또한 나는 0이 아니라 1에서 시작해야합니다. – SpiderPig

답변

1

를 사용하여 최고의 Fisher Yates 셔플 알고리즘

샘플 코드 : 두 가지 방법으로 셔플 할 수

import java.util.*; 

class Test 
{ 
    public static void main(String args[]) 
    { 
    int[] solutionArray = { 1, 2, 3, 4, 5, 6, 16, 15, 14, 13, 12, 11 }; 

    shuffleArray(solutionArray); 
    for (int i = 0; i < solutionArray.length; i++) 
    { 
     System.out.print(solutionArray[i] + " "); 
    } 
    System.out.println(); 
    } 

    // Implementing Fisher–Yates shuffle 
    static void shuffleArray(int[] ar) 
    { 
    Random rnd = new Random(); 
    for (int i = ar.length - 1; i > 0; i--) 
    { 
     int index = rnd.nextInt(i + 1); 
     // Simple swap 
     int a = ar[index]; 
     ar[index] = ar[i]; 
     ar[i] = a; 
    } 
    } 
} 
0

: -

1) 피셔 예이츠 알고리즘은 좋은 선택 중 하나입니다.

2) 그렇지 않으면 카드 목록을 만들어 수집 할 수 있습니다. 그런 다음 Collections.shuffle link for tutorial을 사용하십시오. 지금까지 내가 사용한 가장 쉬운 방법 중 하나.

귀하의 편의를 위해이 질문을 살펴보십시오. question

+0

그래서 나는 Collections.shuffle을 시도했지만 내 문제는 아직 해결되지 않았습니다. 내가 엉망진창에 대한 아이디어가 있니? –

+0

Collections.shuffle 구현에 문제가 있다고 생각합니다. 사례를 뒤섞기위한 예제로 내 대답을 업데이트했습니다. 희망이 당신을 도울 수 있습니다. – Crawler