import java.util.*;
/**
* CS151 Fall 2007 Project 5 Solution.
* Deck of playing cards used in BlackJackGame class.
*
* @author Scott Russell
* @version 10/25/2007 last modified 11/15/2007
*/
public class BlackJackDeck
{
// instance variables - replace the example below with your own
private ArrayList cards;
private Random randNumGen;
/**
* Constructor for standard 52 card deck with card values set to point values in
* BlackJackHand class.
*/
public BlackJackDeck()
{
cards = new ArrayList(52);
randNumGen = new Random();
// numerical version of suit and rank codes to simplify card creation
int suitNum = 0;
int rankNum = 0;
String theRank = new String();
String theSuit = new String();
// value to be assigned to each card created
int value = 0;
// loop over the 13 possible card ranks
for (int rank = 1; rank <= PlayingCard.NUM_CARDS_IN_SUIT; rank++)
{
//determine the rank
switch (rank)
{
case 1:
{
theRank = PlayingCard.ACE;
value = BlackJackHand.ACE_HIGH_VALUE;
break;
}
case 2:
{
theRank = PlayingCard.TWO;
value = BlackJackHand.TWO_VALUE;
break;
}
case 3:
{
theRank = PlayingCard.THREE;
value = BlackJackHand.THREE_VALUE;
break;
}
case 4:
{
theRank = PlayingCard.FOUR;
value = BlackJackHand.FOUR_VALUE;
break;
}
case 5:
{
theRank = PlayingCard.FIVE;
value = BlackJackHand.FIVE_VALUE;
break;
}
case 6:
{
theRank = PlayingCard.SIX;
value = BlackJackHand.SIX_VALUE;
break;
}
case 7:
{
theRank = PlayingCard.SEVEN;
value = BlackJackHand.SEVEN_VALUE;
break;
}
case 8:
{
theRank = PlayingCard.EIGHT;
value = BlackJackHand.EIGHT_VALUE;
break;
}
case 9:
{
theRank = PlayingCard.NINE;
value = BlackJackHand.NINE_VALUE;
break;
}
case 10:
{
theRank = PlayingCard.TEN;
value = BlackJackHand.TEN_VALUE;
break;
}
case 11:
{
theRank = PlayingCard.JACK;
value = BlackJackHand.JACK_VALUE;
break;
}
case 12:
{
theRank = PlayingCard.QUEEN;
value = BlackJackHand.QUEEN_VALUE;
break;
}
case 13:
{
theRank = PlayingCard.KING;
value = BlackJackHand.KING_VALUE;
break;
}
default:
{
theRank = "0";
value = 0;
break;
}
}
// loop over the 4 possible suits
for (int suit = 1; suit <= PlayingCard.NUM_SUITS; suit++)
{
switch (suit)
{
case 1: theSuit = PlayingCard.CLUB; break;
case 2: theSuit = PlayingCard.DIAMOND; break;
case 3: theSuit = PlayingCard.HEART; break;
case 4: theSuit = PlayingCard.SPADE; break;
default: theSuit = "0"; break;
}
//Create a card for each rank and suit combination, assing it its
// BlackJack value and add it to the deck
PlayingCard card = new PlayingCard(theRank+theSuit);
card.setValue(value);
cards.add(card);
}
}
}
/**
* In practice it is safer to clone the cards ArrayList instead of just returning
* a reference to it.
*
* @return the ArrayList of PlayingCards remaining in the deck.
*
*/
public ArrayList getCards()
{
return cards;
}
/**
* Merge the contents of the two decks into a single deck.
* @param deck the deck to merge with this deck
*/
public void merge(BlackJackDeck extraDeck)
{
// get the cards from extraDeck and add them to this deck's cards
cards.addAll(extraDeck.getCards());
}
/**
* Selects a random card from those remaining in the deck.
* @return randomly selected playing card
*/
public PlayingCard drawRandom()
{
// if the deck has no cards in it return null
if (cards == null || cards.size() == 0)
return null;
// otherwise pick a random number between 0 and number of dominoes in set
int index = randNumGen.nextInt(cards.size());
return cards.remove(index);
}
public int numberOfCards()
{
if (cards != null)
return cards.size();
else
return -1;
}
}