www.webdeveloper.com
Results 1 to 9 of 9

Thread: How do you shuffle an ArrayList?

Hybrid View

  1. #1
    Join Date
    Jul 2004
    Location
    West Coast, Canada
    Posts
    665

    How do you shuffle an ArrayList?

    How do you shuffle an ArrayList?

    Currently, I got this code:

    Code:
    import java.util.ArrayList;
    import cs1.Keyboard;
    import java.util.Random;
    
    public class DeckOfCards
    {
    	private static int totalCards = 52;
    	public static ArrayList deck = new ArrayList(0);
    	public static Random generator = new Random();
    	
    	static void deal()
    	{
    		totalCards--;
    		int randNum = generator.nextInt(50) + 1;
    		System.out.println("Card Dealt: " + deck.get(randNum));
    	}
    	
    	static void shuffle();
    	{
    		totalCards = 52;
    		//wtf happens here?? lol.
    		
    	}
    	
    	public static void main(String[] args)
    	{
    		
    		String dealChoice;
    		String shuffleChoice;
    
    		String keepPlaying = "y";
    		
    		Card card1;
    		Card card2;
    		Card card3;
    		Card card4;
    		Card card5;
    		Card card6;
    		Card card7;
    		Card card8;
    		Card card9;
    		Card card10;
    		Card card11;
    		Card card12;
    		Card card13;
    		Card card14;
    		Card card15;
    		Card card16;
    		Card card17;
    		Card card18;
    		Card card19;
    		Card card20;
    		Card card21;
    		Card card22;
    		Card card23;
    		Card card24;
    		Card card25;
    		Card card26;
    		Card card27;
    		Card card28;
    		Card card29;
    		Card card30;
    		Card card31;
    		Card card32;
    		Card card33;
    		Card card34;
    		Card card35;
    		Card card36;
    		Card card37;
    		Card card38;
    		Card card39;
    		Card card40;
    		Card card41;
    		Card card42;
    		Card card43;
    		Card card44;
    		Card card45;
    		Card card46;
    		Card card47;
    		Card card48;
    		Card card49;
    		Card card50;
    		Card card51;
    		Card card52;
    
    		
    		card1 = new Card(1, 1);
    		card2 = new Card(2, 1);
    		card3 = new Card(3, 1);
    		card4 = new Card(4, 1);
    		card5 = new Card(5, 1);
    		card6 = new Card(6, 1);
    		card7 = new Card(7, 1);
    		card8 = new Card(8, 1);
    		card9 = new Card(9, 1);
    		card10 = new Card(10, 1);
    		card11 = new Card(11, 1);
    		card12 = new Card(12, 1);
    		card13 = new Card(13, 1);
    		card14 = new Card(1, 2);
    		card15 = new Card(2, 2);
    		
    		card16 = new Card(3, 2);
    		card17 = new Card(4, 2);
    		card18 = new Card(5, 2);
    		card19 = new Card(6, 2);
    		card20 = new Card(7, 2);
    		card21 = new Card(8, 2);
    		card22 = new Card(9, 2);
    		card23 = new Card(10, 2);
    		card24 = new Card(11, 2);
    		card25 = new Card(12, 2);
    		card26 = new Card(13, 2);
    		card27 = new Card(1, 3);
    		card28 = new Card(2, 3);
    		card29 = new Card(3, 3);
    		card30 = new Card(4, 3);
    		card31 = new Card(5, 3);
    		card32 = new Card(6, 3);
    		card33 = new Card(7, 3);
    		card34 = new Card(8, 3);
    		card35 = new Card(9, 3);
    		card36 = new Card(10, 3);
    		card37 = new Card(11, 3);
    		card38 = new Card(12, 3);
    		card39 = new Card(13, 3);
    		card40 = new Card(1, 4);
    		card41 = new Card(2, 4);
    		card42 = new Card(3, 4);
    		card43 = new Card(4, 4);
    		card44 = new Card(5, 4);
    		card45 = new Card(6, 4);
    		card46 = new Card(7, 4);
    		card47 = new Card(8, 4);
    		card48 = new Card(9, 4);
    		card49 = new Card(10, 4);
    		card50 = new Card(11, 4);
    		card51 = new Card(12, 4);
    		card52 = new Card(13, 4);		
    		
    		//Clubs
    		deck.add(card1);
    		deck.add(card2);
    		deck.add(card3);
    		deck.add(card4);
    		deck.add(card5);
    		deck.add(card6);
    		deck.add(card7);
    		deck.add(card8);
    		deck.add(card9);
    		deck.add(card10);
    		deck.add(card11);
    		deck.add(card12);
    		deck.add(card13);
    		
    		//Diamonds
    		deck.add(card14);
    		deck.add(card15);
    		deck.add(card16);
    		deck.add(card17);
    		deck.add(card18);
    		deck.add(card19);
    		deck.add(card20);
    		deck.add(card21);
    		deck.add(card22);
    		deck.add(card23);
    		deck.add(card24);
    		deck.add(card25);
    		deck.add(card26);
    
    		//Hearts
    		deck.add(card27);
    		deck.add(card28);
    		deck.add(card29);
    		deck.add(card30);
    		deck.add(card31);
    		deck.add(card32);
    		deck.add(card33);
    		deck.add(card34);
    		deck.add(card35);
    		deck.add(card36);
    		deck.add(card37);
    		deck.add(card38);
    		deck.add(card39);
    		      
    		//Spades
    		deck.add(card40);
    		deck.add(card41);
    		deck.add(card42);
    		deck.add(card43);
    		deck.add(card44);
    		deck.add(card45);
    		deck.add(card46);
    		deck.add(card47);
    		deck.add(card48);
    		deck.add(card49);
    		deck.add(card50);
    		deck.add(card51);
    		deck.add(card52);
    						      
    	do {
    		// *** print the contents of the cart object using println
    		System.out.print("Deal? (y/n): ");
    		if (dealChoice.equals("y")) 
    		{
    			deal();
    		}
    		else{
    			System.out.print("Shuffle Deck? (y/n): ");
    			if (shuffleChoice.equals("y"))
    			{
    				shuffle();
    			}
    			else
    			{
    				System.out.println("Bah.");
    			}
    		}
    				
                
    		System.out.print ("Continue shopping (y/n)? ");
    		keepPlaying = Keyboard.readString();
                
    		} while (keepPlaying.equals("y"));
    	}
    }
    Kind regards,
    Jaime Bueza
    Last edited by BuezaWebDev; 11-21-2004 at 12:28 AM.

    "Everything in a web browser."

  2. #2
    Join Date
    Jan 2004
    Location
    Melbourne, Australia
    Posts
    5,298
    Woah!

    I would suggest you setup an array of Cards, rather than 52 seperate Cards. Or load the data from a seperate flat file

    It would be best to actually subclass ArrayList or List and make your own Deck object.

    There is many ways to shuffle the Deck. You could generate a few random numbers (between 1 to 52), which will be used to deal the next bunch of cards. Or you could actually devise some type of formula to grab random indexes and swap them over.

    Regards.

  3. #3
    Join Date
    Jul 2004
    Location
    West Coast, Canada
    Posts
    665
    Hrmm, the instructions specifically say to make a deck of 52 card objects. :O

    So, am I doing that correctly? Because I have a knack to not follow directions properly. Heheh.

    Hrm, about that formula to swap a few cards in and out. Hrm. Argh, that's hard to think of. I wish the ArrayList class had a method to randomize the indexes.

    Hehe, thanks buntine!

    Kind regards,
    J

    "Everything in a web browser."

  4. #4
    Join Date
    Nov 2004
    Posts
    29
    that is a lot of code. holy night. If i get a chance later i will post the code that is in my java book, that has code for shuffling cards. And no offense, but its a lil easier to read, and smaller

  5. #5
    Join Date
    Jan 2004
    Location
    Melbourne, Australia
    Posts
    5,298
    What I like to do is write two versions of the assignment or task; one to the lecturer's specifications and one to my specifications. Pretty sad, but it seems to pay off.

    Regards.

  6. #6
    Join Date
    Jul 2004
    Location
    Canada, eh
    Posts
    784
    Just because they said to make a deck of 52 card objects does not mean you have to write it like
    Code:
    Card card1;
    Card card2;
    ...
    
    card1=new Card(1, 1);
    card2=new Card(1, 2);
    ...
    You could make it like
    Code:
    ArrayList deck=new ArrayList(); //the deck
    Card cards[]=new Card[52]; //array to keep the cards in
    for(int i=0; i<4; i++) {
      for(int j=0; j<13; j++) {
        cards[i*13+j]=new Card(j+1, i+1); //create the card
        deck.add(cards[i*13+j]); //add that card object to the deck
      }
    }
    In that manner, your card1 is the same as cards[0], and so on. As for randomizing it, perform a bunch of random swaps
    Code:
    for(int i=0; i<26; i++) {
      //choose 2 random elements to swap
      int swapIndex1=(int)Math.floor(Math.random()*52);
      if(swapIndex1==52) swapIndex1--;
      int swapIndex2=(int)Math.floor(Math.random()*52);
      if(swapIndex2==52) swapIndex2--;
    
      //swap them
      Object tmp=deck.get(swapIndex1);
      deck.set(swapIndex1, deck.get(swapIndex2));
      deck.set(swapIndex2, tmp);
    }
    Kids, kids... you tried your best, and you failed miserably; the lesson is: never try.

  7. #7
    Join Date
    Jul 2003
    Location
    The City of Roses
    Posts
    2,503
    BuezaWebDev, implementing a shuffle without keeping all the Card objects in an array is going to be damn near impossible.

    Though I would implement the shuffle in this way ...
    Code:
    protected static final Random generator = new Random();
    
    protected static void shuffle(Object[] a)
    {
    	int i, j;
    	
    	for (i = 0; i < a.length; ++i) {
    		j = generator.nextInt(a.length);
    		swap(a, i, j);
    	}
    }
    
    protected static void swap(Object[] a, int m, int n)
    {
    	Object t;
    	
    	t = a[m];
    	a[m] = a[n];
    	a[n] = t;
    }
    This method of shuffling iterates a number of times proportional to the size of the array, and it guarentees that every element is swapped at least once.
    for(split(//,'))*))91:+9.*4:1A1+9,1))2*:..)))2*:31.-1)4131)1))2*:3)"'))
    {for(ord){$i+=$_&7;grep(vec($s,$i++,1)=1,1..($_>>3)-4);}}print"$s\n";

  8. #8
    Join Date
    Jul 2004
    Location
    West Coast, Canada
    Posts
    665
    Holy smokes!

    You guys are the BEST!

    "Everything in a web browser."

  9. #9
    Join Date
    Jul 2004
    Location
    West Coast, Canada
    Posts
    665
    You know what, my friend just told me that there's a ShuffleList method from the Collections java package. :|

    !!

    "Everything in a web browser."

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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



Recent Articles