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

Deck of Cards Applet how to shuffle and display

$
0
0
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 );
        }
     }
  }
}


Viewing all articles
Browse latest Browse all 51036

Trending Articles



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