Why So Scared

A blog about; Programming, Music and Random Stuff

Java Code – Shuffle A Pack Of Cards

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
import java.awt.*;
import java.awt.event.*;
import java.util.Random;
import javax.swing.*;

public class shuffle {

  public shuffle(){
    window.setSize(1100,550);
    window.setLayout(new FlowLayout());
    JLabel cards[] = new JLabel[52];
  int number = 0;
  int [] ranarray = new int[52];
  boolean used[] = new boolean[52] ;

  Random generator = new Random();

  for (int i = 0; i < 52; i++) {
    do { number = generator.nextInt(52); }
      while(!used[number] == false); //while number has been used before try again
        {
           ranarray[i] = number; //unique number? add it to the array
           used[number] = true; //set number to true for comparison
        }
     }

    for (int i = 0; i < 52; i++) {
    cards[ranarray[i]] = new JLabel(new ImageIcon("cards/" + ranarray[i] + ".png")); //display cards
    window.add(cards[ranarray[i]]); //add cards
     }

    //show the window
    window.setVisible(true);

    }

  private JFrame window = new JFrame("Shuffle");

  //open Shuffle
  public static void main(String[] args){
    new shuffle();

  }

}

Copy of code hosted at http://pastie.org/755948

Code saved as Shuffle.java

posted by Juo in Java and have Comment (1)
Tags:

One Response to “Java Code – Shuffle A Pack Of Cards”

  1. Webmaster says:

    Hello! Please e-mail me your contacts. I have a question webmaster@spottovo.ru” rel=”nofollow”>……

    Thank you!!!…

Place your comment

Please fill your data and comment below.
Name
Email
Website
Your comment