In: Computer Science
Deck Class
public class Deck
{
private final String[] suits = {"Spades", "Hearts", "Clubs",
"Diamonds"};
private final String[] faces = {"Ace", "2", "3", "4", "5", "6",
"7", "8", "9", "10", "Jack", "Queen", "King"};
private Card[] cards;
private int topCard;
/**
* Construct a deck of cards and initialize the topCard to -1.
*/
public Deck()
{
}
/**
* deal() returns the next card or null if the deck is empty.
* @return next Card in the deck.
*/
public Card deal()
{
topCard++;// set topCard to the next card
if(topCard < 52)
{
return cards[topCard];
}
else
{
return null;
}
}
/**
* shuffle() randomly generates a sequence of cards for the card
array
*/
public void shuffle()
{
topCard = -1;// reset the top card
int nextNumber;
boolean[] available = new boolean[52];// used to select random #'s
between 0 and 51 without replacement
for(int i = 0; i < 52; i++)
{
available[i] = true;//all #'s between 0 and 51 are available
}
for(int i = 0; i < 52; i++)
{
nextNumber = (int)(Math.random()*52);// select a # from 0 to
51
while(!available[nextNumber])//while nextNumber is not available
(i.e. this number has already been used)
{
nextNumber = (int)(Math.random()*52);//try a different number until
you find an unused one
}
available[nextNumber] = false;// this number is taken
cards[i] = new Card(suits[nextNumber/13],
faces[nextNumber%13]);
}
}
/**
* Print out the entire deck for testing purposes.
*/
public void testDeck()
{
}
/*
Write a method called "testDeck" that uses a loop (whichever
type of loop you think is appropriate) to print out all cards
in the deck (Card array) in four NEAT columns.
When "testDeck" is called on a brand new deck each COLUMN
should
contain one suit with that suit's cards in order. For
example: the first column should contain all of the
spades in order: Ace Spades, 2 Spades, 3 Spades, ..., King
Spades.
The second column should contain all of the clubs in
order:Ace Clubs, 2 Clubs, 3 Clubs, ... and so on.
When called on a shuffled deck cards will be random but still
in NEAT columns.
WHEN YOU ARE FINISHED WRITING THIS METHOD REMOVE THIS COMMENT
*/
/**
* Print out a subset of the deck to test the deal method.
*/
public void testDeal(int numberOfCards)
{
}
/*
Write a method called "testDeal" that takes a single integer
parameter called "numberOfCards". Create a local ArrayList of Cards
(i.e. ArrayList<Card>)
called "hand".
Use an index variable, a while loop, and the deal() method
to fill the "hand" with a number of cards equal to
numberOfCards.
Next use a loop (whichever type of loop you think is appropriate)
to go through the hand
and print out all of the cards in a single NEAT column.
(Note: this method uses an ArrayList of Cards, not an array. Pay
attention
to the difference.)
WHEN YOU ARE FINISHED WRITING THIS METHOD REMOVE THIS COMMENT
*/
Card Class
public class Card
{
private String suit;
private String face;
/**
* Constructor for objects of class Card.
*/
public Card(String suit, String face)
{
this.suit = suit;
this.face = face;
}
/**
* Returns the suit of the card
*/
public String getSuit()
{
return suit;
}
/**
* Returns the face of the card
*/
public String getFace()
{
return face;
}
/**
* Returns the suit and face of the card as a single String
*/
public String getCard()
{
return face + "\t" + suit;
}
}
// Card.java
public class Card
{
private String suit;
private String face;
/**
* Constructor for objects of class Card.
*/
public Card(String suit, String face)
{
this.suit = suit;
this.face = face;
}
/**
* Returns the suit of the card
*/
public String getSuit()
{
return suit;
}
/**
* Returns the face of the card
*/
public String getFace()
{
return face;
}
/**
* Returns the suit and face of the card as a single
String
*/
public String getCard()
{
return face + "\t" + suit;
}
}
// end of Card.java
// Deck.java
import java.util.ArrayList;
public class Deck
{
private final String[] suits = {"Spades", "Hearts",
"Clubs", "Diamonds"};
private final String[] faces = {"Ace", "2", "3", "4",
"5", "6", "7", "8", "9", "10", "Jack", "Queen", "King"};
private Card[] cards;
private int topCard;
/**
* Construct a deck of cards and initialize the topCard
to -1.
*/
public Deck()
{
// create an array of 52
cards
cards = new Card[52];
topCard = 0;
// loop to populate the cards
// loop over the suits
for(int
i=0;i<suits.length;i++)
{
// loop over the
faces for each suit
for(int
j=0;j<faces.length;j++)
{
cards[topCard] = new Card(suits[i], faces[j]);
// create a new card for ith suit and jth face and set it to
topCard
topCard++;
}
}
topCard = -1; // set topCard to
-1
}
/**
* deal() returns the next card or null if the deck is
empty.
* @return next Card in the deck.
*/
public Card deal()
{
topCard++;// set topCard to the
next card
if(topCard < 52)
{
return
cards[topCard];
}
else
{
return
null;
}
}
/**
* shuffle() randomly generates a sequence of cards for
the card array
*/
public void shuffle()
{
topCard = -1;// reset the top
card
int nextNumber;
boolean[] available = new
boolean[52];// used to select random #'s between 0 and 51 without
replacement
for(int i = 0; i < 52;
i++)
{
available[i] =
true;//all #'s between 0 and 51 are available
}
for(int i = 0; i < 52;
i++)
{
nextNumber =
(int)(Math.random()*52);// select a # from 0 to 51
while(!available[nextNumber])//while nextNumber is not available
(i.e. this number has already been used)
{
nextNumber = (int)(Math.random()*52);//try a
different number until you find an unused one
}
available[nextNumber] = false;// this number is taken
cards[i] = new
Card(suits[nextNumber/13], faces[nextNumber%13]);
}
}
/**
* Print out the entire deck for testing
purposes.
*/
public void testDeck()
{
int c = 0;
// loop over the faces
for(int
i=0;i<faces.length;i++)
{
// loop over the
suit for each face
for(int
j=0;j<suits.length;j++)
{
// display the card (so that unshuffled deck
displays each COLUMN containing one suit with that suit's cards in
order
System.out.printf("%20s",cards[13*j+i].getFace()+"
"+cards[13*j+i].getSuit());
if((c+1)%4 == 0) // 4 cards have been displayed,
output a new line
System.out.println();
c++;
}
}
}
/*
Write a method called "testDeck" that uses a loop
(whichever
type of loop you think is appropriate) to print out
all cards
in the deck (Card array) in four NEAT columns.
When "testDeck" is called on a brand new deck each
COLUMN should
contain one suit with that suit's cards in order.
For
example: the first column should contain all of
the
spades in order: Ace Spades, 2 Spades, 3 Spades, ...,
King Spades.
The second column should contain all of the clubs
in
order:Ace Clubs, 2 Clubs, 3 Clubs, ... and so
on.
When called on a shuffled deck cards will be random
but still
in NEAT columns.
WHEN YOU ARE FINISHED WRITING THIS METHOD REMOVE THIS
COMMENT
*/
/**
* Print out a subset of the deck to test the deal
method.
*/
public void testDeal(int numberOfCards)
{
// create an array list of
card
ArrayList<Card> hand = new
ArrayList<Card>();
// loop to deal numberOfCards from
deck to hand
int i = 0;
// loop to deal numberOfCards until
there are cards in the deck
while(i < numberOfCards
&& topCard < 52)
{
hand.add(deal());
i++;
}
// loop to display numberOfCards
dealt
for(i=0;i<hand.size();i++)
{
// display the card
System.out.printf("%20s\n",hand.get(i).getCard());
}
System.out.println();
}
/*
Write a method called "testDeal" that takes a single
integer
parameter called "numberOfCards". Create a local
ArrayList of Cards (i.e. ArrayList<Card>)
called "hand".
Use an index variable, a while loop, and the deal()
method
to fill the "hand" with a number of cards equal to
numberOfCards.
Next use a loop (whichever type of loop you think is
appropriate) to go through the hand
and print out all of the cards in a single NEAT
column.
(Note: this method uses an ArrayList of Cards, not an
array. Pay attention
to the difference.)
WHEN YOU ARE FINISHED WRITING THIS METHOD REMOVE THIS
COMMENT
*/
}
//end of Deck.java
// DeckDriver.java
public class DeckDriver {
public static void main(String[] args) {
// Create new Deck object
Deck deck1 = new Deck();
// Run testDeck( )
System.out.println("Test
Deck(Before Shuffle):");
deck1.testDeck();
// Run shuffle( ) then run
testDeck( ) again
deck1.shuffle();
System.out.println("\nTest
Deck(After Shuffle):");
deck1.testDeck();
// Create another Deck object
Deck deck2 = new Deck();
// Run testDeal( ) with a parameter
of 5
System.out.println("\n\nTest
Deal(Before Shuffle):");
deck2.testDeal(5);
// Now run shuffle( ) then run
testDeal( ) again with a parameter of 10
deck2.shuffle();
System.out.println("\nTest
Deal(After Shuffle):");
deck2.testDeal(10);
}
}
//end of DeckDriver.java
Output: