0

I'm having trouble shuffling an array of cards.I have about 3 shuffling methods in my program and none of them seem to work. If anyone can help me out with this, that would be great. I have a folder full of cards called 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;
            }
        }
    }
}

I actually attempted to create another shuffle class.

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

I don't know how to implement this into my CardButtonPanel, so I'm not too sure if it works or not.

Chris Martin
  • 30,334
  • 10
  • 78
  • 137
Alex M
  • 1
  • 2
  • The best thing you could do would be to step through this with a debugger, so you could see why it's not doing what you expect it to. – Dawood ibn Kareem Nov 09 '14 at 04:02
  • 1
    Don't create a new Random object each time. Create only one at the start of your program. Also i should start at 0 and not 1. – Tesseract Nov 09 '14 at 04:15

2 Answers2

1

Use best Fisher Yates shuffling algorithm

Sample code :

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;
    }
  }
}
Sunil Sharma
  • 1,297
  • 3
  • 17
  • 31
0

You can do shuffling in two ways:-

1)Fisher Yates Algorithm is one of the great choice.

2)Else You can create the list of cards and make its collection. Then use Collections.shuffle link for tutorial. One of easiest way I've used so far.

Take look in this question for your ease. question

Community
  • 1
  • 1
Crawler
  • 1,988
  • 3
  • 21
  • 42
  • So I tried doing Collections.shuffle but my problem still hasn't been fixed. Do you have any ideas on how I messed up? – Alex M Nov 09 '14 at 04:44
  • I think you have problem in implementing the Collections.shuffle. I have update my answer with an example for shuffling case. Hope i might help you. – Crawler Nov 09 '14 at 05:08