how to shuffle a deck of cards using a basic switch method in java

794 views Asked by At

I am creating the basis for a card game right now, I am doing just some basic functions for cards. I want to shuffle the cards, but don't know how to write the function for it. It will be called in the main program here is the deck class I have so far:

    import java.awt.*;
    import javax.swing.*;
    import java.awt.event.*  ;      //capture mouse events
    import java.awt.Graphics ;
    import java.awt.Event;
    import java.util.Random;

    public class Deck 
    {
        private  Random random = new Random (); 
        protected Card cards[] = new Card[52] ; 

        protected int numcards = 0; 

        public Deck()
        {
            int i = 0; 
            for (int s = 1; s<5 ; s++)
            {
                for (int n=1; n<14 ; n++)
                {
                    cards[i] = new Card(n,s);
                    i++; 
                    numcards =  numcards + 1; 
                }
            }

        }

        public void giveCard(Deck p) 
        {
             numcards =  numcards - 1; 
             p.takeCard(cards[numcards]); 
             return;  
        }

        public void takeCard(Card c)
        {
            cards[numcards] = c;
            numcards = numcards + 1;

        }

        public void shuffle ()
        {
             int temp;  
             for (int i = 0; i<52; i++)
             {
                 //cards[i] = temp ; 
             }
        }

        public void draw (Container c ,Graphics g, int x , int y  )
        {
            for (int i = 0; i<numcards; i++)
            {
                 cards[i].draw(c, g, x+i*20,y) ; 
            }

        }

    }
3

There are 3 answers

6
twentylemon On

If you change your Card[] into List<Card> instead, you can simply use Collections.shuffle(cards). Otherwise, loop through the array, at each point, swap the current card with a randomly selected card.

0
Ryan R On

What I like to do is have a card array as the deck, randomly select two indices, and swap the values. You will need a third "dummy" variable to hold one of the card values during the swap. You can have a loop do this some random large number of times (500-1000) for a nice "realistic" shuffle.

Fun fact: this is actually the type of algorithm official computerized blackjack and poker websites use for their shuffling.

0
Shar1er80 On

I'm use to shuffling the top half of the deck into the bottom half of the deck, which simulates a bridge/riffle shuffle. This saves on performance since you don't have to really iterate through the entire deck for a shuffle.

Random r = new Random();
int halfMark = cards.length / 2;
// Iterate through the top half of the deck
for (int i = 0; i < halfMark; i++) {
    // Generate a random index in the bottom half
    int shuffleIndex = r.nextInt(halfMark) + halfMark;

    // Swap the cards
    int temp = cards[i];
    cards[i] = cards[shuffleIndex];
    cards[shuffleIndex] = temp;
}

If you're wanting to do more than one shuffle, just wrap the for loop in another for loop controlling how many times you want to shuffle the top half of the deck into the bottom half.