Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: Please Help Me Fix

  1. #1
    Junior Member
    Join Date
    Nov 2009
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Please Help Me Fix

    I am making a deck of cards and then trying to shuffle them.

    Here is my class for a card.
    public class Card{
    	int rank,suit;
     
    	public Card(int s, int n){ 
    		rank = n;
    		suit = s;
    	}
     
    	public int getRank(){
    		return rank;
    	}
     
    	public void setRank(int n){
    		rank = n;
    	}
     
    	public int getSuit(){
    		return suit;
    	}
     
    	public void setSuit(int s){
    		suit = s;
    	}
     
    	public String toString(){
    		String asdf = "";
    		String fdsa = "";
     
    		if(getSuit() == 0){
    			asdf += "Spades";
    		}
    		else if(getSuit() == 1){
    			asdf += "Diamonds";
    		}
    		else if(getSuit() == 2){
    			asdf += "Clubs";
    		}
    		else{
    			asdf += "Hearts";
    		}
     
     
    		if(getRank() == 0){
    			fdsa += "2";
    		}
    		else if(getRank() == 1){
    			fdsa += "3";
    		}
    		else if(getRank() == 2){
    			fdsa += "4";
    		}
    		else if(getRank() == 3){
    			fdsa += "5";
    		}
    		else if(getRank() == 4){
    			fdsa += "6";
    		}
    		else if(getRank() == 5){
    			fdsa += "7";
    		}
    		else if(getRank() == 6){
    			fdsa+= "8";
    		}
    		else if(getRank() == 7){
    			fdsa += "9";
    		}
    		else if(getRank() == 8){
    			fdsa += "10";
    		}
    		else if(getRank() == 9){
    			fdsa += "Jack";
    		}
    		else if(getRank() == 10){
    			fdsa += "Queen";
    		}
    		else if(getRank() == 11){
    			fdsa += "King";
    		}
    		else if(getRank() == 12){
    			fdsa += "Ace";
    		}
     
    		return fdsa + " of " + asdf;
    	}
    }


    Here is a class for the deck.
    public class Deck{
     
    		Card[] theDeck;  //decalres an array of integers.
    		Card[] deckShuffled;  //decalres an array of integers.
    		Card[] pickedNums;  //decalres an array of integers.
     
    	public Deck(){
     
    		theDeck = new Card[52];  //allocates memory for 52 cards.
    		//This is the deck untouched.
     
    		int x = 0;
     
    		for(int i=0; i<4; i++){
     
    			for(int c=0; c<13; c++){
     
    				theDeck[x] = new Card(i,c);
    				System.out.println(theDeck[x]);
    				x++;
     
    			}
    		}
    		//This has just created all of the cards in order.
     
    		deckShuffled = new Card[52];  //allcates memory for 52 cards.
    		/*This is where the shuffled deck will be, cards are randomly placed
    		here from the deck.*/
     
    		pickedNums = new Card[52];  //allocates memory for 52 cards.
    		/*This will contain the cards that were transfered from deck to 
    		deckShuffled and will be used to stop cards from being used multiple times.
    		*/
     
    		int[] PickedNums = new int[52];
    		for(int i =0; i<PickedNums.length; i++){
    			PickedNums[i] = -1;
    		}
     
    		int pickedNumsIndex = 0, shuffledNumsIndex = 0, num = 0;
    		boolean hasBeenPicked = true;
    		while(shuffledNumsIndex<=51){
    			System.out.println("shuffled nums index: "+shuffledNumsIndex);
    			while(hasBeenPicked){
    				num = (int)(Math.random()*52.0);
    				System.out.println(num);
    				for(int c=0; c <= pickedNumsIndex; c++){
    					if(num == PickedNums[c]){
    						System.out.println("num has been found at: "+c);
    						hasBeenPicked = false;
    					}
    					else{
    						hasBeenPicked = true;
    						break;
    					}
     
    				}
    			}
    			hasBeenPicked = true;
    			PickedNums[pickedNumsIndex] = num;
    			pickedNumsIndex++;
     
    			deckShuffled[shuffledNumsIndex] = theDeck[num];
    			shuffledNumsIndex++;
    		}
    	theDeck = deckShuffled;
     
    	}
     
     
     
    }

    I am having trouble with my deck shuffling. It seems to be stuck in an infinate loop, and I am not sure how to fix it. I printed out num to show the infinate loop, you can just delete that if you don't need it. Please show me how to fix it using my code, not by introducing entirely new concepts. I am new to java and may not be using the most eficient methods to solve problems.



    Here is my driver.
    public class DeckDriver{
    	public static void main(String[]args){
     
    		Deck asdf = new Deck();
    	}
    }


  2. #2
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,301
    Thanks
    180
    Thanked 824 Times in 767 Posts
    Blog Entries
    5

    Default Re: Please Help Me Fix

    The way it is written you may never break out of the loop because you are resetting your boolean before you get a chance to break out or continue the loop. One way to write it to overcome this would be:
    			while(hasBeenPicked){
    				num = (int)(Math.random()*52.0);
    				System.out.println(num);
    				for(int c=0; c <= pickedNumsIndex; c++){
    					if(num == PickedNums[c]){
    						System.out.println("num has been found at: "+c);
    						continue;						
    					}				
    				}
    				hasBeenPicked = false;
     
    			}

    You may wish to write a function to check the contents of an integer within an array and call that function from your while loop. This may have the advantage that it could be used for other purposes, such finding the index of a certain number...

  3. #3
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,896
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: Please Help Me Fix

    The continue will skip through the rest of the for loop, rather than break out. A simple work-around is such: instead of shuffling the deck, have a deck (sorted or not), then pick a card out at random when someone asks to draw a card. To the computer there's no difference, and it's convincing enough that the computer didn't "cheat" on dealing out the card.

    public Card dealCard(LinkedList<Card> deck)
    {
         return deck.remove(new Random().nextInt(deck.size()));
    }
    If you must absolutely have a shuffled deck, then here's a simple algorithm:

    1. start with card 0 (the top card).
    2. generate a random integer [0-51]. This is the card you're going to swap 0 with.
    3. repeat step 2 a lot of times (maybe ~200-300, there's a point you'll be getting diminishing returns). It's not necessary to advance the spot you're looking at.

    public void shuffle(Card[] deck, int swapsToDo)
    {
         Random generator = new Random();
         for (int i = 0; i < swapsToDo; i++)
         {
              int position = generator.nextInt(52);
              Card temp = deck[position];
              deck[position] = deck[0];
              deck[0] = temp;
         }
    }
    Last edited by helloworld922; November 24th, 2009 at 11:48 PM.

  4. #4
    Super Moderator Json's Avatar
    Join Date
    Jul 2009
    Location
    Warrington, United Kingdom
    Posts
    1,274
    My Mood
    Happy
    Thanks
    70
    Thanked 156 Times in 152 Posts

    Default Re: Please Help Me Fix

    Is there not several other threads on this forum about Decks of cards?

    Edit:
    http://www.javaprogrammingforums.com...-up-array.html
    http://www.javaprogrammingforums.com...exception.html
    http://www.javaprogrammingforums.com...-2d-array.html
    http://www.javaprogrammingforums.com...exception.html

    // Json
    Last edited by Json; November 25th, 2009 at 02:40 AM. Reason: Found em :)

  5. The Following User Says Thank You to Json For This Useful Post:

    Mr.cool (December 16th, 2009)