# Deck of Cards Applet how to shuffle and display

• (2 Pages)
• 1
• 2

## 27 Replies - 2984 Views - Last Post: 17 January 2013 - 10:25 PMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=307627&amp;s=2511e298da93f3c54a5b91ff118f243c&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 bwong23

Reputation: -1
• Posts: 29
• Joined: 14-January 13

# Deck of Cards Applet how to shuffle and display

Posted 16 January 2013 - 11:28 PM

I'm writing an applet to shuffle 54 cards that I downloaded from an "images" folder, but I'm having trouble how to shuffle and then display the corresponding cards. So far, I know I need to generate random numbers in order to get different cards (10), but have trouble shuffling and how to have those randomly generated numbers relate to the array of cards I have. Any help is much appreciated and bear with me about the long and stylistically ugly code, I'm a beginner! (As you all can probably tell)

```
import java.awt.Graphics;
import java.awt.Image;
import java.applet.Applet;

public class DisplayCards extends Applet {

Image 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, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55; //declared each card

public void init ( ) {

1 = getImage ( getDocumentBase ( ), "images/c1.gif" ); //initialized each individual card
2 = getImage ( getDocumentBase ( ), "images/c2.gif" );
3 = getImage ( getDocumentBase ( ), "images/c3.gif" );
4 = getImage ( getDocumentBase ( ), "images/c4.gif" );
5 = getImage ( getDocumentBase ( ), "images/c5.gif" );
6 = getImage ( getDocumentBase ( ), "images/c6.gif" );
7 = getImage ( getDocumentBase ( ), "images/c7.gif" );
8 = getImage ( getDocumentBase ( ), "images/c8.gif" );
9 = getImage ( getDocumentBase ( ), "images/c9.gif" );
10 = getImage ( getDocumentBase ( ), "images/c10.gif" );
11 = getImage ( getDocumentBase ( ), "images/cj.gif" );
12 = getImage ( getDocumentBase ( ), "images/cq.gif" );
13 = getImage ( getDocumentBase ( ), "images/ck.gif" );
14 = getImage ( getDocumentBase ( ), "images/d1.gif" );
15 = getImage ( getDocumentBase ( ), "images/d2.gif" );
16 = getImage ( getDocumentBase ( ), "images/d3.gif" );
17 = getImage ( getDocumentBase ( ), "images/d4.gif" );
18 = getImage ( getDocumentBase ( ), "images/d5.gif" );
19 = getImage ( getDocumentBase ( ), "images/d6.gif" );
20 = getImage ( getDocumentBase ( ), "images/d7.gif" );
21 = getImage ( getDocumentBase ( ), "images/d8.gif" );
22 = getImage ( getDocumentBase ( ), "images/d9.gif" );
23 = getImage ( getDocumentBase ( ), "images/d10.gif" );
24 = getImage ( getDocumentBase ( ), "images/dj.gif" );
25 = getImage ( getDocumentBase ( ), "images/dq.gif" );
26 = getImage ( getDocumentBase ( ), "images/dk.gif" );
27 = getImage ( getDocumentBase ( ), "images/ec.gif" );
28 = getImage ( getDocumentBase ( ), "images/h1.gif" );
29 = getImage ( getDocumentBase ( ), "images/h2.gif" );
30 = getImage ( getDocumentBase ( ), "images/h3.gif" );
31 = getImage ( getDocumentBase ( ), "images/h4.gif" );
32 = getImage ( getDocumentBase ( ), "images/h5.gif" );
33 = getImage ( getDocumentBase ( ), "images/h6.gif" );
34 = getImage ( getDocumentBase ( ), "images/h7.gif" );
35 = getImage ( getDocumentBase ( ), "images/h8.gif" );
36 = getImage ( getDocumentBase ( ), "images/h9.gif" );
37 = getImage ( getDocumentBase ( ), "images/h10.gif" );
38 = getImage ( getDocumentBase ( ), "images/hj.gif" );
39 = getImage ( getDocumentBase ( ), "images/hq.gif" );
40 = getImage ( getDocumentBase ( ), "images/hk.gif" );
41 = getImage ( getDocumentBase ( ), "images/jb.gif" );
42 = getImage ( getDocumentBase ( ), "images/s1.gif" );
43 = getImage ( getDocumentBase ( ), "images/s2.gif" );
44 = getImage ( getDocumentBase ( ), "images/s3.gif" );
45 = getImage ( getDocumentBase ( ), "images/s4.gif" );
46 = getImage ( getDocumentBase ( ), "images/s5.gif" );
47 = getImage ( getDocumentBase ( ), "images/s6.gif" );
48 = getImage ( getDocumentBase ( ), "images/s7.gif" );
49 = getImage ( getDocumentBase ( ), "images/s8.gif" );
50 = getImage ( getDocumentBase ( ), "images/s9.gif" );
51 = getImage ( getDocumentBase ( ), "images/s10.gif" );
52 = getImage ( getDocumentBase ( ), "images/sj.gif" );
53 = getImage ( getDocumentBase ( ), "images/sq.gif" );
54 = getImage ( getDocumentBase ( ), "images/sk.gif" );
55 = getImage ( getDocumentBase ( ), "images/jk.gif" );

int [ ] deck = new int {54};

for ( int i = 0; i<deck.length; i++)//initialize cards here?
deck[ i ] = i;

for ( int i = 0; i < deck.length; i++ ) {
int index = (int) (Math.random ( ) * deck.length ) ; //tried to shuffle here
int temp = deck [ i ];
deck [ i ] = deck [ index ];
deck [ index ] = temp;
}

public void paint ( Graphics g ) { //try to display the ten random cards
for ( int i = 0; i < 10; i ++ ) {
g.drawImage (deck[i], 10, 10, this );
g.drawImage (deck[i], 10, 10, this );
g.drawImage (deck[i], 10, 10, this );
g.drawImage (deck[i], 10, 10, this );
g.drawImage (deck[i], 10, 10, this );
g.drawImage (deck[i], 10, 30, this );
g.drawImage (deck[i], 10, 30, this );
g.drawImage (deck[i], 10, 30, this );
g.drawImage (deck[i], 10, 30, this );
g.drawImage (deck[i], 10, 30, this );
}
}
}
}

```

Is This A Good Question/Topic? 0

## Replies To: Deck of Cards Applet how to shuffle and display

### #2 macosxnerd101

• Games, Graphs, and Auctions

Reputation: 11391
• Posts: 42,928
• Joined: 27-December 08

## Re: Deck of Cards Applet how to shuffle and display

Posted 16 January 2013 - 11:35 PM

Store your Images in an array or List<Image>. It makes organizing them a lot easier. If you use a List, you can use the Collections.shuffle() method to shuffle the cards, or you can randomly remove() elements.

### #3 bwong23

Reputation: -1
• Posts: 29
• Joined: 14-January 13

## Re: Deck of Cards Applet how to shuffle and display

Posted 16 January 2013 - 11:39 PM

I know people hate when they are asked for code, but could you show what that would look like, not all of the elements, but just like card1, card2, etc. ?

### #4 macosxnerd101

• Games, Graphs, and Auctions

Reputation: 11391
• Posts: 42,928
• Joined: 27-December 08

## Re: Deck of Cards Applet how to shuffle and display

Posted 16 January 2013 - 11:42 PM

If you have a List<Image>, you can just remove() a single random element as follows:
```list.remove(random.nextInt(list.size());

```

The List remove() method returns an element. Just remove() like that for as many cards as you need.

### #5 bwong23

Reputation: -1
• Posts: 29
• Joined: 14-January 13

## Re: Deck of Cards Applet how to shuffle and display

Posted 16 January 2013 - 11:44 PM

That is a lot easier and logical, except I"m required to use a random number generator in order to get the ten cards...any suggestions?

### #6 macosxnerd101

• Games, Graphs, and Auctions

Reputation: 11391
• Posts: 42,928
• Joined: 27-December 08

## Re: Deck of Cards Applet how to shuffle and display

Posted 16 January 2013 - 11:48 PM

That's exactly what that does, only for one card. I think you can figure out how to apply a loop to this.

### #7 bwong23

Reputation: -1
• Posts: 29
• Joined: 14-January 13

## Re: Deck of Cards Applet how to shuffle and display

Posted 16 January 2013 - 11:53 PM

Okay thanks. How would I store all 54 cards in one list, or can I even define them like those images and then put card1, card2, and so on into a list after?

### #8 macosxnerd101

• Games, Graphs, and Auctions

Reputation: 11391
• Posts: 42,928
• Joined: 27-December 08

## Re: Deck of Cards Applet how to shuffle and display

Posted 16 January 2013 - 11:56 PM

Quote

or can I even define them like those images and then put card1, card2, and so on into a list after?

This is pointless. The whole point of a List or an array is to avoid variable names like card1, card2, etc.

Quote

How would I store all 54 cards in one list,

See the add() method. Make sure to read the documentation for Lists as well. The specific implementation you would want to use here is an ArrayList.

### #9 bwong23

Reputation: -1
• Posts: 29
• Joined: 14-January 13

## Re: Deck of Cards Applet how to shuffle and display

Posted 17 January 2013 - 12:10 AM

I still don't understand what you would put in the arrayList then, but thanks anyways.

### #10 macosxnerd101

• Games, Graphs, and Auctions

Reputation: 11391
• Posts: 42,928
• Joined: 27-December 08

## Re: Deck of Cards Applet how to shuffle and display

Posted 17 January 2013 - 12:16 AM

The actual Images. Not sure why you need an int[] and the Images.

```String[] suits = {"c", "s", "h", "d"};
String[] values = {"1", "2", "3", "4", "5", "6", "7", "8", "9", "j", "q", "k"};
ArrayList<Image> images = new ArrayList<Image>();

for(String suit : suits){
for(String value : values){
Image img = getImage ( getDocumentBase ( ), "images/" + suit + value + ".gif" );
}
}

```

### #11 bwong23

Reputation: -1
• Posts: 29
• Joined: 14-January 13

## Re: Deck of Cards Applet how to shuffle and display

Posted 17 January 2013 - 12:25 AM

Ah, much appreciated. So then I can do shuffle ( List images), even though there is no main method?

### #12 macosxnerd101

• Games, Graphs, and Auctions

Reputation: 11391
• Posts: 42,928
• Joined: 27-December 08

## Re: Deck of Cards Applet how to shuffle and display

Posted 17 January 2013 - 12:26 AM

You can use the Collections.shuffle() method, or you can randomly remove elements. Applets (and really you should make sure to use the more modern JApplet) do not have a main() method. They have milestone methods instead.

### #13 bwong23

Reputation: -1
• Posts: 29
• Joined: 14-January 13

## Re: Deck of Cards Applet how to shuffle and display

Posted 17 January 2013 - 12:33 AM

Okay, yeah the online class I"m taking from hasn't updated their curriculum since '99 so it's most likely super-outdated. I've compiled the data like you posted but I'm getting two error messages relating to the ArrayLists.

```import java.awt.Graphics;
import java.awt.Image;
import java.applet.Applet;

public class DeckofCards1 extends Applet {

public void init ( ) {
String[] suits = {"c", "s", "h", "d"};
String[] values = {"1", "2", "3", "4", "5", "6", "7", "8", "9", "j", "q", "k"};

ArrayList<Image> images = new ArrayList<Image>( );
//command prompt says 'ArrayList<Image> images' and 'new ArrayList<Image>( )' are symbols that cannot be found?
for(String suit : suits){
for(String value : values){
Image img = getImage ( getDocumentBase ( ), "images/" + suit + value + ".gif" );
}
}

}
}

```

This post has been edited by macosxnerd101: 17 January 2013 - 12:34 AM
Reason for edit:: Fixed code tags

### #14 macosxnerd101

• Games, Graphs, and Auctions

Reputation: 11391
• Posts: 42,928
• Joined: 27-December 08

## Re: Deck of Cards Applet how to shuffle and display

Posted 17 January 2013 - 12:35 AM

Are you bothering to read the documentation? ArrayList is located in the java.util package. You have to import it.

### #15 bwong23

Reputation: -1
• Posts: 29
• Joined: 14-January 13

## Re: Deck of Cards Applet how to shuffle and display

Posted 17 January 2013 - 12:46 AM

Oh shoot, must have missed that when I was reading it. So now I can use the Collection.shuffle ( images) and randomly remove elements? I"m sorry you have to explain so much, but once I remove ten elements, how do I display those? ( I have one day to do this project so sorry for the frantic questioning)