Lost Password?

Go Back   CodeCall Programming Forum > Software Development > Java Help

Unregistered, Check out the Coder Battles in the Announcement and Game forums.

Java Help Java Help forum discussing all Java platforms - J2ME, J2SE and J2EE - as well as relevant standards, APIs and frameworks such as Swing, Servlets, JSPs, Applets, Struts, Spring, Hibernate, ANT, EJB, and other Java-related topics.

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 01-09-2008, 03:21 AM
damien's Avatar   
damien damien is offline
Newbie
 
Join Date: Jan 2008
Location: Philippines
Age: 17
Posts: 3
Credits: 0
Rep Power: 0
damien is on a distinguished road
Question create a deck of cards with objects

I'm using a Linked list stack with objects. I figured out the reason for my earlier problem (where I couldn't access "rank" and "suit" from one of the objects in my stack), and it was because my object didn't have the attributes "rank" and "suit". Can anyone help me create a whole deck of cards represented by 52 objects in a linked list stack with the attributes "suit" and "rank"?
__________________

To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.

On the whole human beings want to be good, but not too good, and not quite all the time.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote

Sponsored Links
  #2 (permalink)  
Old 01-09-2008, 12:43 PM
John's Avatar   
John John is offline
Co-Administrator
 
Join Date: Jul 2006
Age: 19
Posts: 3,211
Last Blog:
Passwords
Credits: 842
Rep Power: 20
John has much to be proud ofJohn has much to be proud ofJohn has much to be proud ofJohn has much to be proud ofJohn has much to be proud ofJohn has much to be proud ofJohn has much to be proud ofJohn has much to be proud ofJohn has much to be proud of
Send a message via AIM to John
Default

It's probably best to create an ADT to represent the card object. Then another ADT to represent the deck object.
__________________

To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
|
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
|
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
|
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
|
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.

Chat with other CodeCall members on IRC; connect to irc.codecall.net and join #codecall
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #3 (permalink)  
Old 01-09-2008, 08:28 PM
gszauer's Avatar   
gszauer gszauer is offline
Programmer
 
Join Date: Nov 2007
Location: Florida
Age: 18
Posts: 113
Credits: 0
Rep Power: 4
gszauer is on a distinguished road
Default

Java5 Code:
  1. /**
  2. *  An object of type Deck represents a deck of playing cards.  The deck
  3. *  is a regulate poker deck that contains 52 regular cards and that can
  4. *  also optionally include two Jokers.
  5. */
  6. public class Deck {
  7.    
  8.    /**
  9.     * An array of 52 or 54 cards.  A 54-card deck contains two Jokers,
  10.     * in addtion to the 52 cards of a regular poker deck.
  11.     */
  12.    private Card[] deck;
  13.    
  14.    /**
  15.     * Keeps track of the number of cards that have been dealt from
  16.     * the deck so far.
  17.     */
  18.    private int cardsUsed;
  19.    
  20.    /**
  21.     * Constructs a regular 52-card poker deck.  Initially, the cards
  22.     * are in a sorted order.  The shuffle() method can be called to
  23.     * randomize the order.  (Note that "new Deck()" is equivalent
  24.     * to "new Deck(false)".)
  25.     */
  26.    public Deck() {
  27.       this(false)// Just call the other constructor in this class.
  28.    }
  29.    
  30.    /**
  31.     * Constructs a poker deck of plaing cards, The deck contains
  32.     * the ususal 52 cards and can optionally contain two Jokers
  33.     * in addtion, for a total of 54 cards.    Initially the cards
  34.     * are in a sorted order.  The shuffle() method can be called to
  35.     * randomize the order.
  36.     * @param includeJokers if true, two Jokers are included in the deck; if false,
  37.     * there are no Jokers in the deck.
  38.     */
  39.    public Deck(boolean includeJokers) {
  40.       if (includeJokers)
  41.          deck = new Card[54];
  42.       else
  43.          deck = new Card[52];
  44.       int cardCt = 0; // How many cards have been created so far.
  45.       for ( int suit = 0; suit <= 3; suit++ ) {
  46.          for ( int value = 1; value <= 13; value++ ) {
  47.             deck[cardCt] = new Card(value,suit);
  48.             cardCt++;
  49.          }
  50.       }
  51.       if (includeJokers) {
  52.          deck[52] = new Card(1,Card.JOKER);
  53.          deck[53] = new Card(2,Card.JOKER);
  54.       }
  55.       cardsUsed = 0;
  56.    }
  57.    
  58.    /**
  59.     * Put all the used cards back into the deck (if any), and
  60.     * shuffle the deck into a random order.
  61.     */
  62.    public void shuffle() {
  63.       for ( int i = deck.length-1; i > 0; i-- ) {
  64.          int rand = (int)(Math.random()*(i+1));
  65.          Card temp = deck[i];
  66.          deck[i] = deck[rand];
  67.          deck[rand] = temp;
  68.       }
  69.       cardsUsed = 0;
  70.    }
  71.    
  72.    /**
  73.     * As cards are dealt from the deck, the number of cards left
  74.     * decreases.  This function returns the number of cards that
  75.     * are still left in the deck.  The return value would be
  76.     * 52 or 54 (depending on whether the deck includes Jokers)
  77.     * when the deck is first created or after the deck has been
  78.     * shuffled.  It decreases by 1 each time the dealCard() method
  79.     * is called.
  80.     */
  81.    public int cardsLeft() {
  82.       return deck.length - cardsUsed;
  83.    }
  84.    
  85.    /**
  86.     * Removes the next card from the deck and return it.  It is illegal
  87.     * to call this method if there are no more cards in the deck.  You can
  88.     * check the number of cards remaining by calling the cardsLeft() function.
  89.     * @return the card which is removed from the deck.
  90.     * @throws IllegalStateException if there are no cards left in the deck
  91.     */
  92.    public Card dealCard() {
  93.       if (cardsUsed == deck.length)
  94.          throw new IllegalStateException("No cards are left in the deck.");
  95.       cardsUsed++;
  96.       return deck[cardsUsed - 1];
  97.       // Programming note:  Cards are not literally removed from the array
  98.       // that represents the deck.  We just keep track of how many cards
  99.       // have been used.
  100.    }
  101.    
  102.    /**
  103.     * Test whether the deck contains Jokers.
  104.     * @return true, if this is a 54-card deck containing two jokers, or false if
  105.     * this is a 52 card deck that contains no jokers.
  106.     */
  107.    public boolean hasJokers() {
  108.       return (deck.length == 54);
  109.    }
  110.    
  111. } // end class Deck
  112.  
The first result on google....
java card deck - Google Search

There is also this Enums tutorial
Enums

Is either one what you are looking for?
__________________
Quote:
Originally Posted by ~Aristotle
It is the mark of an educated mind to entertain a tought without accepting it
If my post was helpful, please help me build some rep
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.

Last edited by gszauer; 01-09-2008 at 08:30 PM.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #4 (permalink)  
Old 01-09-2008, 08:41 PM
John's Avatar   
John John is offline
Co-Administrator
 
Join Date: Jul 2006
Age: 19
Posts: 3,211
Last Blog:
Passwords
Credits: 842
Rep Power: 20
John has much to be proud ofJohn has much to be proud ofJohn has much to be proud ofJohn has much to be proud ofJohn has much to be proud ofJohn has much to be proud ofJohn has much to be proud ofJohn has much to be proud ofJohn has much to be proud of
Send a message via AIM to John
Default

Thats 50% of what I said, but that class also needs a "Card" object to instantiate.
__________________

To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
|
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
|
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
|
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
|
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.

Chat with other CodeCall members on IRC; connect to irc.codecall.net and join #codecall
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #5 (permalink)  
Old 01-10-2008, 10:05 AM
gszauer's Avatar   
gszauer gszauer is offline
Programmer
 
Join Date: Nov 2007
Location: Florida
Age: 18
Posts: 113
Credits: 0
Rep Power: 4
gszauer is on a distinguished road
Default

Oh, i see.
I'll do some research, post what i come up with tommorow.
__________________
Quote:
Originally Posted by ~Aristotle
It is the mark of an educated mind to entertain a tought without accepting it
If my post was helpful, please help me build some rep
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote

Sponsored Links
  #6 (permalink)  
Old 01-10-2008, 05:33 PM
damien's Avatar   
damien damien is offline
Newbie
 
Join Date: Jan 2008
Location: Philippines
Age: 17
Posts: 3
Credits: 0
Rep Power: 0
damien is on a distinguished road
Default

Thank you very much everyone. I figured out how to make the Card class now as well.
__________________

To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.

On the whole human beings want to be good, but not too good, and not quite all the time.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #7 (permalink)  
Old 01-11-2008, 02:24 PM
gszauer's Avatar   
gszauer gszauer is offline
Programmer
 
Join Date: Nov 2007
Location: Florida
Age: 18
Posts: 113
Credits: 0
Rep Power: 4
gszauer is on a distinguished road
Default

Quote:
Originally Posted by damien View Post
Thank you very much everyone. I figured out how to make the Card class now as well.
How about a sample?
Or some explanation, I'm interested in how you did it.
__________________
Quote:
Originally Posted by ~Aristotle
It is the mark of an educated mind to entertain a tought without accepting it
If my post was helpful, please help me build some rep
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Similar Threads
Thread Thread Starter Forum Replies Last Post
How to create a XML file at the server side? shannelleng ASP, ASP.NET and Coldfusion 8 08-12-2007 03:27 AM
Create pluggin with Java nilshv Java Help 0 04-08-2007 09:27 AM
Java:Tutorial - Making multiple objects work differently John Java Tutorials 0 01-11-2007 02:10 PM
Java:Tutorial - Adding more objects to your window John Java Tutorials 0 01-11-2007 02:09 PM
Objects Nightracer General Programming 8 07-28-2006 08:02 PM


All times are GMT -5. The time now is 11:32 PM.

Contest Stats

Xav ........ 1097.16
MeTh0Dz|Reb0rn ........ 986.37
morefood2001 ........ 850.04
John ........ 841.93
WingedPanther ........ 684.54
marwex89 ........ 638.26
Brandon W ........ 492.36
chili5 ........ 292.12
orjan ........ 187.41
Steve.L ........ 185.02

Contest Rules

CodeCall Goal

Goal: 100,000 Posts
Complete: 79%

Ads