Quantcast
Channel: Programming Forums
Viewing all articles
Browse latest Browse all 51036

How to rerun a program with a loop

$
0
0
I have a program that randomly generates 5 playing cards from a 52 card deck. The program runs fine. Each card is displayed in a separate panel. The next objective I'm trying to accomplish is how to guarantee that the cards will never be duplicated.

I'm still very new to programming, so keep that in mind if some of this doesn't make sense. I've tried using if/else loops at the end of each panel but that didn't work. I used nested if loops and those actually stopped the card from being populated if it was a duplicate, but I couldn't get it to generate a new card.

So here is my code to the whole program (sorry if it's too long). This is my first post here so I probably don't know all the ins and outs of what to post.

public static void main(String[] args) {
 
        JFrame frame = new JFrame("Playing Cards");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
 
        //*********************************************************************
        // This is the array list of all 52 cards. 
       
        
        ArrayList<String> number = new ArrayList<String>();
        
        number.add("s1.gif");
        number.add("s2.gif");
        number.add("s3.gif");
        number.add("s4.gif");
        number.add("s5.gif");
        number.add("s6.gif");
        number.add("s7.gif");
        number.add("s8.gif");
        number.add("s9.gif");
        number.add("s10.gif");
        number.add("sj.gif");
        number.add("sq.gif");
        number.add("sk.gif");
        number.add("d1.gif");
        number.add("d2.gif");
        number.add("d3.gif");
        number.add("d4.gif");
        number.add("d5.gif");
        number.add("d6.gif");
        number.add("d7.gif");
        number.add("d8.gif");
        number.add("d9.gif");
        number.add("d10.gif");
        number.add("dj.gif");
        number.add("dq.gif");
        number.add("dk.gif");
        number.add("c1.gif");
        number.add("c2.gif");
        number.add("c3.gif");
        number.add("c4.gif");
        number.add("c5.gif");
        number.add("c6.gif");
        number.add("c7.gif");
        number.add("c8.gif");
        number.add("c9.gif");
        number.add("c10.gif");
        number.add("cj.gif");
        number.add("cq.gif");
        number.add("ck.gif");
        number.add("h1.gif");
        number.add("h2.gif");
        number.add("h3.gif");
        number.add("h4.gif");
        number.add("h5.gif");
        number.add("h6.gif");
        number.add("h7.gif");
        number.add("h8.gif");
        number.add("h9.gif");
        number.add("h10.gif");
        number.add("hj.gif");
        number.add("hq.gif");
        number.add("hk.gif");
        
        //*********************************************************************
        // This is the panel for the first card
       
        
        JPanel subPanel1 = new JPanel();
        subPanel1.setPreferredSize(new Dimension (110, 150));
        subPanel1.setBackground(Color.green);
               
        Random num = new Random();
        for (int i = 0; i < 1; i++) {
        int value = num.nextInt(52) + 1;
        System.out.println(value);
        
        subPanel1.add(new JLabel(new ImageIcon(number.get(value))));
        
        //*********************************************************************
        // This is the panel for the second card
        
        
        JPanel subPanel2 = new JPanel();
        subPanel2.setPreferredSize(new Dimension (110, 150));
        subPanel2.setBackground(Color.green);
                     
        Random num2 = new Random();
        for (int i2 = 0; i2 < 1; i2++) {
        int value2 = num.nextInt(52) + 1;
        System.out.println(value2);
        
        subPanel2.add(new JLabel(new ImageIcon(number.get(value2))));
        
        //*********************************************************************
        // This is the panel for the third card
        
        
        JPanel subPanel3 = new JPanel();
        subPanel3.setPreferredSize(new Dimension (110, 150));
        subPanel3.setBackground(Color.green);
        
        Random num3 = new Random();
        for (int i3 = 0; i3 < 1; i3++) {
        int value3 = num.nextInt(52) + 1;
        System.out.println(value3);
        
        subPanel3.add(new JLabel(new ImageIcon(number.get(value3))));
        
        //*********************************************************************
        // This is the panel for the fourth card
        
        
        JPanel subPanel4 = new JPanel();
        subPanel4.setPreferredSize(new Dimension (110, 150));
        subPanel4.setBackground(Color.green);
        
        Random num4 = new Random();
        for (int i4 = 0; i4 < 1; i4++) {
        int value4 = num.nextInt(52) + 1;
        System.out.println(value4);
        
        subPanel4.add(new JLabel(new ImageIcon(number.get(value4))));
        
        //*********************************************************************
        // This is the panel for the fifth card
        
        
        JPanel subPanel5 = new JPanel();
        subPanel5.setPreferredSize(new Dimension (110, 150));
        subPanel5.setBackground(Color.green);
        
        Random num5 = new Random();
        for (int i5 = 0; i5 < 1; i5++) {
        int value5 = num.nextInt(52) + 1;
        System.out.println(value5);
        
        subPanel5.add(new JLabel(new ImageIcon(number.get(value5))));
        
        //*********************************************************************
        // Reshuffle button
       

        
        
        //*********************************************************************
        // Primary panel containing all the subpanels/cards
        
        
        JPanel primary = new JPanel();
        primary.setBackground(Color.green);
        primary.add(subPanel1);
        primary.add(subPanel2);
        primary.add(subPanel3);
        primary.add(subPanel4);
        primary.add(subPanel5);
        frame.getContentPane().add(primary);
        frame.pack();
        frame.setVisible(true);
        
        //*********************************************************************
        // The following link is where the card images were downloaded from:
        //                 http://www.jfitz.com/cards/
        //*********************************************************************
        
        
        
    }
}
}
}
}
}
}


Oops! I forgot to explain all of my question! Sorry. I am trying to use an if loop at the end of each panel to see if the value generated is the same as the previous panel. If it is, then have the loop rerun the random generator. When I try to put the random generator in the if loop, it is full of errors saying the variables have already been previously assigned. Is this the best way to go about doing this ?

Viewing all articles
Browse latest Browse all 51036

Latest Images

Trending Articles



Latest Images

<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>