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 6 of 6

Thread: HashMap Shuffle Card

  1. #1
    Junior Member
    Join Date
    Jun 2011
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default HashMap Shuffle Card

    Hi can u help me what's wrong in my code.
    I used a HasMap Method for shuffling of Cards.
    but it has still a wrong code. and it is in the HashMap Method.

    Here's my code:

    import java.util.*;
     
    public class WarGame {
    	public static void main(String[] args) {
     
    		String deckInitial = "A/D, K/D, Q/D, J/D, 10/D, 9/D, 8/D, 7/D, 6/D, 5/D, 4/D, 3/D, 2/D, A/H, K/H, Q/H, J/H, 10/H, 9/H, 8/H, 7/H, 6/H, 5/H, 4/H, 3/H, 2/H, A/S, K/S, Q/S, J/S, 10/S, 9/S, 8/S, 7/S, 6/S, 5/S, 4/S, 3/S, 2/S, A/C, K/C, Q/C, J/C, 10/C, 9/C, 8/C, 7/C, 6/C, 5/C, 4/C, 3/C, 2/C";
    		String deckRank = "A/D, A/H, A/S, A/C, K/D, K/H, K/S, K/C, Q/D, Q/H, Q/S, Q/C, J/D, J/H, J/S, J/C, 10/D, 10/H, 10/S, 10/C, 9/D, 9/H, 9/S, 9/C, 8/D, 8/H, 8/S, 8/C, 7/D, 7/H, 7/S, 7/C, 6/D, 6/H, 6/S, 6/C, 5/D, 5/H, 5/S, 5/C, 4/D, 4/H, 4/S, 4/C, 3/D, 3/H, 3/S, 3/C, 2/D, 2/H, 2/S, 2/C";
     
    		System.out.println ("Deck of Cards - Initial: " + deckInitial);
     
    		HashMap <String, Integer> deck1 = new HashMap <String, Integer> ();
    		HashMap <String, Integer> rank1 = new HashMap <String, Integer> ();
     
     
    		StringTokenizer str1 = new StringTokenizer (deckInitial, ",");
    		while (str1.hasMoreTokens()){
    		 	for (int i=0; i<52; i++){
    		 		deck1.add(str1.nextToken(), (i));
    		 	}
    		}
     
    		StringTokenizer str2 = new StringTokenizer (deckRank, ",");
    		while (str2.hasMoreTokens()){
    		 	for (int i=0; i<52; i++){
    		 		rank1.add(str2.nextToken(), (i));
    		 	}
    		}
     
     
    		HashMap<String, Integer> rd = new HashMap<String, Integer>();
    		for (int i=0; i<52; i++){
    			rd.add(rank1.pop(), (i));
    		}
     
    		//Shuffle
    		Integer shuffleInput = null;
     
    		HashMap<String, Integer> deck2 = new HashMap<String, Integer>();
    		HashMap<String, Integer> deck3 = new HashMap<String, Integer>();
    		HashMap<String, Integer> deckFinal = new HashMap<String, Integer>();
     
    		Scanner input = new Scanner (System.in);
    		System.out.print("\nEnter number of Shuffle: ");
     
    		if (input.hasNextInt()){
    			shuffleInput = input.nextInt();
    						}
     
    		while(shuffleInput != 0){
    			do {
    				deck2.add(deck1.pop());
    					}while (deck2.size() != 26);
     
    			do {
    				deck3.add(deck1.pop());
    					}while (deck3.size() != 26);
     
    			do{
    				deckFinal.add(deck2.pop());
    				deckFinal.add(deck3.pop());
    					}while(!deck3.isEmpty() && !deck2.isEmpty());
    						shuffleInput--;
     
    					if(shuffleInput != 0){
    						deck1 = deckFinal;
    					}
    				}
     
    		System.out.println("\nDeck of Cards - Shuffled: " + deckFinal);
    	}
     
    }


  2. #2
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,545
    My Mood
    Grumpy
    Thanks
    0
    Thanked 167 Times in 158 Posts

    Default Re: HashMap Shuffle Card

    Quote Originally Posted by d'fitz View Post
    but it has still a wrong code
    Do you honestly think that people will be able to help without a more detailed explanation than that?

    Why are you using HashMaps at all? Just create a Card class that has a rank and suit. Then have a List of Card objects. Much cleaner.

  3. #3
    Junior Member
    Join Date
    Jun 2011
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: HashMap Shuffle Card

    we are required to use HashMap so we can have a key and value for each card.
    so that when the time comes that we will deal/compare the cards, it will have a key and value.

    I'm just a beginner here in java though.
    So I'm having a hard time. Btw, thanks for d answer.

  4. #4
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,545
    My Mood
    Grumpy
    Thanks
    0
    Thanked 167 Times in 158 Posts

    Default Re: HashMap Shuffle Card

    OK so you are required to use HashMap but why do have six of them? You only need one. Clean your code up and you might be able to see things more clearly.

  5. #5
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,545
    My Mood
    Grumpy
    Thanks
    0
    Thanked 167 Times in 158 Posts

    Default Re: HashMap Shuffle Card

    Another thing, shuffling a Map makes no sense. The items are placed into "buckets" based on some hashing funtion. They are not stored in any particular order thus they cannot be shuffled.

  6. #6
    Forum old-timer
    Join Date
    Nov 2008
    Location
    Faversham, Kent, UK
    Posts
    472
    My Mood
    Mellow
    Thanks
    4
    Thanked 58 Times in 54 Posts

    Default Re: HashMap Shuffle Card

    Surely it would be better design to have a Card class containing both the value and name/description/key? Then you can put them into a deck List (e.g. ArrayList) and sort or shuffle them however you want using the Collections API.

Similar Threads

  1. [SOLVED] Should i use a hashmap?
    By 6Sloth9 in forum Collections and Generics
    Replies: 2
    Last Post: May 1st, 2011, 08:58 PM
  2. TreeMap vs HashMap
    By Kerr in forum Collections and Generics
    Replies: 7
    Last Post: March 10th, 2011, 10:12 AM
  3. Problem in HashMap
    By Hikari9 in forum Collections and Generics
    Replies: 2
    Last Post: April 19th, 2010, 10:44 PM
  4. Bitstrings vs Hashmap
    By April in forum Collections and Generics
    Replies: 3
    Last Post: February 2nd, 2010, 11:56 AM
  5. [SOLVED] I am trying to shuffle up an array
    By rsala004 in forum Collections and Generics
    Replies: 3
    Last Post: July 18th, 2009, 03:31 PM