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!

New Topic/Question
Reply



MultiQuote






|