5 Replies - 1878 Views - Last Post: 30 October 2011 - 05:22 PM Rate Topic: -----

#1 dubp65  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 17
  • Joined: 10-October 11

Shuffling an array that uses enumerated types from other classes

Posted 29 October 2011 - 03:21 PM

Hi guys,

I am trying (and struggling) to figure out how to shuffle an array that represents a deck of cards. Basically this array of 52 numbers generates cards based on their properties such as rank and suit which are referenced from enumerated types in other classes (at least from what I understand):

The driver class:
package carddeck;

//From Java Tutorial

public class DisplayDeck
{
    private static Deck deck = new Deck();
    public static void main(String[] args)
    {
        printDeck();
        deck.shuffler();
        System.out.println("Now printing shuffled deck ...");
        printDeck();
    }
    public static void printDeck()
    {
        for (int i = 0;i < deck.getCards().length;i++)
            System.out.println(deck.getCards()[i].toString());
    }
}


Deck class, which has the shuffler and as you can see I have tried to use the collections class to shuffle the cards but it is not doing anything
package carddeck;



import java.util.Collections;
import java.util.ArrayList;
public class Deck
{
    private static Card[] cards = new Card[52];
    private int length;
    public Deck()
    {
        int i = 0;
        for (Suit suit : Suit.values())
        {
            for (FaceValue face : FaceValue.values())
            {
                cards[i] = new Card(face, suit);
                i = i + 1;
            }
        }
    }
    
     public Card[] getCards()
     {
         return cards;
     }
public static void main(String[] args)
    {
    
     int j;
     Deck deck = new Deck();
     for (int i = 0;i < cards.length;i++)
        {
            
            j =((int)(Math.random() * 52));
            System.out.println(cards[j]);
            
        }
    }
    
 public void shuffler(){
     
    ArrayList arrayList = new ArrayList();
    arrayList.add(Deck.cards);
    System.out.println(arrayList);
}
}


The facevalue class
package carddeck;

public enum FaceValue {Ace, Two, Three, Four, Five,
    Six, Seven, Eight, Nine,Ten, Jack, Queen, King}




...and the suit class
package carddeck;

public enum Suit {Clubs, Diamonds, Hearts, Spades}



and the card class

package carddeck;

public class Card
{
    private FaceValue face;
    private Suit suit;

    public Card(FaceValue cardFace, Suit cardSuit)
    {
        face = cardFace;
        suit = cardSuit;
    }

    public Suit getSuit()
    {
        return suit;
    }

    public FaceValue getFaceValue()
    {
        return face;
    }

    @Override
    public String toString()
    {
        return face + " of " + suit;
    }
}


I feel like I've tried everything within my knowledge and research ability. Hopefully one of you can please help me out!

Is This A Good Question/Topic? 0
  • +

Replies To: Shuffling an array that uses enumerated types from other classes

#2 baavgai  Icon User is offline

  • Dreaming Coder
  • member icon

Reputation: 5932
  • View blog
  • Posts: 12,857
  • Joined: 16-October 07

Re: Shuffling an array that uses enumerated types from other classes

Posted 29 October 2011 - 03:49 PM

public class Deck {
	// static bad!
	// don't do this
	// private static Card[] cards = new Card[52];
	
	private Card[] cards;
	private int length; // good, but you don't use it
	
	public Deck() {
		cards = new Card[52];
		length = 0;
		// int i = 0;
		for (Suit suit : Suit.values()) {
			for (FaceValue face : FaceValue.values()) {
				cards[length++] = new Card(face, suit);
			}
		}
	}
	
	// do you really need this?
	// your class shouldn't just spit out an array
	// public Card[] getCards()     {         return cards;     }
	public Card getCard(int i) { return (i>=0 && i<length) ? cards[i] : null; }
	public int getLength() { return length; }
	
	public void shuffle() {
		// why?
		// ArrayList arrayList = new ArrayList();
		// arrayList.add(Deck.cards);
		// System.out.println(arrayList);
		
		// code should either
		// Method I
		// 1. pick two random cards
		// 2. swap them
		// 3. repeat for a while
		// or
		// Method II
		// 1. loop through all cards
		// 2. pick a random card from the ones you haven't swapped
		// 2. swap the current with the random pick
		// 3. repeat until all cards are iterated over
		
		// Method I is conceptually easier to understand
		// Method II consistently results in a well sorted list
		// pick one
	}
	
	
	// you already basically have this code in two places
	public void print() {
		for (int i=0; i<length; i++) {
			System.out.println(cards[i]);
		}
	}
}


public class DisplayDeck {
	// why static, why global?!?
	// private static Deck deck = new Deck();
	public static void main(String[] args) {
		Deck deck = new Deck();
		// printDeck(deck);
		deck.print();
		deck.shuffle();
		System.out.println("Now printing shuffled deck ...");
		deck.print();
	}
	
	// if you were doing this from the outside
	// or, we could just make this a Deck method
	public static void printDeck(Deck deck) {
		for (int i=0;i < deck.getLength(); i++) {
			System.out.println(deck.getCard(i));
		}
	}
}



The problem is too much static. Both too much and not enough OO.

Hope this helps.

This post has been edited by baavgai: 29 October 2011 - 03:50 PM

Was This Post Helpful? 2
  • +
  • -

#3 pbl  Icon User is offline

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

Reputation: 8347
  • View blog
  • Posts: 31,910
  • Joined: 06-March 08

Re: Shuffling an array that uses enumerated types from other classes

Posted 29 October 2011 - 03:53 PM

Why shuffling ? :)
The easiest way is to create an ArrayList of Cards and remove randomly from it

http://www.dreaminco...snippet3176.htm

but you can always add a shuffle() method in your Deck class

void suffle() {
   Random ran = new Random();
   for(int i = 0; i < cards.length; ++i) {        // shuffle 52 times
       int from = ran.nextInt(cards.length);      // first card to be swap
       int to = ran.nextInt(cards.length);        // with that card
       Card c = cards[from];                      // save the card
       cards[from] = cards[to];                   // replace it by the other one
       cards[to] = c;                             // restore saved one at the other place
   }
}


Was This Post Helpful? 2
  • +
  • -

#4 g00se  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2831
  • View blog
  • Posts: 11,996
  • Joined: 20-September 08

Re: Shuffling an array that uses enumerated types from other classes

Posted 30 October 2011 - 09:39 AM

Quote

I am trying (and struggling) to figure out how to shuffle an array that represents a deck of cards.


The reason you're struggling is that your underlying container class in Deck is an array. Make it a List<Card> and the problem is solved, as well as making other operations a lot easier.
Was This Post Helpful? 1
  • +
  • -

#5 dubp65  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 17
  • Joined: 10-October 11

Re: Shuffling an array that uses enumerated types from other classes

Posted 30 October 2011 - 03:52 PM

View Postpbl, on 29 October 2011 - 03:53 PM, said:

Why shuffling ? :)
The easiest way is to create an ArrayList of Cards and remove randomly from it

http://www.dreaminco...snippet3176.htm

but you can always add a shuffle() method in your Deck class

void suffle() {
   Random ran = new Random();
   for(int i = 0; i < cards.length; ++i) {        // shuffle 52 times
       int from = ran.nextInt(cards.length);      // first card to be swap
       int to = ran.nextInt(cards.length);        // with that card
       Card c = cards[from];                      // save the card
       cards[from] = cards[to];                   // replace it by the other one
       cards[to] = c;                             // restore saved one at the other place
   }
}


I cannot express how thankful I am for the shuffle method. I tried your arrayList snippet advice and just couldn't get it to work for some reason. The shuffle method works flawlessly though.

Thanks everyone.
Was This Post Helpful? 0
  • +
  • -

#6 baavgai  Icon User is offline

  • Dreaming Coder
  • member icon

Reputation: 5932
  • View blog
  • Posts: 12,857
  • Joined: 16-October 07

Re: Shuffling an array that uses enumerated types from other classes

Posted 30 October 2011 - 05:22 PM

Hmm... pbl just gives up method one. For completeness, this would be method two:
// nice function to have regardless of how you shuffle
void swap(int i, int j) {
	if (i!=j) {             // if it isn't pointless, do it
		Card c = cards[i];   // save the card
		cards[i] = cards[j]; // replace it by the other one
		cards[j] = c;        // restore saved one at the other place
	}
}

void shuffle() {
	Random ran = new Random();
	for(int i = cards.length-1; i>0; i--) { // start at the tail, last card
		swap(i, ran.nextInt(i+1));           // choose from the cards left
	}
}


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1