# Card Game Problem

Page 1 of 1

## 5 Replies - 2144 Views - Last Post: 26 April 2012 - 01:31 PMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=276600&amp;s=7a599a6df053267d84090836c10afa9f&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 blackvelvet

Reputation: 1
• Posts: 20
• Joined: 17-March 12

# Card Game Problem

Posted 24 April 2012 - 09:56 AM

So, I'm trying to create a simple card game called "High, Low". The way it works is...The player is dealt a card. They then guess whether the next card will be higher or lower than the previous card. If they guess correct they win, if they don't, they loose.

I'm suppose to create this card game with code that has already been provided for me, such as Card, Deck, Hand, Rank, and Suit.

Here are the codes...

```
// Card.java - John K. Estell - 8 May 2003
// Implementation of a playing card.  Uses classes Rank and Suit for
// expressing the card value.

import javax.swing.*;

/**
* Representation of a single playing card. A card consists of a suit value
* (e.g. hearts, spades), a rank value (e.g. ace, 7, king), and an image of
* the front of the card.  A card object is immutable; once instantiated, the
* values cannot change.
*
* @author John K. Estell
* @version 1.0
*/
public class Card implements Comparable {

// instance variables for the card
private Suit suitValue;
private Rank rankValue;
private ImageIcon cardImage;
private static boolean sortRankMajorOrder = true;

/**
* Creates a new playing card.
* @param suit the suit value of this card.
* @param rank the rank value of this card.
* @param cardFace the face image of this card.
*/
public Card( Suit suit, Rank rank, ImageIcon cardFace ) {
cardImage = cardFace;
suitValue = suit;
rankValue = rank;
}

/**
* Generates the filename associated with the card.  <code>getFilename</code> assumes that all of the standard card images
* are stored in individual files using filenames in the form of:
* <b>RS.gif</b> where <b>R</b> is a single character used to represent
* the rank value of the card and <b>S</b> is a single character used to represent
* the suit value of the card.
* <p>The characters used for <b>R</b> are:
* 'a' (ace), '2', '3', '4', '5', '6', '7', '8', '9',
* 't' (10), 'j' (jack), 'q' (queen), and 'k' (king).
* <p>The characters used for <b>S</b> are:
* 'c' (clubs), 'd' (diamonds), 'h' (hearts), and 's' (spades).
* <p>Two other cards are also available: "b.gif" (back of card) and "j.gif" (joker).
*
* @param suit the suit value of the card.
* @param rank the rank value of the card.
* @return a string containing the filename of the card.
*/
public static String getFilename( Suit suit, Rank rank ) {
return rank.getSymbol() + suit.getSymbol() + ".gif";
}

/**
* Returns the suit of the card.
* @return a Suit constant representing the suit value of the card.
*/
public Suit getSuit() {
return suitValue;
}

/**
* Returns the rank of the card.
* @return a Rank constant representing the rank value of the card.
*/
public Rank getRank() {
return rankValue;
}

/**
* Returns the graphic image of the card.
* @return an icon containing the graphic image of the card.
*/
public ImageIcon getCardImage() {
return cardImage;
}

/**
* Returns a description of this card.
* @return the name of the card.
*/
public String toString() {
return rankValue.toString() + " of " + suitValue.toString();
}

/**
* Returns a description of the rank of this card.
* @return the rank value of the card as a string.
*/
public String rankToString() {
return rankValue.toString();
}

/**
* Returns a description of the suit of this card.
* @return the suit value of the card as a string.
*/
public String suitToString() {
return suitValue.toString();
}

/**
* Specifies that cards are to be sorted in rank-major order.  Cards are ordered
* first by their rank value; cards of the same rank are then ordered by their
* suit value.
*/
public static void setRankMajorSort() {
sortRankMajorOrder = true;
}

/**
* Specifies that cards are to be sorted in suit-major order.  Cards are ordered
* first by their suit value; cards of the same suit are then ordered by their
* rank value.
*/
public static void setSuitMajorSort() {
sortRankMajorOrder = false;
}

/**
* Compares two cards for the purposes of sorting.
* Cards are ordered first by their suit value, then by their
* rank value.
* @param otherCardObject the other card
* @return a negative integer, zero, or a positive integer is this card is
* less than, equal to, or greater than the referenced card.
*/
public int compareTo( Object otherCardObject ) {
Card otherCard = (Card) otherCardObject;
int suitDiff = suitValue.compareTo( otherCard.suitValue );
int rankDiff = rankValue.compareTo( otherCard.rankValue );

if ( sortRankMajorOrder ) {
if ( rankDiff != 0 )
return rankDiff;
else
return suitDiff;
}
else {
if ( suitDiff != 0 )
return suitDiff;
else
return rankDiff;
}
}

/**
* Compares two cards to determine if they have the same value.
* This is not the same as the use of <code>equals</code> which compares
* two objects for equality.
* @param card the other card
* @return <code>true</code> if the two cards have the same rank and suit
* values, <code>false</code> if they do not.
*/
public boolean isSameAs( Card card ) {
if ( ( rankValue != card.rankValue ) || ( suitValue != card.suitValue ) )
return false;
else
return true;
}

}

```

```
// Deck.java - John K. Estell - 8 May 2003
// Implementation of a deck of playing cards.  Uses the Card class.

import java.util.*;

/**
* Represents a deck of playing cards.  In order to have maximum flexibility,
* this class does not implement a standard deck of playing cards; it only
* provides the functionality of a deck of cards.  The client programmer must
* instantiate a Deck object, then populate it with the set of playing cards
* appropriate for the card game being implemented.  This allows for proper
* implementation of card games such as pinochle (a 48-card deck containing
* only aces, nines, tens, jacks, queens, and kings in all four suits, with
* each card present twice in the deck) or casino-style blackjack (where six
* standard decks are used for a game).
* @author John K. Estell
* @version 1.0
*/
public class Deck {
private java.util.List deck;
private int index;

/**
* Creates an empty deck of cards.
*/
public Deck() {
deck = new ArrayList();
index = 0;
}

/**
* Adds a card to the deck.
* @param card card to be added to the deck.
*/
public void addCard( Card card ) {
}

/**
* The size of a deck of cards.
* @return the number of cards present in the full deck.
*/
public int getSizeOfDeck() {
return deck.size();
}

/**
* The number of cards left in the deck.
* @return the number of cards left to be dealt from the deck.
*/
public int getNumberOfCardsRemaining() {
return deck.size() - index;
}

/**
* Deal one card from the deck.
* @return a card from the deck, or the null reference if there
* are no cards left in the deck.
*/
public Card dealCard() {
if ( index >= deck.size() )
return null;
else
return (Card) deck.get( index++ );
}

/**
* Shuffles the cards present in the deck.
*/
public void shuffle() {
Collections.shuffle( deck );
}

/**
* Looks for an empty deck.
* @return <code>true</code> if there are no cards left to be dealt from the deck.
*/
public boolean isEmpty() {
if ( index >= deck.size() )
return true;
else
return false;
}

/**
* Restores the deck to "full deck" status.
*/
public void restoreDeck() {
index = 0;
}

}

```

```
// Hand.java - John K. Estell - 8 May 2003
// Implementation of a abstract hand of playing cards.
// Uses the Card class.  Requires subclass for specifying
// the specifics of what constitutes the evaluation of a hand
// for the game being implemented.

import java.util.*;

/**
* Represents the basic functionality of a hand of cards.
* Extensions of this class will provide the
* definition of what constitutes a hand for that game and how hands are compared
* to one another by overriding the <code>compareTo</code> method.
* @author John K. Estell
* @version 1.0
*/
public abstract class Hand implements Comparable {

private java.util.List hand = new ArrayList();

/**
* Adds a card to this hand.
* @param card card to be added to the current hand.
*/
public void addCard( Card card ) {
}

/**
* Obtains the card stored at the specified location in the hand.  Does not
* remove the card from the hand.
* @param index position of card to be accessed.
* @return the card of interest, or the null reference if the index is out of
* bounds.
*/
public Card getCard( int index ) {
return (Card) hand.get( index );
}

/**
* Removes the specified card from the current hand.
* @param card the card to be removed.
* @return the card removed from the hand, or null if the card
* was not present in the hand.
*/
public Card removeCard( Card card ) {
int index = hand.indexOf( card );
if ( index < 0 )
return null;
else
return (Card) hand.remove( index );
}

/**
* Removes the card at the specified index from the hand.
* @param index poisition of the card to be removed.
* @return the card removed from the hand, or the null reference if
* the index is out of bounds.
*/
public Card removeCard( int index ) {
return (Card) hand.remove( index );
}

/**
* Removes all the cards from the hand, leaving an empty hand.
*/
hand.clear();
}

/**
* The number of cards held in the hand.
* @return number of cards currently held in the hand.
*/
public int getNumberOfCards() {
return hand.size();
}

/**
* Sorts the card in the hand.
* Sort is performed according to the order specified in the {@link Card} class.
*/
public void sort() {
Collections.sort( hand );
}

/**
* Checks to see if the hand is empty.
* @return <code>true</code> is the hand is empty.
*/
public boolean isEmpty() {
return hand.isEmpty();
}

/**
* Determines whether or not the hand contains the specified card.
* @param card the card being searched for in the hand.
* @return <code>true</code> if the card is present in the hand.
*/
public boolean containsCard( Card card ) {
return false;
}

/**
* Searches for the first instance of the specified card in the hand.
* @param card card being searched for.
* @return position index of card if found, or <code>-1</code> if not found.
*/
public int findCard( Card card ) {
return hand.indexOf( card );
}

/**
*  Compares two hands.
*  @param otherHandObject the hand being compared.
*  @return < 0 if this hand is less than the other hand, 0 if the two hands are
*  the same, or > 0 if this hand is greater then the other hand.
*/
public int compareTo( Object otherHandObject ) {
Hand otherHand = (Hand) otherHandObject;
return evaluateHand() - otherHand.evaluateHand();
}

/**
*  Evaluates the hand.  Must be defined in the subclass that implements the hand
*  for the game being written by the client programmer.
*  @return an integer corresponding to the rating of the hand.
*/
public abstract int evaluateHand();

/**
* Returns a description of the hand.
* @return a list of cards held in the hand.
*/
public String toString() {
return hand.toString();
}

/**
* Replaces the specified card with another card.  Only the first
* instance of the targeted card is replaced.  No action occurs if
* the targeted card is not present in the hand.
* @return <code>true</code> if the replacement occurs.
*/
public boolean replaceCard( Card oldCard, Card replacementCard ) {
int location = findCard( oldCard );
if ( location < 0 )
return false;
hand.set( location, replacementCard );
return true;
}

}

```

```
// Rank.java - John K. Estell - 8 May 2003
// Implementation of the "rank" value for a playing card.

import java.util.*;

/**
*  Specification of the rank values for a standard deck of cards.
*  Client has ability to set either the ace or the king to be the
*  highest ranking card; default is king high.  Ranks are
*  established in the following ascending order:
*  <p>King high: ace, 2, 3, 4, 5, 6, 7, 8, 9, 10, jack, queen, king.
*  <p>Ace high: 2, 3, 4, 5, 6, 7, 8, 9, 10, jack, queen, king, ace.
*  <p>Class can be extended for implementation of speciality decks
*  containing a subset of the standard ranks, e.g. pinochle.
*/
public class Rank implements Comparable {
private String name;
private String symbol;
private static boolean aceHigh = false;

/**
* The rank ace.
*/
public final static Rank ACE = new Rank( "Ace", "a" );
/**
* The rank two.
*/
public final static Rank TWO = new Rank( "Two", "2" );
/**
* The rank three.
*/
public final static Rank THREE = new Rank( "Three", "3" );
/**
* The rank four.
*/
public final static Rank FOUR = new Rank( "Four", "4" );
/**
* The rank five.
*/
public final static Rank FIVE = new Rank( "Five", "5" );
/**
* The rank six.
*/
public final static Rank SIX = new Rank( "Six", "6" );
/**
* The rank seven.
*/
public final static Rank SEVEN = new Rank( "Seven", "7" );
/**
* The rank eight.
*/
public final static Rank EIGHT = new Rank( "Eight", "8" );
/**
* The rank nine.
*/
public final static Rank NINE = new Rank( "Nine", "9" );
/**
* The rank ten.
*/
public final static Rank TEN = new Rank( "Ten", "t" );
/**
* The rank jack.
*/
public final static Rank JACK = new Rank( "Jack", "j" );
/**
* The rank queen.
*/
public final static Rank QUEEN = new Rank( "Queen", "q" );
/**
* The rank king.
*/
public final static Rank KING = new Rank( "King", "k" );

private final static java.util.List VALUES_KING_HIGH =
Collections.unmodifiableList(
Arrays.asList( new Rank[] { ACE, TWO, THREE, FOUR, FIVE, SIX, SEVEN,
EIGHT, NINE, TEN, JACK, QUEEN, KING      } ) );

private final static java.util.List VALUES_ACE_HIGH =
Collections.unmodifiableList(
Arrays.asList( new Rank[] { TWO, THREE, FOUR, FIVE, SIX, SEVEN,
EIGHT, NINE, TEN, JACK, QUEEN, KING, ACE } ) );

/**
* List of all rank values.  Used primarily for the purpose of iteration.
*/
public final static java.util.List VALUES =
Collections.unmodifiableList( VALUES_KING_HIGH );

// Constructor - declared private as only the predefined values should
// be used by the client.
private Rank( String nameValue, String symbolValue ) {
name = nameValue;
symbol = symbolValue;
}

/**
*  Sets the king to be the card having highest rank.  The ace is
*  reduced to the lowest rank.
*/
public static void setKingHigh() {
aceHigh = false;
}

/**
*  Sets the ace to be the card having highest rank.  The two becomes
*  the lowest rank.
*/
public static void setAceHigh() {
aceHigh = true;
}

/**
*  Returns a description of this rank.
*  @return the name of this rank.
*/
public String getName() {
return name;
}

/**
*  Returns a description of this rank.
*  @return the name of this rank.
*/
public String toString() {
return name;
}

/**
*  The symbol associated with this rank.  Returns the symbol, which
*  usually constitutes a single character, in the form of a string.
*  Symbol is used for the construction of the filenames of the card images.
*  @return string containing the symbol for the rank.
*/
public String getSymbol() {
return symbol;
}

/**
*  Compares the ranks.  Result is dependent on the whether the ace
*  or the king is considered to be the high rank.
*  @param otherRankObject the other rank.
*  @return the arithmetic difference between the compared ranks
*  based on their ordering in the listing of values.  This result
*  may differ depending on whether the king or the ace is considered
*  the high card.  Result will be < 0 if this rank is lower than
*  the other rank, 0 if the ranks are the same, or > 0 if this
*  rank is higher than the other rank.
*/
public int compareTo( Object otherRankObject ) {
Rank otherRank = (Rank) otherRankObject;
if ( aceHigh )
return VALUES_ACE_HIGH.indexOf( this ) - VALUES_ACE_HIGH.indexOf( otherRank );
else
return VALUES_KING_HIGH.indexOf( this ) - VALUES_KING_HIGH.indexOf( otherRank );
}

}

```

```
// Suit.java - John K. Estell - 8 May 2003
// Implementation of the "suit" value for a playing card.

import java.util.*;

/**
* Specification of the suit values for a standard deck of cards.
*/
public final class Suit implements Comparable {
private String name;
private String symbol;

/**
* The suit clubs.
*/
public final static Suit CLUBS = new Suit( "Clubs", "c" );
/**
* The suit diamonds.
*/
public final static Suit DIAMONDS = new Suit( "Diamonds", "d" );
/**
* The suit hearts.
*/
public final static Suit HEARTS = new Suit( "Hearts", "h" );
/**
*/

/**
* List of all suit values.  Primarily for use with iteration.
*/
public final static java.util.List VALUES =
Collections.unmodifiableList(
Arrays.asList( new Suit[] { CLUBS, DIAMONDS, HEARTS, SPADES } ) );

// Constructor - declared private as only the predefined values should
// be used by the client.
private Suit( String nameValue, String symbolValue ) {
name = nameValue;
symbol = symbolValue;
}

/**
*  Returns a description of this suit.
*  @return the name of the suit.
*/
public String getName() {
return name;
}

/**
*  The symbol associated with this suit.  Returns the symbol, which
*  usually constitutes a single character, in the form of a string.
*  Symbol is used for the construction of the filenames of the card images.
*  @return string containing the symbol for the suit.
*/
public String getSymbol() {
return symbol;
}

/**
* Returns a description of this suit.
* @return the name of this suit.
*/
public String toString() {
return name;
}

/**
*  Compares the suits.  Used for the purpose of sorting cards in a hand or deck.
*  @param otherSuitObject the other suit.
*  @return < 0 if this suit is lower than the other suit, 0 if the suits
*  are the same, or > 0 if this suit is higher than the other suit.
*/
public int compareTo( Object otherSuitObject ) {
Suit otherSuit = (Suit) otherSuitObject;
return VALUES.indexOf( this ) - VALUES.indexOf( otherSuit );
}

}

```

And here is my code so far...

```
//Player is dealt a card. They then guess whether the next card
//will be higher or lower than the previous card. If they guess
//correct they win, if they don't, they loose.

import java.awt.*;
import javax.swing.*;
import java.util.*;

public class HighLowGame extends JApplet
{

private Deck cardDeck;
private final String directory = "cards/";

public void init()
{
getRootPane().putClientProperty("defeatSystemEventQueueCheck", Boolean.TRUE);
getContentPane().setLayout(null);
getContentPane().setBackground(java.awt.Color.green);
setSize(500,360);
cardDisplayLabel.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
cardDisplayLabel.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
cardDisplayLabel.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
cardDisplayLabel.setText("Draw a Card");
cardDisplayLabel.setOpaque(true);
cardDisplayLabel.setBackground(java.awt.Color.lightGray);
cardDisplayLabel.setForeground(java.awt.Color.black);
cardDisplayLabel.setBounds(48,24,132,168);
drawCardBtn.setText("Draw a Card");
drawCardBtn.setBounds(24,200,192,24);
newHighBtn.setText("Pick High");
newHighBtn.setBounds(24,230,192,24);
newLowBtn.setText("Pick Low");
newLowBtn.setBounds(24,260,192,24);
newDeckBtn.setText("Shuffled Deck/New Game");
newDeckBtn.setBounds(24,290,192,24);

cardDeck = new Deck();

Iterator suitIterator = Suit.VALUES.iterator();
while ( suitIterator.hasNext() )
{
Suit suit = (Suit) suitIterator.next();
Iterator rankIterator = Rank.VALUES.iterator();
while ( rankIterator.hasNext() ) {
Rank rank = (Rank) rankIterator.next();
String imageFile = directory + Card.getFilename( suit, rank );
ImageIcon cardImage = new ImageIcon( getImage( getCodeBase(), imageFile ) );
Card card = new Card( suit, rank, cardImage );
}
}

SymAction lSymAction = new SymAction();
}

javax.swing.JLabel cardDisplayLabel = new javax.swing.JLabel();
javax.swing.JButton drawCardBtn = new javax.swing.JButton();
javax.swing.JButton newDeckBtn = new javax.swing.JButton();
javax.swing.JButton newHighBtn = new javax.swing.JButton();
javax.swing.JButton newLowBtn = new javax.swing.JButton();

class SymAction implements java.awt.event.ActionListener
{
public void actionPerformed(java.awt.event.ActionEvent event)
{
Object object = event.getSource();
if (object == drawCardBtn)
drawCardBtn_actionPerformed(event);
else if (object == newDeckBtn)
newDeckBtn_actionPerformed(event);
}
}

void drawCardBtn_actionPerformed(java.awt.event.ActionEvent event)
{
Card card = cardDeck.dealCard();
cardDisplayLabel.setText( card.toString() );
cardDisplayLabel.setIcon( card.getCardImage() );
if ( cardDeck.getNumberOfCardsRemaining() == 0 )
drawCardBtn.setEnabled( false );
}

void newDeckBtn_actionPerformed(java.awt.event.ActionEvent event)
{
cardDeck.restoreDeck();
cardDeck.shuffle();
drawCardBtn.setEnabled( true );
cardDisplayLabel.setIcon( null );
cardDisplayLabel.setText( "Draw a Card" );
}

}

```

I'm stuck at this point. I need to add a score that shows up to the right, saying if you won or lost, depending on which button is pressed. And I need to add an if, else statement that determines if the card is higher or lower. I'm just really lost and need a little help on where to start. Thanks!

Is This A Good Question/Topic? 0

## Replies To: Card Game Problem

### #2 higgsch

Reputation: 13
• Posts: 126
• Joined: 26-October 09

## Re: Card Game Problem

Posted 24 April 2012 - 10:58 AM

As with any program, there are many ways of accomplishing this...
Personally, I wouldn't have a button to deal a card. I would have the two, high/low buttons.
When the player starts the game, have the first card already dealt, then the player would choose if the next card would be high or low (via the buttons). When the high/low button is clicked, deal the second card and test if the card is high or low, then compare to the player's choice.

P.S. Looking at the predefined classes, you need to instanciate all the cards in the deck on load (the deck is empty until you define the cards in the deck). Does that make sense to anybody besides myself? I have a habit of talking in abstract ways. If you need clarification, simply ask!

--Edit--
Similarly, if you want to keep your deal card button, you have to decide how you want the user to choose high or low.

You could use radio buttons and test which one is selected.

You could have them type in their choice (probably not the best method, but still functional).

You could even have the two buttons to make your choice, inform the user of their decision. Then have them click deal card to actually deal the card. This method could be an option if you wanted the user to be able to change his/her mind before actually taking the card.

Ultimately, it's up to you. You decide, then we can help you figure out how to do it.

This post has been edited by higgsch: 24 April 2012 - 11:04 AM

### #3 blackvelvet

Reputation: 1
• Posts: 20
• Joined: 17-March 12

## Re: Card Game Problem

Posted 24 April 2012 - 12:05 PM

Thanks for your insight. Your'e right I don't need the draw card button. This is the code without it now..

```
//Player is dealt a card. They then guess whether the next card
//will be higher or lower than the previous card. If they guess
//correct they win, if they don't, they loose.

import java.awt.*;
import javax.swing.*;
import java.util.*;

public class HighLowGame extends JApplet
{

private Deck cardDeck;
private final String directory = "cards/";

public void init()
{
getRootPane().putClientProperty("defeatSystemEventQueueCheck", Boolean.TRUE);
getContentPane().setLayout(null);
getContentPane().setBackground(java.awt.Color.green);
setSize(500,360);
cardDisplayLabel.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
cardDisplayLabel.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
cardDisplayLabel.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
cardDisplayLabel.setOpaque(true);
cardDisplayLabel.setBackground(java.awt.Color.lightGray);
cardDisplayLabel.setForeground(java.awt.Color.black);
cardDisplayLabel.setBounds(48,24,132,168);
newHighBtn.setText("Pick High");
newHighBtn.setBounds(24,200,192,24);
newLowBtn.setText("Pick Low");
newLowBtn.setBounds(24,230,192,24);
newDeckBtn.setText("Shuffled Deck/New Game");
newDeckBtn.setBounds(24,260,192,24);

cardDeck = new Deck();

Iterator suitIterator = Suit.VALUES.iterator();
while ( suitIterator.hasNext() )
{
Suit suit = (Suit) suitIterator.next();
Iterator rankIterator = Rank.VALUES.iterator();
while ( rankIterator.hasNext() )
{
Rank rank = (Rank) rankIterator.next();
String imageFile = directory + Card.getFilename( suit, rank );
ImageIcon cardImage = new ImageIcon( getImage( getCodeBase(), imageFile ) );
Card card = new Card( suit, rank, cardImage );
}
}

}

javax.swing.JLabel cardDisplayLabel = new javax.swing.JLabel();
javax.swing.JButton newDeckBtn = new javax.swing.JButton();
javax.swing.JButton newHighBtn = new javax.swing.JButton();
javax.swing.JButton newLowBtn = new javax.swing.JButton();

void newDeckBtn_actionPerformed(java.awt.event.ActionEvent event)
{
cardDeck.restoreDeck();
cardDeck.shuffle();
cardDisplayLabel.setIcon( null );
}
}

```

As for instantiating all the cards in the deck and having a random card come up at first, this is what I came up with...But I don't think its right because it doesn't work..

```
myHand = new Deck();

for ( int i = 0; i < 1; i++ )
{
Card drawnCard = cardDeck.dealCard();
}

for ( int i = 0; i < 1; i++ )
{
Card c = myHand.getCard( i );
}

```

### #4 higgsch

Reputation: 13
• Posts: 126
• Joined: 26-October 09

## Re: Card Game Problem

Posted 24 April 2012 - 05:19 PM

I see that you have removed your deal card button, added the high and low buttons, also you have the new game button. That looks much better, in my opinion.

You have declared the deck as cardDeck and you have instanciated the cardDeck object:

```cardDeck = new Deck();
```

But the predesigned Deck object was created requiring you to instanciate each card within the deck:

```* Represents a deck of playing cards.  In order to have maximum flexibility,
* this class does not implement a standard deck of playing cards; it only
* provides the functionality of a deck of cards.  The client programmer must
* instantiate a Deck object, then populate it with the set of playing cards
* appropriate for the card game being implemented.  This allows for proper
* implementation of card games such as pinochle (a 48-card deck containing
* only aces, nines, tens, jacks, queens, and kings in all four suits, with
* each card present twice in the deck) or casino-style blackjack (where six
* standard decks are used for a game).
```

(from the Deck.java file code)

This means that you'll have to add each and every card that you want available from the deck to the deck. (i.e. If you want a regular 52 card deck, add each card number in each suit to make 52 cards in the deck. If you want a different style deck, you'll need to use Deck.add() for each card you want to be in the deck) Right now, your cardDeck is empty, having no cards in it because you haven't added any (unless I am overlooking something in your code).

### #5 blackvelvet

Reputation: 1
• Posts: 20
• Joined: 17-March 12

## Re: Card Game Problem

Posted 26 April 2012 - 10:09 AM

Yeah, that's the point I'm stuck at. I don't think "Deck" is the right class to implement to bring all the card from the deck into the game. This is so confusing!

### #6 higgsch

Reputation: 13
• Posts: 126
• Joined: 26-October 09

## Re: Card Game Problem

Posted 26 April 2012 - 01:31 PM

You just need to create each card that you want in the deck.

```Card AceOfSpades = new Card(SPADES, ACE, 'IconImagePath');

```

or

```cardDeck.addCard(Card CardToAdd = new Card(cardSuit, cardRank, cardImageIcon));
//within loops to increment through the suits and ranks

```

For each card that you want to have in your deck.

Implementing the Deck class in this manner allows you to create your own style of deck. You might have a normal 52 card deck for playing Egyptian Ratscrew, you might have a deck of 104 cards for a game of Spider Solitaire, or you might even have a 3 card deck to demonstrate a magic trick. But that's the thing, you need to create the deck yourself with whatever cards you want. Then once you have your deck created, you can shuffle and deal or whatever you want to do with the deck.

Does that make more sense?

This post has been edited by higgsch: 26 April 2012 - 01:33 PM