Results 1 to 14 of 14

Thread: Deck of Cards

  1. #1
    khunmato is offline Member
    Join Date
    Sep 2009
    Posts
    7
    Rep Power
    0

    Default Deck of Cards

    Hello all!!

    Please please help me..Iīve got stuck, and really bad to. I have tried so many different ways now and really donīt know what to do anymore..

    I have an assigment for school, the deadline is very very close, that I need some help with. Here is a copy of it:

    Write a class called deckOfCards. The class shall be able to create a deck of cards (52 different cards), shuffle the deck and pull the top card from the deck.

    Then write a program that uses the class deckOfCards, for example create an object of the class, shuffle the cards and print out the top card with its suit (spades, hearts, diamonds, clubs) and value (1-13).


    This is what Iīve got so far..feel free to to not use what I have..


    Java Code:
    public class Card {
       private CardSuit s;
       private CardValue v;
    
       public Card(CardSuit suit, CardValue value) {
         s = suit;
         v = value;
       }
    
       public CardSuit suit() {  
         return s;
       }
    
       public CardValue value() { 
         return v;
       }
    
       public String toString() {
         return f.toString() + " " + s.toString();
       }
    
     }
    Please if there is anyone...

  2. #2
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    7

    Default

    Your assignment starts with the words "Write a class called deckOfCards".
    Have you written that class somewhere?

  3. #3
    khunmato is offline Member
    Join Date
    Sep 2009
    Posts
    7
    Rep Power
    0

    Default

    Thanks for the reply!


    This is what I got at this stage..

    Java Code:
    public class DeckOfCards {
    
    public static void main (String args[]){
    
    	int numberOfCards;
    	
    	String [] suit = { "Diamonds", "Spades", "Cluds", "Hearts"};
    	String [] value = { "Narf", "Ace", "Duece", "Three", "Four", "Five", "Six", "Seven", 
    							"Eight", "Nine", "Ten", "Jack","Queen", "King"};
    	Card []card = new Card [52];
    	for ( int suit = 0; <suit.length; suit++ ) 
        { 
            for ( int value = 1; <value.lenght;value++ )
            { 
    
                card [numberOfCards] = new Card(value,suit[]); 
                numberOfCards ++; 
            } 
    
        }
    		
    	
    }
    
    }

  4. #4
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    7

    Default

    Don't do everything in main method. Read your specs.

    The class shall be able to create a deck of cards (52 different cards),
    So have an initialize method and call it from the constructor of the class.

    shuffle the deck
    so you need a shuffle method in that class.
    and pull the top card from the deck.
    then you need a getTopCard() method

  5. #5
    khunmato is offline Member
    Join Date
    Sep 2009
    Posts
    7
    Rep Power
    0

    Default

    Ok..here is how far I have come, still canīt get it to work though.


    Java Code:
    import javax.swing.*;
    import java.awt.*;
    
    
    public class DeckOfCards {
    
    
    
    	private Card deck[];
    	private int currentCard;
    
    	
    	public DeckOfCards(){
    
    
    		String faces[] = { "Ace", "Deuce", "Three", "Four","Five", "Six", 
    		    "Seven", "Eight", "Nine", "Ten", "Jack", "Queen", "King" };
    		String suits[] = { "Hearts", "Diamonds", "Clubs", "Spades" };
            
    		 deck = new Card[ 52 ];
    		currentCard = -1;
            
    		 for ( int i = 0; i < deck.length; i++ )
    		deck[ i ] = new Card( faces[ i % 13 ], suits[ i / 13 ] );
           
         }
        
    		 public void shuffle(){
    
    			 currentCard = -1;
            
    			 for ( int i = 0; i < deck.length; i++ ){
    				 int j = ( int ) ( Math.random() * 52 );
    				 Card temp = deck[ i ];
    				 deck[ i ] = deck[ j ]; 
    			     deck[ j ] = temp; 
             }
            
     
         }
        
    		   public Card dealCard(){
    
    				 if ( ++currentCard < deck.length )
    				 return deck[ currentCard ];
    				 else{
    					 
    					 return null;
             }
    
         }
        
         public static void main( String args[] ){
    		 DeckOfCards app = new DeckOfCards();
    		 System.out.println(app);
            
      
         }
    }


    Java Code:
    class Card 
         {
         private String face;
         private String suit;
        
         public Card( String f, String s )
             {
             face = f;
             suit = s;
         }
        
         public String toString() 
             { 
             return face + " of " + suit; 
         }
    }
    Anyone??

  6. #6
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Ok..here is how far I have come, still canīt get it to work though.
    You may wish to tell us more on how it's currently not working. Also, could you be a little less "creative" in your code indentation? Your current code is next to impossible to read. Thanks and much luck.

  7. #7
    khunmato is offline Member
    Join Date
    Sep 2009
    Posts
    7
    Rep Power
    0

    Default

    Thanks for the prompt reply

    This is the print I get in the complier; DeckOfCards@3e25a5


    And here is the code again

    Java Code:
    public class DeckOfCards {
    	 
         private Card deck[];
         private int currentCard;
    
         public DeckOfCards(){
            
             String faces[] = { "Ace", "Deuce", "Three", "Four","Five", "Six", 
    				"Seven", "Eight", "Nine", "Ten", "Jack", "Queen", "King" };
             String suits[] = { "Hearts", "Diamonds", "Clubs", "Spades" };
             deck = new Card[ 52 ];
             currentCard = -1;
            
             for ( int i = 0; i < deck.length; i++ )
    			deck[ i ] = new Card( faces[ i % 13 ], suits[ i / 13 ] );
         }
        
         public void shuffle(){
             currentCard = -1;
             for ( int i = 0; i < deck.length; i++ ) 
                 {
                 int j = ( int ) ( Math.random() * 52 );
                 Card temp = deck[ i ];
                 deck[ i ] = deck[ j ]; 
                 deck[ j ] = temp; 
             }
            }
        
         public Card dealCard(){
             if ( ++currentCard < deck.length )
    			 return deck[ currentCard ];
    		 else {return null;}
    		}
        
         public static void main( String args[] ){
    
            DeckOfCards app = new DeckOfCards();
    		app.shuffle();
    		app.dealCard();
    		String s = app.toString();
    		System.out.println(s);
         }
    }
    Last edited by khunmato; 09-06-2009 at 03:15 PM.

  8. #8
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by khunmato View Post
    Thanks for the prompt reply

    This is the print I get in the complier; DeckOfCards@3e25a5
    Thanks for reposting the code with decent indentation -- it is now much easier to read!

    Your problem may be helped by giving DeckOfCards a toString method. This method will have to iterate through the deck using a for loop and adding the resultant Strings together and returning the long string at the end. Or you can do this by creating a StringBuffer object in the toString method and using its append method in your for loop, then after the for loop completes returning the StringBuffer's string via its toString method.

    Please let me know if this is confusing or if it makes sense.

  9. #9
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    7

    Default

    Just thought I'd add that it's probably better to use StringBuilder rather than StringBuffer in this case.

  10. #10
    khunmato is offline Member
    Join Date
    Sep 2009
    Posts
    7
    Rep Power
    0

    Default

    It is a bit confusing, well all of it actually.
    Iīve looked around in my books(and head) and arenīt quite sure how I would go about to create one of these solutions. Perhaps itīs the effect of my looong weekend.

    Do you mind helping me a bit more?

  11. #11
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    1. Create a toString method for your DeckOfCards class.
    2. At the beginning of this method declare and initialize a StringBuilder object.
    3. After this have a for loop, and loop through all the cards in the deck array.
    4. Within the for loop, add each card's toString string to the StringBuilder object via its append method. You may also want to append the String "\n" after each card's toString to give a next line char.
    5. After the for loop, return the StringBuilder's string by calling toString on your StringBuilder object.


    That's it. I really can't help any more until you at least try to implement some of this. Your move.

  12. #12
    khunmato is offline Member
    Join Date
    Sep 2009
    Posts
    7
    Rep Power
    0

    Default

    Thanks...I will give it a try

  13. #13
    khunmato is offline Member
    Join Date
    Sep 2009
    Posts
    7
    Rep Power
    0

    Default

    It works!!:D

    What I needed to do was to change in "main"

    Java Code:
         
    public static void main( String args[] ){
    
           DeckOfCards app = new DeckOfCards();
           app.shuffle();
    [B]       Card c = app.dealCard();
           System.out.println(c);[/B]
         }


    I appreciate all of you help. Thank you!!

  14. #14
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    7

    Default

    Great, that is what your main method should look like.
    Now compare that with what you had when you started.

Similar Threads

  1. Problem with making/sorting a deck of cards
    By Franneldort in forum New To Java
    Replies: 9
    Last Post: 11-07-2008, 12:47 AM
  2. creating a deck of cards using a linked list
    By boomba88 in forum New To Java
    Replies: 2
    Last Post: 09-11-2008, 03:34 PM
  3. Display random cards
    By carl in forum Advanced Java
    Replies: 1
    Last Post: 08-07-2007, 07:01 AM
  4. Help with Random cards
    By carl in forum Java Applets
    Replies: 1
    Last Post: 08-03-2007, 08:48 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •