Reputation: 141
I'm trying to test out my two functions in my Pack class to simulate a deck of cards.
public class Pack {
private PlayingCard[] deck; // An array of 52 cards, representing the deck.
private int cardsUsed; // How many cards have been dealt from the deck.
/**
* Creating an unshuffled deck of cards
*/
public Pack() {
deck = new PlayingCard[51]; //Creates an array of 52 playing cards
int cardCt = 0; // How many cards have been created so far.
for ( int suit = 0; suit <= 3; suit++ ) { //If a suit is complete, move to the next suit
for ( int value = 1; value <= 14; value++ ) { //Builds a complete suit
deck[51] = new PlayingCard(value,suit);
cardCt++; //Adds one to the card count
}
}
cardCt = 0;
}
/**
* Shuffling a deck of cards
*/
public void shuffle() {
// Put all the used cards back into the deck, and shuffle it into
// a random order.
for ( int i = 51; i > 0; i-- ) {
int rand = (int)(Math.random()*(i+1));
PlayingCard temp = deck[i];
deck[i] = deck[rand];
deck[rand] = temp;
}
cardsUsed = 0;
}
public @Override String toString()
{
return Pack();
}
} // end class Pack
I want it to look similar to how I've done my PlayingCard class, to keep it more formal.
public class PlayingCard {
/**
* Declaring Variables
*/
private int rank;
private int suit;
/**
* Declaring Non-Numerical Cards
*/
public static final int jack = 11;
public static final int queen = 12;
public static final int king = 13;
public static final int ace = 14;
/**
* Declaring Suits
*/
public static final int clubs = 0;
public static final int diamonds = 1;
public static final int hearts = 2;
public static final int spades = 3;
/**
* Constructs a card with specified initial rank and suit
*/
public PlayingCard(int rank, int suit)
{
this.rank=rank;
this.suit=suit;
}
public int getSuit()
{
return suit;
}
public int getRank()
{
return rank;
}
/**
* Switch Statements are use because there are many execution paths
*/
public String getSuitAsString()
{
switch(suit)
{
case clubs: return "Clubs";
case diamonds: return "Diamonds";
case hearts: return "Hearts";
case spades: return "Spades";
default: return "ERR";
}
}
/**
* Switch Statements are use because there are many execution paths
*/
public String getRankAsString()
{
switch(rank)
{
case 2: return "2";
case 3: return "3";
case 4: return "4";
case 5: return "5";
case 6: return "6";
case 7: return "7";
case 8: return "8";
case 9: return "9";
case 10: return "10";
case 11: return "Jack";
case 12: return "Queen";
case 13: return "King";
case 14: return "Ace";
default: return "ERR";
}
}
/**
* toString Method
*/
public @Override String toString()
{
return getRankAsString() + " of " + getSuitAsString();
}
}
What I want to do is put the shuffle method into a toString method, like the PlayingCard class so I can then test it in my PackTester. I understand I can't return a void statement, and that's where I'm stuck. If someone could explain what to do, I'd really appreciate it.
Upvotes: 0
Views: 1900
Reputation: 35
This is the best practice shuffling method around http://datagenetics.com/blog/november42014/index.html
Here's a very rough snippit from my program
ArrayList cardsArray = new ArrayList();
cardsArray.Add(0);
//generate deck
for (int i = 1; i <= 52; i++)
{
cardsArray.Add(i);
}
// shuffle
object t = 0;
int s = 0;
Random r = new Random();
for(int i=52; i>=1; i=i-1){
s= (r.Next(i)) +1;
//swap
t = cardsArray[s];
cardsArray[s] = cardsArray[i];
cardsArray[i] = t;
}
cardsArray.RemoveAt(0);
Upvotes: 1
Reputation: 10121
Can you guess what I'm doing here? :)
public class Pack {
// .. other code ..
public @Override String toString() {
// see note below
// shuffle();
StringBuilder sb = new StringBuilder();
sb.append("The cards are: ");
for (int i = 0; i < deck.length; i++) {
if (i > 0) sb.append(", ");
sb.append(deck.toString());
}
sb.append(".");
return sb.toString();
}
}
Note: If you want, you can call the shuffle
method here to shuffle up the cards but this is bad design, as every time you want to print the deck, you end up shuffling it. The toString
method should only tell you about the current state, not change the state.
Ideally you should do as following:
Pack myPack = new Pack();
myPack.shuffle();
System.out.println(myPack); // this automatically calls toString()
Take note that void
return type means there's no object returned - the method just does its thing when called.
Upvotes: 1