Java 洗牌

Java 洗牌,java,shuffle,Java,Shuffle,我洗牌时遇到问题。我的程序中有大约3种洗牌方法,但它们似乎都不起作用。如果有人能帮我解决这个问题,那就太好了。我有一个满是卡片的文件夹,叫做1.png,2.png。。。。52.png public class CardButton extends JButton{ ImageIcon front, back; boolean flipped; public CardButton(ImageIcon front, ImageIcon back) {

我洗牌时遇到问题。我的程序中有大约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;
            }
        }
    }
}
公共类CardButton扩展了JButton{
图像图标前,后;
布尔翻转;
公共卡按钮(图像图标前、图像图标后)
{
this.front=front;
this.back=back;
翻转=正确;
翻转();
}
公共空间翻转()
{
翻转=!翻转;
如果(翻转)
此.setIcon(前);
其他的
此.setIcon(返回);
}
}
公共类CardButtonPanel扩展了JPanel{
卡片按钮b、b1、b2、b3;
图像图标背面,card1,card2,card3;
整数计数;
ActionListener任务执行者;
定时器t1;
//实例化新数组
CardButton[]数组;
公共卡按钮面板(){
int w=72,h=86;
back=newImageIcon(getClass().getResource(“b2fv.png”);
Image i1=back.getImage();
Image i2=i1.getScaledInstance(w,h,Image.SCALE\u默认值);
back.setImage(i2);
数组=新卡片按钮[53];
列表=数组。asList(数组);
集合。洗牌(列表);
for(int i=1;i
实际上,我试图创建另一个洗牌类

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;
    }
}
公共类洗牌{
公共静态int[]shuffleCards(int[]卡){
对于(int i=1;i
我不知道如何在我的CardButtonPanel中实现这一点,所以我不太确定它是否有效。

使用最佳的洗牌算法

示例代码:

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;
    }
  }
}
import java.util.*;
课堂测试
{
公共静态void main(字符串参数[])
{
int[]solutionaray={1,2,3,4,5,6,16,15,14,13,12,11};
Shuffleray(Solutionaray);
对于(int i=0;i0;i--)
{
整数指数=rnd.nextInt(i+1);
//简单交换
int a=ar[指数];
ar[index]=ar[i];
ar[i]=a;
}
}
}

您可以通过两种方式进行洗牌:-

1) Fisher-Yates算法是一个很好的选择

2) 否则,您可以创建卡片列表并进行收集。然后使用
Collections.shuffle
. 这是迄今为止我用过的最简单的方法之一

看看这个问题,你就放心了。

最好的方法是使用调试器逐步完成此操作,这样您就可以了解为什么它没有按预期执行。不要每次都创建一个新的随机对象。在程序开始时只创建一个。而且我应该从0开始,而不是1。所以我尝试了Collections.shuffle,但我的问题仍然没有解决。你对我是怎么搞砸的有什么想法吗?我想你在实现集合方面有问题。shuffle。我已经更新了我的答案,用一个例子来说明这个问题。希望我能帮助你。