Results 1 to 4 of 4
  1. #1
    javaa is offline Member
    Join Date
    Aug 2012
    Posts
    28
    Rep Power
    0

    Default what is wrong with this shuffle method??

    Hey guys!!! for some reason my program is displaying stuff i dont want it to display so if you could help determine what is wrong with this method that would be great. thanks!!!

    Java Code:
    	public void shuffle() {
    		for (int i = 0; i < 200; i++) {
    			int randomCard = (int)(Math.random() * 52);
    			int randomCard2 = (int)(Math.random() * 52);
    
    			Image temp = cards[randomCard2];
    			cards[randomCard2] = cards[randomCard];
    			cards[randomCard] = temp;
    		}
    	}

  2. #2
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,789
    Rep Power
    7

    Default Re: what is wrong with this shuffle method??

    What do you mean by "stuff"? We cannot see your screen so you need to post it here.

    {Prediction} Are you getting something like Classname@9ed84a? If so that is the output from the toString method in the Object class. All classes inherit this method and you need to override it in any class you write to return meaningful data.

  3. #3
    javaa is offline Member
    Join Date
    Aug 2012
    Posts
    28
    Rep Power
    0

    Default Re: what is wrong with this shuffle method??

    Hi! sorry for not being clear. I am trying to display two rows of 5 cards but it appears as though some blank cards are showing up. Here is the whole code if you need it:
    Java Code:
    import java.awt.*;
    import java.applet.Applet;
    
    public class Unit12CTYassignment extends Applet {
    	private Image[] cards = new Image[52];
    
    	public void init(){		// Create a card array
    		String images = "images/";
    
    		for (int i = 0; i < 52; i++) {
    			int j = i + 1;
    
    			if ( i < 13 ) {
    				if (i < 10) 
    					cards[i] = getImage( getDocumentBase(), images + "c" + j + ".gif" );
    				else if (i == 10)
    					cards[i] = getImage( getDocumentBase(), images + "c" + "j.gif" );
    				else if (i == 11)
    					cards[i] = getImage( getDocumentBase(), images + "c" + "k.gif" );
    				else if (i == 12)
    					cards[i] = getImage( getDocumentBase(), images + "c" + "q.gif" );
    			}
    
    			else if ( i < 26 ) {
    				if (i < 23)
    					cards[i] = getImage( getDocumentBase(), images + "d" + j + ".gif" );
    				else if (i == 23)
    					cards[i] = getImage( getDocumentBase(), images + "d" + "j.gif" );
    				else if (i == 24)
    					cards[i] = getImage( getDocumentBase(), images + "d" + "k.gif" );
    				else if (i == 25)
    					cards[i] = getImage( getDocumentBase(), images + "d" + "q.gif" );
    			}
    			else if ( i < 39 ) {
    				if (i < 36)
    					cards[i] = getImage( getDocumentBase(), images + "h" + j + ".gif" );
    				else if (i == 36)
    					cards[i] = getImage( getDocumentBase(), images + "h" + "j.gif" );
    				else if (i == 37)
    					cards[i] = getImage( getDocumentBase(), images + "h" + "k.gif" );
    				else if (i == 38)
    					cards[i] = getImage( getDocumentBase(), images + "h" + "q.gif" );
    			}
    			else {
    				if (i <= 49)
    					cards[i] = getImage( getDocumentBase(), images + "s" + j + ".gif" );
    				else if (i == 49)
    					cards[i] = getImage( getDocumentBase(), images + "s" + "j.gif" );
    				else if (i == 50)
    					cards[i] = getImage( getDocumentBase(), images + "s" + "k.gif" );
    				else if (i == 51)
    					cards[i] = getImage( getDocumentBase(), images + "s" + "q.gif" );
    			}
    		}
    
    		shuffle();
    	}
    
    	public void paint(Graphics screen) {
    		for (int i = 0; i < 10; i++) {
    			if (i < 5)
    				screen.drawImage( cards[i], 5 + (i*75) , 5, this);
    			else
    	 			screen.drawImage( cards[i], 5 + ((i-5)*75) , 105, this);
    		}
    	}
    
    	public void shuffle() {
    		for (int i = 0; i < 200; i++) {
    			int randomCard = (int)(Math.random() * 52);
    			int randomCard2 = (int)(Math.random() * 52);
    
    			Image temp = cards[randomCard2];
    			cards[randomCard2] = cards[randomCard];
    			cards[randomCard] = temp;
    		}
    	}
    }

  4. #4
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,331
    Rep Power
    25

    Default Re: what is wrong with this shuffle method??

    Please post the output from the program so we can see what you are seeing.
    Since we don't have the images you are using we can't execute the code to see it.

    Also print out the contents of the cards array before and after calling shuffle. You can use the Arrays class's toString() method to format the array for printing.
    Last edited by Norm; 09-06-2012 at 02:32 AM.
    If you don't understand my response, don't ignore it, ask a question.

Similar Threads

  1. Whats wrong with this tiny method?
    By N00Bie in forum New To Java
    Replies: 6
    Last Post: 02-17-2011, 09:08 PM
  2. What's wrong with this method?
    By jazzermonty in forum New To Java
    Replies: 2
    Last Post: 01-17-2011, 07:16 PM
  3. method return statement is wrong
    By ftrengnr in forum New To Java
    Replies: 1
    Last Post: 10-19-2010, 04:15 AM
  4. What did i do wrong on thing method?
    By PureAwesomeness in forum New To Java
    Replies: 9
    Last Post: 03-08-2009, 08:37 AM
  5. Shuffle Group Method
    By xelo in forum New To Java
    Replies: 6
    Last Post: 01-05-2009, 07:41 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •