Results 1 to 7 of 7
  1. #1
    sjud9227 is offline Member
    Join Date
    Jul 2014
    Posts
    3
    Rep Power
    0

    Default Help with deck of cards and dealing five cards...

    Hello, I have an assignment for my summer class. FYI, I'm super new to programming in general and this is my first time with java and it's a summer class so it's going super fast (please be nice!) lol.

    Basically I started writing my code in one class, then I split it up into a Card class and a DeckOfCards class and I now need to figure out how to get it all to work together. I get a little confused with calling methods sometimes, especially when separate classes are in play. I think I just need a method to deal out . Besides getting it all working together correctly. I'm also having trouble creating the method to deal out five cards that also tells how many cards are left in the deck and I believe I need a toString method but I honestly do not know how to go about that. Any help is greatly appreciated! If you could help explain things too that would be awesome! I think I have everything SO FAR correct but I could be wrong and I'm sure there are better ways to write the code, I'll take any suggestions for a cleaner look too. FYI, I think the prof would rather arrays then enums since we're dealing with arrays right now hence the array for the 52 card deck.

    Here are the directions...
    Design and implement a class called Card that represents a standard playing card. Each card has a suit and a face value. Then create a class called DeckOfCards that stores 52 objects of the Card class. Include methods to shuffle the deck, deal a card and report the number of cards left in the deck. The shuffle methods should assume a full deck. Create a driver class (CardsGame) with a main method that deals five cards from the shuffled deck, printing each card as it is dealt. Make sure to write the appropriate constructors, getters, setters, toString and other methods as required for both classes.


    The main class, CardsGame Class
    Java Code:
        import java.util.Scanner;
    
        public class CardsGame {
            public static void main (String [] args) {  
            
            DeckOfCards deck = new DeckOfCards();
            //call shuffle
            deck.shuffle();
        
           }
        }
    Card Class

    Java Code:
        
        class Card {
           public static final int SPADE   = 4;
           public static final int HEART   = 3;
           public static final int CLUB    = 2;
           public static final int DIAMOND = 1;
        
           private int rank;
           private int suit;
           private static final String[] Suit = {"Hearts", "Clubs", "Spades", "Diamonds"};
           private static final String[] Rank = {"Ace", "2", "3", "4", "5", "6", "7", "8", "9", "10", "Jack", "Queen", "King"};                     
    
           private int cardSuit;
           private int cardRank;
    
           public Card(int suit, int rank) {
              if (rank == 1)
                 cardRank = 14;     // Give Ace the rank 14
              else
                 cardRank = (int) rank;
                 cardSuit = (int) suit;
           }
    
           public int suit() {
              return this.cardSuit;         
           }
    
           public String suitStr() {
              return(this.Suit[ this.cardSuit ]);                                
           }
    
           public int rank() {
              return this.cardRank;
           }
    
           public String rankStr() {
              return ( Rank[ cardRank ] );
           }
    
    
           public String toString() {
              return ( Rank[ cardRank ] + Suit[ cardSuit ] );
           }
        }
    DeckOfCards Class

    Java Code:
        class DeckOfCards {
           public static final int NEWCARDS = 52;
           private Card[] deckOfCards;         // Contains all 52 cards
           private int currentCard;            // deal THIS card in deck
    
        public DeckOfCards( ) {
          deckOfCards = new Card[NEWCARDS];
          int i = 0;
    
          for ( int suit = Card.DIAMOND; suit <= Card.SPADE; suit++ )
             for ( int rank = 1; rank <= 13; rank++ )
                 deckOfCards[i++] = new Card(suit, rank);
                 currentCard = 0;
         }
    
    
           //shuffle(n): shuffle the deck
           public void shuffle(int n) {
              int i, j, k;
              for ( k = 0; k < n; k++ ) {
    	          i = (int) ( NEWCARDS * Math.random() );  // Pick 2 random cards
    	          j = (int) ( NEWCARDS * Math.random() );  // in the deck?
    
    	  //swap these randomly picked cards
    	      Card temp = deckOfCards[i];
    	      deckOfCards[i] = deckOfCards[j];
    	      deckOfCards[j] = temp;
           }
          currentCard = 0;   // Reset current card to deal
          }
        }
    Last edited by sjud9227; 08-02-2014 at 09:09 AM.

  2. #2
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,800
    Rep Power
    7

    Default Re: Help with deck of cards and dealing five cards...

    Quote Originally Posted by sjud9227 View Post
    int deck.shuffle();
    Even a beginner should be able to tell there is something wrong with that line.

    BTW use code tags around your code to retain formatting.

  3. #3
    sjud9227 is offline Member
    Join Date
    Jul 2014
    Posts
    3
    Rep Power
    0

    Default Re: Help with deck of cards and dealing five cards...

    Sorry about that.

  4. #4
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,800
    Rep Power
    7

    Default Re: Help with deck of cards and dealing five cards...

    You should have a variable that keeps track of how many cards are in the deck. Decrease when a card is removed (dealt) and increase it when a card is returned (useful for card games that have a discard pile). The "report number of cards" method simply has to return that value.

    The "deal a card" method removes a card from the front of the deck and returns it.

  5. #5
    javaone01 is offline Member
    Join Date
    Aug 2014
    Posts
    3
    Rep Power
    0

    Default Re: Help with deck of cards and dealing five cards...

    29: currentCard = 0; // Reset current card to deal
    This doesn't do what you think it does, When you have a variable outside of a constructor, the program will declare it during initialization and will not run it again after that, you have to put that inside of a constructor if you want the program to reset the card count.

  6. #6
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,225
    Rep Power
    20

    Default Re: Help with deck of cards and dealing five cards...

    Quote Originally Posted by javaone01 View Post
    29: currentCard = 0; // Reset current card to deal
    This doesn't do what you think it does, When you have a variable outside of a constructor, the program will declare it during initialization and will not run it again after that, you have to put that inside of a constructor if you want the program to reset the card count.
    Not sure what you're getting at there.
    Whenever the shuffle method is called then the currentCard will be reset to 0, which makes sense.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  7. #7
    jbarke12's Avatar
    jbarke12 is offline Member
    Join Date
    May 2013
    Location
    Charleston, South Carolina USA
    Posts
    88
    Rep Power
    0

    Default Re: Help with deck of cards and dealing five cards...

    Also to make your suit and rank Strings to work correctly.... Ace would have to be mapped to 0 and King would be 13. May think about using Enums instead of a string arrays that way you don't have to worry about the number values.

Similar Threads

  1. Shuffling a deck of cards
    By coolman50544 in forum New To Java
    Replies: 3
    Last Post: 05-31-2014, 09:48 PM
  2. Deck of Cards Program
    By Hector in forum New To Java
    Replies: 6
    Last Post: 03-12-2014, 07:37 PM
  3. Help with creating a deck of cards
    By Carouselification in forum New To Java
    Replies: 5
    Last Post: 03-06-2011, 05:45 AM
  4. Deck of cards problem
    By VelvetMirror in forum New To Java
    Replies: 2
    Last Post: 02-16-2011, 09:02 PM
  5. Deck of Cards
    By khunmato in forum New To Java
    Replies: 13
    Last Post: 09-06-2009, 06:47 PM

Tags for this Thread

Posting Permissions

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