2014-11-09 104 views
0

我在洗牌時遇到了問題。在我的程序中有大約3種洗牌方法,它們都不起作用。如果有人能幫我解決這個問題,那會很棒。我有一個文件夾名爲1.png,2.png .... 52.png重新排列一系列卡片

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

我實際上試圖創建另一個shuffle類。

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

你可以做的最好的事情就是用調試器來完成這個任務,這樣你就能明白爲什麼它沒有達到你期望的效果。 – 2014-11-09 04:02:44

+1

不要每次創建一個新的Random對象。在程序開始時只創建一個。我也應該從0開始,而不是1. – SpiderPig 2014-11-09 04:15:41

回答

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,但是我的問題仍然沒有解決。你對我如何搞砸了有什麼想法嗎? – 2014-11-09 04:44:27

+0

我認爲你在實現Collections.shuffle時遇到了問題。我用一個洗牌案例來更新我的答案。希望我可以幫助你。 – Crawler 2014-11-09 05:08:17