Results 1 to 6 of 6
  1. #1
    Join Date
    Mar 2011
    Posts
    2
    Rep Power
    0

    Default Help with creating a deck of cards

    Hey Java Users.

    I am a new student to Java, and have been assigned a question to create a deck of cards and displaying them using one class for the card itself, one class for creating the deck and the main class to display the deck. I have had quite a few attempts at getting this to work without any success.

    So far i have got the card class working, which i know works and i can created a deck of cards, but i am having issues assigning values into the deck created. I was planning on using an array to save myself hundreds of lines of programming creating new objects for each card.

    I believe my issue is with this method.
    public void CreateDeck(){
    for(int counter = 0; counter <= 51; counter++){
    DECK[counter] = new Card();
    //use createCard method on new card created
    }
    }

    When entered in the program like this, it works. I have the method called createCard which takes the counter information and applies the suit and rank of the card accordingly. I am wanting to use this method onto the new Card which was created and entered into the DECK array.

    I was wondering if it is possible to do this, or alternatively to be able to pass this information straight into the class called Card itself?

    Thanks so much for your help in advance :)
    Carouselification.

  2. #2
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,585
    Rep Power
    12

    Default

    Leopards don't change their spots and neither do playing cards. In some places the attempt to do so might get you shot.

    I think it would be better to follow reality and give the Card constructor an argument which would determine its value at the time it was created. The card's value would never change thereafter ie there would be no "setter" method by whatever name). Or possibly two arguments: rank and suit.

    The deck proceeds to create its array of Card instances being careful that all 52 values are represented exactly once each.

    (Somewhere down the line you are sure to want to shuffle the deck. I would use a List<Card> deck rather than a Card[] deck because lists have a built in shuffle method.)
    Last edited by pbrockway2; 03-06-2011 at 04:59 AM.

  3. #3
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    What kind of method is createCard, my best tip is to create it to accept one integer argument and use just that argument to create a simple card. Use % and / to find the suit and rank. Then you can instantiate the array by looping and settig each index to a card using the index number. If this isn't clear let me know and I can clarify for you.

  4. #4
    Join Date
    Mar 2011
    Posts
    2
    Rep Power
    0

    Default

    What do you mean by give the card constructor an argument? How would i go by that?
    Also, how does the list<card> work? What is the difference between a list and an array?

    sunde887, I think i have done what you suggested using one value and using % to find and set the suit and rank. I dont understand what you mean about the instantiate the array by looping and setting each index to a card using the index number though. Do you think you could go through that with me? I have a basic understanding how they work and relate to each other, just not in Java.

  5. #5
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    Alright, i will do my best to explain it better. First i wanna answer the constructor argument with you, do you know what a constructor is? You can overload it with arguments. Instead of a method to take an argument you can have a constructor that creates the object and takes an argument. Let's say you make it take an int in the constructor, you can than have it create a card with the appropriate rank and suit. I don't have too much time right now to explain.

    My idea though is this

    Java Code:
    for(int j = 0; j < 51; j++){
      Array[j] = createCard(j);
    }
    However, try looking up the arraylist API and using that instead for the deck. Also try and create a card constructor to do the instantiation(instantiation is just a big word for create an object)

  6. #6
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,585
    Rep Power
    12

    Default

    What do you mean by give the card constructor an argument? How would i go by that?
    Java Code:
    import java.util.ArrayList;
    import java.util.Arrays;
    import java.util.Collections;
    import java.util.List;
    
    /*public*/ class Card {
        private int suit;  // 0->3 == CDHS
        private int value; // 0->12 == 2->A
    
            // a constructor with arguments
        public Card(int value, int suit) {
            this.suit = suit;
            this.value = value;
        }
    
            // we have "getters"
        public int getValue() {
            return value;
        }
    
            // but *no* setters
    
        @Override
        public String toString() {
            String[] suits = {"C", "D", "H", "S"};
            String[] values = {"2", "3", "4", "5", "6", "7", "8", "9", 
                    "T", "J", "Q", "K", "A"};
            return values[value] + suits[suit];
        }
    }

    This is really just intended to show a constructor with arguments. I haven't thought about it and would not necessarily use two ints like this to represent suit and value - although it's perfectly possible to do so.

    Also, how does the list<card> work? What is the difference between a list and an array?

    Like this:

    Java Code:
    public class Deck {
        private List<Card> deck;
    	
        public Deck() {
            deck = new ArrayList<Card>();
                // something like this was what you were wanting to do
            for(int i = 0; i < 52; i++) {
                deck.add(new Card(i % 13, i / 13));
            }
        }
    	
        public void shuffle() {
            Collections.shuffle(deck);
        }
    	
        // etc
    	
        @Override
        public String toString() {
            return Arrays.toString(deck.toArray());
        }
    
        public static void main(String[] args) {
            Deck test = new Deck();
            System.out.println("Newly created deck: " + test);
            
            test.shuffle();
            System.out.println("Shuffled deck: " + test);
        }
    }

    The major difference between an array and a list in this case is the huge number of library methods that exist for things like shuffling and getting a decent String for the toString() method. They will take some work (see the Collections trail in Oracle's Tutorial), but it pays off.

    Unlike arrays, lists can also change size. Of course a deck is always 52 cards, but you may have need of a way of representing a group of cards that can change in size (a hand, or the cards remainging to be played etc). List provides the ability to add and remove elements without the nasty "null" values that would have to be used in the case of an array.

    (Again: the code is not intended as a "solution" but just an illustration of constructors with arguments, and collection framework methods. It will run if all pasted into one Deck.java file although you wouldn't write it like that!)
    Last edited by pbrockway2; 03-06-2011 at 05:49 AM.

Similar Threads

  1. Deck of cards problem
    By VelvetMirror in forum New To Java
    Replies: 2
    Last Post: 02-16-2011, 09:02 PM
  2. Creating a deck of cards and printing in console
    By Danieldcc in forum New To Java
    Replies: 7
    Last Post: 10-12-2010, 08:37 AM
  3. Deck of Cards
    By khunmato in forum New To Java
    Replies: 13
    Last Post: 09-06-2009, 06:47 PM
  4. Problem with making/sorting a deck of cards
    By Franneldort in forum New To Java
    Replies: 9
    Last Post: 11-07-2008, 01:47 AM
  5. creating a deck of cards using a linked list
    By boomba88 in forum New To Java
    Replies: 2
    Last Post: 09-11-2008, 04:34 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
  •