3 Replies - 1884 Views - Last Post: 19 April 2011 - 10:05 PM Rate Topic: -----

#1 cferrel  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 16-April 11

How do i do a riffle shuffle in a deck of cards in a java array?

Posted 19 April 2011 - 08:50 AM

I was trying to go about it several ways but for the past three days failed to do so. The shuffling order is the deck is spit in half and top 1 and bottom 1 then top 2 bottom 2 to 2(N+1).
public void shuffle() {
    int index = 0;

    for (int suit = 0; suit <= 1; suit++) {
        for (int value = 1; value <= 13; value++) {
            cards[index] = new Card(value, suit);
            index++;
        }
    }
    Card[] botHalf= new Card[(cards.length+26)/2];

    int index2 = 0;

    for (int suit = 2; suit <= 3; suit++) {
        for (int value = 1; value <= 13; value++) {
            cards[index] = new Card(value, suit);
            index2++;

        }
        for (int row = 0; row < cards.length; row++){

            row++;

            Card [] temp = new Card[( topHalf.length)+botHalf.length];
        //cards[row]= cards[index];
    }


This post has been edited by macosxnerd101: 19 April 2011 - 09:27 PM
Reason for edit:: Please use code tags


Is This A Good Question/Topic? 0
  • +

Replies To: How do i do a riffle shuffle in a deck of cards in a java array?

#2 CasiOo  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1394
  • View blog
  • Posts: 3,083
  • Joined: 05-April 11

Re: How do i do a riffle shuffle in a deck of cards in a java array?

Posted 19 April 2011 - 09:17 AM

How I see a riffle shuffle is
1. split the deck in half (halfA and halfB).
2. put the bottom card from halfA in another array.
3. put the bottom card from halfB in another array.
4. repeat steps 2+3 until no more cards.

so something like:
public Card[] shuffle( Card[] cards ) {
	ArrayList<Card> halfA = new ArrayList<Card>();
	ArrayList<Card> halfB = new ArrayList<Card>();
	Card[] shuffledCards = new Card[cards.length];
	
	//Splitting the deck
	int i = 0;
	for ( Card card : cards ) {
		if ( i < (cards.length/2) )
			halfA.add( card );
		else
			halfB.add( card );
		i++;
	}
	
	//Placing into the new array
	for ( int k=halfA.length; k>=0; k-- ) {
		shuffledCards[k*2] = halfA.get( k );
		shuffledCards[k*2-1] = halfB.get( k );
	}
	
	return shuffledCards;
}



Also you can also split the deck like this:
	int i = 0;
	for ( Card card : cards ) {
		if ( i % 2 == 0 )
			halfA.add( card );
		else
			halfB.add( card );
		i++;
	}



(I have not tested any of this!)

Errrrr I already see a fault when k is 0 and shuffledCards[k*2-1] gets to be -1.

This post has been edited by CasiOo: 19 April 2011 - 09:19 AM

Was This Post Helpful? 0
  • +
  • -

#3 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8329
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: How do i do a riffle shuffle in a deck of cards in a java array?

Posted 19 April 2011 - 08:49 PM

We are using computer they are not human
Unless it is a homework assignment, stupid assignment, just shuffle no need to split in two and then in two and then in two...

Randon ran = new Random();
Cards[] card = new Cards[52];
... init card array here
for(int i = 0; i < 1000; ++i) {         // 1000 or whatever
   int from = ran.nextInt(card.length);
   int to = ran.nextInt(card.length);
   Cards temp = card[from];
   card[from] = card[to];
   card[to] = temp;
}


Was This Post Helpful? 0
  • +
  • -

#4 cferrel  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 16-April 11

Re: How do i do a riffle shuffle in a deck of cards in a java array?

Posted 19 April 2011 - 10:05 PM

I agree its stupid. I spent all week trying to figure this crap out and I wished the school focused more on fundamentals of good programming instead of stupid stuff. I Wrote a random algorithm that worked well but they want it in a specific order which is annoying.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1