-5

Hey im trying to make a card of deck

the deck should contain 52 cards,

ofc with spade,diamonds,heart,clubs with ranks ,

this is my code so far i havent gotten any longer Thanks for help in advance.

get the deck to shuffle cards ,Create a class that represents a deck containing 52 cards, When a new object of this class is created, the deck is initialized with the cards that it will contain.

public class Card {
    int[] deck = new int[52];

    String[] suits = {"Spades", "Hearts", "Diamonds", "Clubs"};
    String[] ranks = {"Ace", "2", "3", "4", "5", "6", "7", "8", "9", "10", "Jack", "Queen", "King"};

    Card() {
       for (int i = 0; i < deck.length; i++) {
           deck[i] = i;
       }
    }
}
Simon Forsberg
  • 13,086
  • 10
  • 64
  • 108
user2825771
  • 13
  • 2
  • 4
  • 7
    You have not asked a question – bengoesboom Sep 29 '13 at 20:39
  • To me it doesn't seem like you're asking a question. Please ask a real question. And please, before you ask a real question (for example: "How can I set a card to have a specific suit or rank?) try some more yourself first. – Simon Forsberg Sep 29 '13 at 20:40
  • how do i do it? get the deck to shuffle cards ,Create a class that represents a deck containing 52 cards, When a new object of this class is created, the deck is initialized with the cards that it will contain. – user2825771 Sep 29 '13 at 20:41
  • containing cards: `List cards;`, shuffling cards: `Collections.shuffle(cards);` – Simon Forsberg Sep 29 '13 at 20:43
  • I think it is obvious what the design "question" is here. – Glenn Sep 29 '13 at 21:11

3 Answers3

2

A Card is a not a Deck of Cards. A Card has attributes (i.e. member variables) like suit and rank (although for comparing it is usually useful to use integers - or enums - for the rank and then change how they are displayed).

A Deck of Cards is many cards; usually with restrictions established when the deck is created. In this case the suits[] and ranks[] can be used to build the many Cards in the deck (giving each Card a particular suit and rank when it is created). The easiest way to do this in Java is to use nested loops - once for each dimension.

The "color" of each card is derived from the suit and does not need to be stored as it can be determined given a suit. If enums are used this can be merely assigned as an attribute.

Going by the above logic, here is a sample set of classes:

class Card {
   final public String suit;
   final public String rank;
   public Card(String suit, String rank) {
      // Assign suit/rank from arguments
   }
   String getColor() {
      // Return the color based on suit (effectively "for free" if
      // using an enumeration)
   }
}

class DeckOfCards {
    String[] suits = {..};
    String[] ranks = {..};

    // Arrays are icky to deal with; favor Lists/Collections.
    List<Card> cards = new ArrayList<Card>();

    public DeckOfCards () {
       // For each suit/rank pair, create a Card and add it
       // to the cards collection.
       // You'll want nested loops, as shown by another answer.
    }

    public List<Card> getCards () {
        // Return cards, perhaps
    }

    // Other methods relating to the Deck of Cards
    public void shuffle () {
    }
}

While I would recommend looking into enums, the above should indicate the difference between the two distinct "things" - Cards and a Deck (or Collection) of Cards. The other answer contains code showing how to generate cards from the cross product of the two input arrays.

user2246674
  • 7,621
  • 25
  • 28
  • 1
    `IList`? You mean just `List` right? But otherwise yes, this is very important distinction that OP needs to make – Java Devil Sep 29 '13 at 20:58
  • @JavaDevil Yes, too much C#, thanks :) – user2246674 Sep 29 '13 at 20:58
  • Seems like a likely place for enums. – Glenn Sep 29 '13 at 21:09
  • what u guys mean about the enums stuf thanks for clearing it up @user2246674 – user2825771 Sep 29 '13 at 21:28
  • @user2825771 "An enum type is a special data type that enables for a variable to be a set of predefined constants. The variable must be equal to one of the values that have been predefined for it. Common examples include compass directions (values of NORTH, SOUTH, EAST, and WEST) and the days of the week [or suits of cards]." http://docs.oracle.com/javase/tutorial/java/javaOO/enum.html , http://stackoverflow.com/questions/2151232/java-class-card-enum-example-revised , http://stackoverflow.com/questions/2179854/javas-card-class-example-printing-enum-values – user2246674 Sep 29 '13 at 21:32
  • @user2825771 For the case of Suit it could be `Suit(Name,Color)` (e.g. `SPADES("Spades", "Black")`) and for the sake of a Rank it could be `Rank(Name,Value)` (e.g. `ACE("Ace", 1`). The value of this is that it lets us deal with a constant in code (`ACE`) but have easy access to it's characteristics (it is *called* an "Ace" but it *has a value of* 1). The characteristics of cards is important for actually using the cards, such has determining a straight (e.g. 9..10..J..Q..K) - if we *only* had the string name ("9", "King") it would be harder to determine such. – user2246674 Sep 29 '13 at 21:36
0

First of all, make suits[] and ranks[] into int arrays. Make deck an array of cards, and make deck and the constructor you are using in a different calss (Called deck) The change the constructor to Card(int suit, int name)Just do this:

for(int i = 0; i < 4; i++) for(int j = 0; j < 13; j++){
   Cards[13*i + j] = new Card(i, j); 
}
NerdicViking
  • 137
  • 9
0

Create a Card Object which has a suit and value property.

Create a method to create a List of all 52 Cards.

For your deck, I suggest using a Deque, Often pronounced "Deck" and has likeness to a deck of cards.

Then to create shuffled deck

//Create a List of cards
List<Card> deckList = ... //A method to create your 52 cards
Collections.shuffle(deckList);
Deque<Card> deck = new ArrayDeque<Card>(deckList);
Java Devil
  • 10,629
  • 7
  • 33
  • 48
  • I doubt the pronunciation is because the word's meaning is similar to a deck of cards, although that could be a question for [english stackexchange](http://english.stackexchange.com). The pronunciation is most likely because of the way the word is spelled. – Simon Forsberg Sep 29 '13 at 21:03
  • 1
    @SimonAndréForsberg Likely not, but it is an easy way to get the point across that it is a suitable structure to use for this situation, have edited. – Java Devil Sep 29 '13 at 21:07